autogenerated name metadata table
[sgn.git] / R / DRRC.r
bloba19b7249a1a40192572099e81309ab66db6b69ae
2 args=commandArgs(TRUE)
4 ##args is now a list of character vectors
5 ## First check to see if arguments are passed.
6 ## Then cycle through each element of the list and evaluate the expressions.
8 if(length(args)==0){
9 print("No arguments supplied.")
10 ##supply default values
11 paramfile=''
12 } else {
13 for(i in 1:length(args)){
14 print(paste("Processing arg ", args[[i]]));
15 eval(parse(text=args[[i]]))
19 library(reshape2)
20 library(dplyr)
21 library(blocksdesign)
23 source(paramfile)
24 ## 1) Preparing dataframe
25 all.clones <- treatments
26 nTrt <- length(all.clones)
27 nRep <- nRep
28 nRows <- nRow
29 nCols <- nCol
30 # nCols <- nTrt*nRep/nRows
31 rowsPerBlock <- nTrt/nCols
32 colsPerBlock <- nTrt/nRows
33 superCols <- nCols/colsPerBlock
34 totalPlots <- nTrt*nRep
36 plot_type <- plot_type
37 plot_start <- plot_start
39 blocks = data.frame(block_number = gl(nRep,nTrt),
40 Cols = gl(superCols,colsPerBlock,totalPlots),
41 row_number = gl(nRows,nCols,totalPlots),
42 col_number = gl(nCols,1,totalPlots))
44 ## Setting rep number orthogonal to block number
45 rep_number = as.numeric(blocks$Cols)
48 # treatments = data.frame(treatments =gl(nTrt,1,totalPlots))
49 Z=design(all.clones,blocks, searches = 50, weighting=0.5)
50 fieldBook <- Z$Design
52 trialMatrix <- matrix(0,nRows,nCols)
54 for(i in 1:nrow(fieldBook)){
55 trialMatrix[fieldBook$subRows[i],fieldBook$subCols[i]]<-fieldBook$treatments[i]
57 trialMatrix
59 ## Adding plot number
60 colnames(fieldBook)[5] <- "plot_number"
62 fieldBook$block_number <- as.integer(fieldBook$block_number)
63 fieldBook$row_number <- as.integer(fieldBook$row_number)
64 fieldBook$col_number <- as.integer(fieldBook$col_number)
66 # Load dplyr
67 library(dplyr)
69 # Arrange fieldBook by row_number and col_number
70 fieldBook <- fieldBook %>% arrange(row_number, col_number)
71 fieldBook$plot_number <- c(1:totalPlots)
72 fieldBook$plot_id <- c(1:nTrt)
76 ## Number start
77 ## 00101 will be added for NCSU
78 if(plot_start == "00101"){
79 fieldBook$plot_number = paste0(formatC(fieldBook$block_number,width=3,flag="0"),
80 formatC(fieldBook$plot_id,width=2,flag="0"))
81 }else if (plot_start == 1001){
82 fieldBook$plot_number <- (1000*fieldBook$block_number)+fieldBook$plot_id
83 }else if (plot_start == 101) {
84 fieldBook$plot_number <- (100*fieldBook$block_number)+fieldBook$plot_id
87 cat("plot start is ", plot_start,"\n")
88 cat("plot type is ", plot_type,"\n")
90 plot_type = "serpentine"
91 ## Plot number format
92 if(plot_type == "serpentine"){
93 for(i in 1:nRows){
94 if(i%%2==0){
95 fieldBook[fieldBook$row_number == i, "plot_number"] <- rev(fieldBook[fieldBook$row_number==i,"plot_number"])
100 fieldBook$rep_number <- rep_number
103 #### create is_a_control
104 names(fieldBook)[names(fieldBook) == "treatments"] <- "accession_name"
105 fieldBook <- transform(fieldBook, is_a_control = ifelse(fieldBook$accession_name %in% controls, 1, 0))
107 design <- fieldBook %>% dplyr::select(block_number, rep_number, row_number, col_number, plot_number, accession_name, is_a_control)
109 head(design)
111 # save result files
112 basefile <- tools::file_path_sans_ext(paramfile)
113 outfile = paste(basefile, ".design", sep="");
114 sink(outfile)
115 write.table(design, quote=F, sep='\t', row.names=FALSE)
116 sink();