R/uCareChemSuite_upgradation.R

Defines functions drug.class.stochastic

# Stochastic class predition starts here
#' uCAREChemSuiteCLI
#' @title drug.class.stochastic
#' @description Takes structure data file (SDF) of candidate drug, Nearest Neighbor value and threshold similarity score to predict its drug class using stochastic model.
#' @param sdf input sdf file
#' @param NearestNeighbor Nearest Neighbor = 1, 3
#' @param Threshold Threshold = 0.25, 0.3, 0.35, 0.4
#' @return Predicted drug class of the candidate drug using Nearest Neighbor algorithm
#' @usage drug.class.stochastic("sdf", "NearestNeighbor", "Threshold")
#' @import ChemmineR
#' @import stats
#' @import utils
#' @import usethis
#' @examples{
#' example.class.stochastic<- system.file('extdata/example.sdf', package="uCAREChemSuiteCLI")
#' drug.class.stochastic(example.class.stochastic,"3","0.25")
#' }
#' @export

drug.class.stochastic <- function(sdf, NearestNeighbor, Threshold){


  # Reading the Database
  db_antibiotics<- system.file('extdata/all_sdf_names.sdf', package="uCAREChemSuiteCLI")
  antibiotics <- read.SDFset(db_antibiotics)
  cid(antibiotics) <-  makeUnique(sdfid(antibiotics))
  apset <- sdf2ap(antibiotics)
  Score<- 0
  Drug_Name<-0


  # Reading Query
  sdf_input <- read.SDFset(sdf[[1]])
  sdf_input_ap<- sdf2ap(sdf_input)

  df<-cmp.search(apset, sdf_input_ap, type=3, cutoff = 77, quiet=TRUE)
  colnames(df)<- c("Index","Drug_Name","Score")

  # Clustering drug against database
  subset_of_drugs_with_equal_or_less_than_score <- subset(df, Score >= Threshold)
  drugs_falling_under_threshold<-head(subset_of_drugs_with_equal_or_less_than_score, n=as.numeric(NearestNeighbor))

  if(nrow(subset_of_drugs_with_equal_or_less_than_score) != 0)
  {
    # Finding the class of drug using Stochastic model
    antibiotic_dataset<- system.file('extdata/Antibiotic_data_set.csv', package="uCAREChemSuiteCLI")
    db<- read.csv2(antibiotic_dataset ,header = TRUE, sep=",")
    drug_list<- drugs_falling_under_threshold[2]

    if (as.numeric(table(subset_of_drugs_with_equal_or_less_than_score["Score"] >=Threshold)["TRUE"]) < as.numeric(NearestNeighbor))
    {
      subset_of_drug_class<- data.frame()
      subset_of_drug_class_final<- data.frame()

      for(i in 1:length(drug_list[,1]))
      {
        for(j in 1:length(db[,1]))
        {
          drug<- drug_list[[1]][i]
          db_drug<- db[[1]][j]


          db_drug_final <- gsub(" ", "", db_drug[1])
          drug_final <- gsub("_", "", drug[1])

          if(db_drug_final[1] %in% drug_final[1])
          {
            subset_of_drug_class<-db[j,]
            subset_of_drug_class_final<- rbind(subset_of_drug_class_final,subset_of_drug_class)
          }
        }
      }

      #Concatanated dataframe of drugname, drug class and similarity socre
      concat_table_drug_Class_score<- cbind(unique(subset_of_drug_class_final[,1:2]),drugs_falling_under_threshold[,3])

      #Prediction of drug class
      if(as.numeric(NearestNeighbor) == 1)
      {
        predicted_class<- c("Drug class:",as.character(concat_table_drug_Class_score[1,2]))
      }
      else if(as.numeric(NearestNeighbor) == 3)
      {
        if(as.numeric(concat_table_drug_Class_score[1,3])==1)
        {
          predicted_class<- c("Drug class:",as.character(concat_table_drug_Class_score[1,2]))
        }
        else
        {
          drug_class_frequency<- aggregate(data.frame(count = concat_table_drug_Class_score[,2]), list(value = concat_table_drug_Class_score[,2]), length)
          if(as.numeric(drug_class_frequency[1,2])>=2)
          {
            predicted_class<- c("Drug class:",as.character(drug_class_frequency[1,1]))
          }
          else
          {
            predicted_class<- c("Drug class:",as.character(drug_class_frequency[1,1]))
          }
        }
      }
    }
    else
    {
      neighbor <-subset_of_drugs_with_equal_or_less_than_score[1,2]
      fetchted_row<-   subset(db, Drug_Name %in% as.character(neighbor))
      predicted_class<-c("Drug class:", as.character(fetchted_row["Drug_class"][1,1]))
    }
  }
  else
  {
    predicted_class<- "Not enough neighbors"
  }
  #predicted_class<- db_drug[1]
  return(predicted_class)
  # Stochastic model Ends here ###############################################
}
Shivangi001/myfirstpackage documentation built on June 29, 2020, 12:51 a.m.