<commit_before> #SNOPSIS

 #Commands for running phenotypic correlation analysis.
 #Correlation coeffiecients are stored in tabular and json formats 

 #AUTHOR
 # <NAME> (<EMAIL>)


options(echo = FALSE)

library(gplots)
library(ltm)
library(plyr)
library(rjson)
library(lme4)
#library(rbenchmark)


allargs<-commandArgs()

refererQtl <- grep("qtl",
                   allargs,
                   ignore.case=TRUE,
                   perl=TRUE,
                   value=TRUE
                   )

phenoDataFile <- grep("\\/phenotype_data",
                      allargs,
                      ignore.case=TRUE,
                      perl=TRUE,
                      value=TRUE
                      )

correCoefficientsFile <- grep("corre_coefficients_table",
                              allargs,
                              ignore.case=TRUE,
                              perl=TRUE,
                              value=TRUE
                              )

correCoefficientsJsonFile <- grep("corre_coefficients_json",
                                  allargs,
                                  ignore.case=TRUE,
                                  perl=TRUE,
                                  value=TRUE
                                  )


formattedPhenoFile <- grep("formatted_phenotype_data",
                  allargs,
                  ignore.case = TRUE,
                  fixed = FALSE,
                  value = TRUE
                  )


formattedPhenoData <- c()
phenoData <- c()

if ( length(refererQtl) != 0 ) {

  phenoData <- read.csv(phenoDataFile,
                        header=TRUE,
                        row.names = NULL,
                        dec=".",
                        sep=",",
                        na.strings=c("NA", "-", " ", ".", "..")
                        )
 
} else {
  phenoData <- read.table(phenoDataFile,
                          header = TRUE,
                          row.names = NULL,
                          sep = "\t",
                          na.strings = c("NA", " ", "--", "-", ".", ".."),
                          dec = "."
                          )


} 

if (file.info(formattedPhenoFile)$size > 0 ) {

  formattedPhenoData <- read.table(formattedPhenoFile,
                                   header = TRUE,
                                   row.names = 1,
                                   sep = "\t",
                                   na.strings = c("NA", " ", "--", "-", "."),
                                   dec = "."
                                   )

  } else {
 
    phenoData <- read.table(phenoDataFile,
                            header = TRUE,
                            row.names = NULL,
                            sep = "\t",
                            na.strings = c("NA", " ", "--", "-", "."),
                            dec = "."
                            )
  }


allTraitNames      <- c()
nonTraitNames      <- c()
if (length(refererQtl) != 0) {

  allNames      <- names(phenoData)
  nonTraitNames <- c("ID")

  allTraitNames <- allNames[! allNames %in% nonTraitNames]
  
} else if (file.info(formattedPhenoFile)$size == 0) {
  dropColumns <- c("uniquename", "stock_name")
  phenoData   <- phenoData[,!(names(phenoData) %in% dropColumns)]

  allNames      <- names(phenoData)
  nonTraitNames <- c("object_name", "object_id", "stock_id", "design", "block", "replicate")

  allTraitNames <- allNames[! allNames %in% nonTraitNames]
 
}


if (!is.null(phenoData)) {
  for (i in allTraitNames) {

    if (class(phenoData[, i]) != 'numeric') {
      phenoData[, i] <- as.numeric(as.character(phenoData[, i]))
    }
    
    if (all(is.nan(phenoData$i))) {
      phenoData[, i] <- sapply(phenoData[, i], function(x) ifelse(is.numeric(x), x, NA))                     
    }
  }
}

phenoData     <- phenoData[, colSums(is.na(phenoData)) < nrow(phenoData)]
allTraitNames <- names(phenoData)[! names(phenoData) %in% nonTraitNames]
    
trait <- c()
cnt   <- 0
phenoTrait <- c()

###############################
if (length(refererQtl) == 0  ) {
  if (file.info(formattedPhenoFile)$size == 0) {
    for (i in allTraitNames) {

      cnt   <- cnt + 1
      trait <- i
  
      experimentalDesign <- c()
      
      if ('design' %in% colnames(phenoData)) {

        phenoTrait  <- subset(phenoData,
                              select = c("object_name", "object_id", "design", "block", "replicate", trait)
                              )
    
        experimentalDesign <- phenoTrait[2, 'design']
  
        if (is.na(experimentalDesign)) {
          experimentalDesign <- c('No Design')
        }
    
      } else {   
        experimentalDesign <- c('No Design')
      }

      if ((experimentalDesign == 'Augmented' || experimentalDesign == 'RCBD')  &&  unique(phenoTrait$block) > 1) {

      message("GS experimental design: ", experimentalDesign)

      augData <- subset(phenoTrait,
                        select = c("object_name", "object_id",  "block",  trait)
                        )

      colnames(augData)[1] <- "genotypes"
      colnames(augData)[4] <- "trait"

      model <- try(lmer(trait ~ 0 + genotypes + (1|block),<commit_msg>streamline code..<commit_after> #SNOPSIS

 #Commands for running phenotypic correlation analysis.
 #Correlation coeffiecients are stored in tabular and json formats 

 #AUTHOR
 # <NAME> (<EMAIL>)


options(echo = FALSE)

library(gplots)
library(ltm)
library(plyr)
library(rjson)
library(lme4)
#library(rbenchmark)


allargs<-commandArgs()

refererQtl <- grep("qtl",
                   allargs,
                   ignore.case=TRUE,
                   perl=TRUE,
                   value=TRUE
                   )

phenoDataFile <- grep("\\/phenotype_data",
                      allargs,
                      ignore.case=TRUE,
                      perl=TRUE,
                      value=TRUE
                      )

correCoefficientsFile <- grep("corre_coefficients_table",
                              allargs,
                              ignore.case=TRUE,
                              perl=TRUE,
                              value=TRUE
                              )

correCoefficientsJsonFile <- grep("corre_coefficients_json",
                                  allargs,
                                  ignore.case=TRUE,
                                  perl=TRUE,
                                  value=TRUE
                                  )


formattedPhenoFile <- grep("formatted_phenotype_data",
                  allargs,
                  ignore.case = TRUE,
                  fixed = FALSE,
                  value = TRUE
                  )


formattedPhenoData <- c()
phenoData <- c()

if ( length(refererQtl) != 0 ) {

  phenoData <- read.csv(phenoDataFile,
                        header=TRUE,
                        row.names = NULL,
                        dec=".",
                        sep=",",
                        na.strings=c("NA", "-", " ", ".", "..")
                        )
 
} else {

  phenoData <- read.table(phenoDataFile,
                          header = TRUE,
                          row.names = NULL,
                          sep = "\t",
                          na.strings = c("NA", " ", "--", "-", ".", ".."),
                          dec = "."
                          )
} 

if (file.info(formattedPhenoFile)$size > 0 ) {

  formattedPhenoData <- read.table(formattedPhenoFile,
                                   header = TRUE,
                                   row.names = 1,
                                   sep = "\t",
                                   na.strings = c("NA", " ", "--", "-", "."),
                                   dec = "."
                                   )
  } else {
 
    phenoData <- read.table(phenoDataFile,
                            header = TRUE,
                            row.names = NULL,
                            sep = "\t",
                            na.strings = c("NA", " ", "--", "-", "."),
                            dec = "."
                            )
  }


allTraitNames <- c()
nonTraitNames <- c()

if (length(refererQtl) != 0) {

  allNames      <- names(phenoData)
  nonTraitNames <- c("ID")

  allTraitNames <- allNames[! allNames %in% nonTraitNames]
  
} else if (file.info(formattedPhenoFile)$size == 0) {
  dropColumns <- c("uniquename", "stock_name")
  phenoData   <- phenoData[,!(names(phenoData) %in% dropColumns)]

  allNames      <- names(phenoData)
  nonTraitNames <- c("object_name", "object_id", "stock_id", "design", "block", "replicate")

  allTraitNames <- allNames[! allNames %in% nonTraitNames]
 
}


if (!is.null(phenoData)) {
  for (i in allTraitNames) {

    if (class(phenoData[, i]) != 'numeric') {
      phenoData[, i] <- as.numeric(as.character(phenoData[, i]))
    }
    
    if (all(is.nan(phenoData$i))) {
      phenoData[, i] <- sapply(phenoData[, i], function(x) ifelse(is.numeric(x), x, NA))                     
    }
  }
}


phenoData     <- phenoData[, colSums(is.na(phenoData)) < nrow(phenoData)]
allTraitNames <- names(phenoData)[! names(phenoData) %in% nonTraitNames]
    


###############################
if (length(refererQtl) == 0  ) {
  if (file.info(formattedPhenoFile)$size == 0) {
    
    cnt   <- 0
 
    for (i in allTraitNames) {

      cnt   <- cnt + 1
      trait <- i
  
      experimentalDesign <- c()
      
      if ('design' %in% colnames(phenoData)) {

        experimentalDesign <- phenoData[2, 'design']
  
        if (is.na(experimentalDesign)) {
          experimentalDesign <- c('No Design')
        }
    
      } else {   
        experimentalDesign <- c('No Design')
      }

      if ((experimentalDesign == 'Augmented' || experimentalDesign == 'RCBD')  &&  unique(phenoData$block) > 1) {

      message("GS experimental design: ", experimentalDesign)

      augData <- subset(phenoData,
                        select = c("object_name", "object_id",  "block",  trait)
                        )

      colnames(augData)[1] <- "genotypes"
      colnames(augData)[4] <- "trait"

      model <- try(lmer(trait ~ 0 + genotypes + (1|block),
{'fraction_non_alphanumeric': 0.13702305246422894, 'fraction_numerical': 0.003179650238473768, 'mean_word_length': 1.283866575901974, 'pattern_counts': {'":': 0, '<': 86, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2729222', 'n_tokens_mistral': 3167, 'n_tokens_neox': 2835, 'n_words': 515}