9 args = commandArgs(trailingOnly = TRUE)
11 pheno <- read.table(args[1], sep = "\t", header = TRUE)
13 study_trait <- args[2]
20 study_trait <- gsub("\\."," ",study_trait)
21 cat("study trait is ", study_trait,"\n")
24 names <- colnames(pheno)
25 allTraits <- grep("CO_", names, value = TRUE)
27 pheno <- pheno[,colnames(pheno) %in% c("locationName", "germplasmName", "studyYear", "blockNumber","replicate", allTraits)]
28 names <- colnames(pheno)
29 new_names <- gsub(".CO_.*","", names)
30 new_names <- gsub("\\."," ",new_names)
31 colnames(pheno) <- new_names
33 #Calculating missing data
34 missingData <- data.frame(missingData = tapply(pheno[,6], pheno$germplasmName, function(x){
35 return(length(which(is.na(x))))
38 missingData <- tibble::rownames_to_column(missingData, "germplasmName")
39 md <- sum(missingData$missingData)/nrow(pheno)
41 #Removing trait with more than 60% of missing data
43 message1 <- c("Please, check your dataset! There are more than 60% of missing data for selected trait.")
49 #Removing non numeric data
50 isNumeric <- is.numeric(pheno[,6])
51 if(isNumeric == "FALSE"){
52 message2 <- c("Please, check your dataset! The selected trait is not numeric.")
57 #checkning number of locations
58 szloc <- length(unique(pheno$locationName))
59 szreps <- length(unique(pheno$replicate))
60 szyr <- length(unique(pheno$studyYear))
63 # Still need check temp data to ensure wright dimension
64 an.error.occured <- FALSE
73 tryCatch({ for(i in 6:ncol(pheno)){
74 outcome = colnames(pheno)[i]
78 model <- lmer(get(outcome)~(1|germplasmName)+replicate,
79 na.action = na.exclude,
81 variance = as.data.frame(VarCorr(model))
82 gvar = variance [1,"vcov"]
84 resvar = variance [2, "vcov"]
86 model <- lmer(get(outcome) ~ (1|germplasmName) + replicate + studyYear,
87 na.action = na.exclude,
89 variance = as.data.frame(VarCorr(model))
90 gvar = variance [1,"vcov"]
92 resvar = variance [2, "vcov"]
94 }else if (szloc > 1) {
96 model <- lmer(get(outcome) ~ (1|germplasmName) + replicate + (1|locationName),
97 na.action = na.exclude,
99 variance = as.data.frame(VarCorr(model))
100 gvar = variance [1,"vcov"]
101 envar = variance [2, "vcov"]
102 resvar = variance [3, "vcov"]
104 model <- lmer(get(outcome) ~ (1|germplasmName) + replicate + (1|locationName) + studyYear,
105 na.action = na.exclude,
107 variance = as.data.frame(VarCorr(model))
108 gvar = variance [1,"vcov"]
109 envar = variance [2, "vcov"]
110 resvar = variance [3, "vcov"]
113 }else if (szreps == 1){
116 model <- lmer(get(outcome)~(1|germplasmName) + blockNumber,
117 na.action = na.exclude,
119 variance = as.data.frame(VarCorr(model))
120 gvar = variance [1,"vcov"]
122 resvar = variance [2, "vcov"]
124 model <- lmer(get(outcome) ~ (1|germplasmName) + studyYear + blockNumber,
125 na.action = na.exclude,
127 variance = as.data.frame(VarCorr(model))
128 gvar = variance [1,"vcov"]
130 resvar = variance [2, "vcov"]
132 }else if (szloc > 1){
134 model <- lmer(get(outcome)~(1|germplasmName)+ (1|locationName) + blockNumber,
135 na.action = na.exclude,
137 variance = as.data.frame(VarCorr(model))
138 gvar = variance [1,"vcov"]
139 envar = variance [2, "vcov"]
140 resvar = variance [3, "vcov"]
142 model <- lmer(get(outcome) ~ (1|germplasmName) + studyYear + (1|locationName) + blockNumber,
143 na.action = na.exclude,
145 variance = as.data.frame(VarCorr(model))
146 gvar = variance [1,"vcov"]
147 envar = variance [2, "vcov"]
148 resvar = variance [3, "vcov"]
153 H2 = append(H2, gvar/ (gvar + (envar) + (resvar)))
154 H2nw = format(round(H2[j], 4), nsmall = 4)
155 her = append(her, round(as.numeric(H2nw), digits =3))
156 Vg = append(Vg, round(as.numeric(gvar), digits = 3))
157 Ve = append(Ve, round(as.numeric(envar), digits = 2))
158 Vres = append(Vres, round(as.numeric(resvar), digits = 3))
159 resp_var = append(resp_var, colnames(pheno)[i])
163 }, error = function(e) {
164 an.error.occured <<- TRUE
165 errorMessages <<- c(errorMessages, as.character(e))
170 #Prepare information to export data
173 Heritability = data.frame(trait = resp_var,
180 Heritability = na.omit(Heritability)
182 h2_json <- jsonlite::toJSON(Heritability)
183 jsonlite::write_json(h2_json, h2File)
184 write.csv(Heritability, file = h2CsvFile)
186 }, error = function(e) {
187 an.error.occured <<- TRUE
188 errorMessages <<- c(errorMessages, as.character(e))
191 errorMessages <- c(errorMessages, as.character(message1))
192 errorMessages <- c(errorMessages, as.character(message2))
194 cat("Was there an error? ", an.error.occured,"\n")
195 if ( length(errorMessages) > 0 ) {
196 print(sprintf("Writing Error Messages to file: %s", errorFile))
198 write(errorMessages, errorFile)
202 #-------------------------------------------------------------------------