1 ## File was edited in ESS mode in emacs and some indents changed. sorry.
2 ##First read in the arguments listed at the command line
5 ##args is now a list of character vectors
6 ## First check to see if arguments are passed.
7 ## Then cycle through each element of the list and evaluate the expressions.
10 print("No arguments supplied.")
11 ##supply default values
18 for(i in 1:length(args)){
19 print(paste("Processing arg ", args[[i]]));
20 eval(parse(text=args[[i]]))
25 workdir = dirname(datafile);
35 pd = read.csv(datafile, sep="\t")
37 source(paramfile) # should give us dependent_variable and the model
39 trait = dependent_variables
40 pd$studyYear = as.factor(pd$studyYear)
41 print(paste("MODEL :", model))
42 print(paste("FIXED FACTORS: ", fixed_factors));
43 print(paste("RANDOM FACTORS: ", random_factors));
46 BLUE = as.data.frame(unique(pd$germplasmName))
47 colnames(BLUE) = "germplasmName"
53 for(i in 1:length(trait)){
55 print(paste("processing trait", trait[i]))
56 dependent_variables = trait[i]
57 dependent_variables = gsub(" ", "\\.", dependent_variables) # replace space with "." in variable name
58 dependent_variables = gsub("\\|", "\\.", dependent_variables) # replace | with .
59 dependent_variables = gsub("\\:", "\\.", dependent_variables)
60 dependent_variables = gsub("\\-", "\\.", dependent_variables)
61 dependent_variables = gsub("\\/", "\\.", dependent_variables)
63 print(paste("Dependent variables : ", dependent_variables))
65 model_string = paste(dependent_variables, '~', model)
67 print(paste('MODEL STRING:', model_string));
68 #mixmodel = lmer(as.formula(model_string), data=pd)
70 #model_summary = summary(allEffects(model,se=T))
75 genotypeEffectType = as.vector(str_match(model_string, '1\\|germplasmName'))
76 genotypeEffectType = ifelse(is.na(genotypeEffectType), 'fixed', 'random')
77 print(paste('modeling genotypes as: ', genotypeEffectType))
79 if (genotypeEffectType=="random") {
81 mixmodel = lmer(as.formula(model_string), data=pd)
87 res <- (ranef(mixmodel)$germplasmName)
89 blup <- res%>%mutate("germplasmName" = rownames(res))
90 names(blup)[1] = trait[i]
91 blup <- blup[,c("germplasmName",trait[i])]
93 blup <- as.data.frame(blup)
95 BLUP <- merge(x = BLUP, y = blup, by ="germplasmName", all=TRUE)
97 adj = coef(mixmodel)$germplasmName
99 print(paste("adj", adj));
101 adj = adj[1] # keep only one column
102 adj_means = adj%>%mutate("germplasmName" = rownames(adj))
103 names(adj_means)[1] = trait[i]
104 adj_means = as.data.frame(adj_means)
106 adjusted_means = merge(x = adjusted_means, y = adj_means, by ="germplasmName", all=TRUE)
110 mixmodel = lmer(as.formula(model_string), data=pd)
112 # compute adjusted blues
114 adj <- summary(lsmeans(mixmodel, "germplasmName"))
115 blue <- adj[c("germplasmName", "lsmean")]
116 colnames(blue)[2] = trait[i]
117 blueadj = as.data.frame(blue)
118 adjustedBLUE = merge(x = adjustedBLUE, y = blueadj, by ="germplasmName", all=TRUE)
120 #Computing fixed effects
121 #feff <- (fixef(mixmodel)$germplasmName)
122 feff<-data.frame(coef(summary(mixmodel))[ , "Estimate"])
123 rownames(feff) <- blue$germplasmName
124 colnames(feff) <- trait[i]
126 fixedeff <- feff%>%mutate("germplasmName" = rownames(feff))
127 fixedeff<- fixedeff[,c("germplasmName", trait[i])]
128 fixedeff<-as.data.frame(fixedeff)
130 #file with fixed effect
136 BLUE = merge(x = BLUE, y = fixedeff, by="germplasmName", all=TRUE)
144 # for random effects: file.BLUPs and file.adjustedBLUPs
145 # for fixed effects: file.BLUEs and file.adjustedBLUEs
147 if (genotypeEffectType=="random") {
148 outfile_blup = paste(datafile, ".BLUPs", sep="");
150 write.table(BLUP, quote=F, sep='\t', row.names=FALSE)
152 outfile_adjmeans = paste(datafile, ".adjustedBLUPs", sep="")
153 sink(outfile_adjmeans)
154 write.table(adjusted_means, quote=F , sep='\t', row.names=FALSE)
158 outfile_blue = paste(datafile, ".BLUEs", sep="")
160 write.table(BLUE, quote=F , sep='\t', row.names=FALSE)
162 outfile_adjblues = paste(datafile, ".adjustedBLUEs", sep="")
163 sink(outfile_adjblues)
164 write.table(adjustedBLUE, quote=F , sep='\t', row.names=FALSE)