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);
36 pd = read.csv(datafile, sep="\t")
38 source(paramfile) # should give us dependent_variable and the model
40 trait = dependent_variables
41 pd$studyYear = as.factor(pd$studyYear)
42 print(paste("FIXED MODEL :", fixed_model))
43 print(paste("RANDOM MODEL: ", random_model))
44 print(paste("FIXED FACTORS: ", fixed_factors))
45 print(paste("RANDOM FACTORS: ", random_factors))
48 BLUE = as.data.frame(unique(pd$germplasmName))
49 colnames(BLUE) = "germplasmName"
55 for(i in 1:length(trait)){
58 #print(paste("processing trait", trait[i]))
59 dependent_variables = trait[i]
60 dependent_variables = gsub(" ", "\\.", dependent_variables) # replace space with "." in variable name
61 dependent_variables = gsub("\\|", "\\.", dependent_variables) # replace | with .
62 dependent_variables = gsub("\\:", "\\.", dependent_variables)
63 dependent_variables = gsub("\\-", "\\.", dependent_variables)
64 dependent_variables = gsub("\\/", "\\.", dependent_variables)
66 print(paste("Dependent variables : ", dependent_variables))
68 pd <- pd[!(is.na(pd[c(dependent_variables)])), ]
72 genotypeEffectType = as.vector(str_match(random_model, 'germplasmName'))
73 genotypeEffectType = ifelse(is.na(genotypeEffectType), 'fixed', 'random')
74 print(paste('modeling genotypes as: ', genotypeEffectType))
76 if (genotypeEffectType=="random") {
78 mixmodel = mmer(as.formula(fixed_model), random = as.formula(random_model), rcov = ~ units, data=pd)
79 print(paste("MIXED MODEL: ", mixmodel))
80 varcomp<- summary(mixmodel)$varcomp
88 res <- randef(mixmodel) ##obtain the blups
89 BLUP <- as.data.frame(res)<
93 p0 = predict.mmer(mixmodel, D="germplasmName") ##runs the prediction
95 adj = p0$pvals ##obtains the predictions
96 adjusted_means = as.data.frame(adj)
97 #print(paste("adj", adj))
102 if (random_model!="") {
103 mixmodel = mmer(as.formula(fixed_model), random = as.formula(random_model), rcov = ~ units, data=pd)
105 mixmodel = mmer(as.formula(fixed_model), rcov = ~ units, data=pd)
109 varcomp<-summary(mixmodel)$varcomp
110 print(paste("MIXED MODEL: ", mixmodel))
112 #Computing fixed effects
113 blue = summary(mixmodel)$beta
114 BLUE<-as.data.frame(blue)
116 #BLUE = merge(x = BLUE, y = fixedeff, by="germplasmName", all=TRUE)
118 # compute adjusted blues
119 p0 = predict.mmer(mixmodel, D="germplasmName") ##runs the prediction
121 adj = p0$pvals ##obtains the predictions
122 adjustedBLUE = as.data.frame(adj)
123 #print(paste("adj", adj))
131 # for random effects: file.BLUPs and file.adjustedBLUPs
132 # for fixed effects: file.BLUEs and file.adjustedBLUEs
134 if (genotypeEffectType=="random") {
135 outfile_blup = paste(datafile, ".BLUPs", sep="");
137 write.table(BLUP, quote=F, sep='\t', row.names=FALSE)
139 outfile_adjmeans = paste(datafile, ".adjustedBLUPs", sep="")
140 sink(outfile_adjmeans)
141 write.table(adjusted_means, quote=F , sep='\t', row.names=FALSE)
145 outfile_blue = paste(datafile, ".BLUEs", sep="")
147 write.table(BLUE, quote=F , sep='\t', row.names=FALSE)
149 outfile_adjblues = paste(datafile, ".adjustedBLUEs", sep="")
150 sink(outfile_adjblues)
151 write.table(adjustedBLUE, quote=F , sep='\t', row.names=FALSE)