1 # calculates selection index
2 # and ranks genotypes accordingly
3 # Isaak Y Tecle iyt2cornell.edu
13 allArgs
<- commandArgs()
15 inputFiles
<- scan(grep("input_files", allArgs
, value
= TRUE),
18 relWeightsFile
<- grep("rel_weights", inputFiles
, value
= TRUE)
20 outputFiles
<- scan(grep("output_files", allArgs
, value
= TRUE),
23 traitsFiles
<- grep("gebv_files_of_traits", inputFiles
, value
= TRUE)
25 gebvsSelectionIndexFile
<- grep("gebvs_selection_index",
29 selectionIndexFile
<- grep("selection_index_only",
33 inTraitFiles
<- scan(traitsFiles
, what
= "character")
35 traitFilesList
<- strsplit(inTraitFiles
, "\t");
36 traitsTotal
<- length(traitFilesList
)
39 stop("There are no traits with GEBV data.")
40 if (length(relWeightsFile
) == 0)
41 stop("There is no file with relative weights of traits.")
44 relWeights
<- data
.frame(fread(relWeightsFile
, header
= TRUE))
45 rownames(relWeights
) <- relWeights
[, 1]
46 relWeights
[, 1] <- NULL
48 if (is
.null(relWeights
)) {
49 stop('There were no relative weights for all the traits.')
52 combinedRelGebvs
<- c()
54 for (i
in 1:traitsTotal
) {
55 traitFile
<- traitFilesList
[[i
]]
56 traitGEBV
<- data
.frame(fread(traitFile
, header
= TRUE))
57 rownames(traitGEBV
) <- traitGEBV
[, 1]
58 traitGEBV
[, 1] <- NULL
59 traitGEBV
<- traitGEBV
[order(rownames(traitGEBV
)),,drop
=FALSE]
60 trait
<- colnames(traitGEBV
)
62 relWeight
<- relWeights
[trait
, ]
64 if (is
.na(relWeight
) == FALSE && relWeight
!= 0 ) {
66 weightedTraitGEBV
<- apply(traitGEBV
, 1,
67 function(x
) x
*relWeight
)
69 weightedTraitGEBV
<- data
.frame(weightedTraitGEBV
)
70 colnames(weightedTraitGEBV
) <- paste0(trait
, '_weighted')
72 combinedRelGebvs
<- merge(combinedRelGebvs
, weightedTraitGEBV
,
77 rownames(combinedRelGebvs
) <- combinedRelGebvs
[, 1]
78 combinedRelGebvs
[, 1] <- NULL
82 sumRelWeights
<- apply(relWeights
, 2, sum
)
83 sumRelWeights
<- sumRelWeights
[[1]]
85 combinedRelGebvs$Index
<- apply(combinedRelGebvs
, 1, function (x
) sum(x
))
87 combinedRelGebvs
<- combinedRelGebvs
[ with(combinedRelGebvs
,
88 order(-combinedRelGebvs$Index
)
92 combinedRelGebvs
<- round(combinedRelGebvs
, 2)
96 if (!is
.null(combinedRelGebvs
)) {
97 selectionIndex
<- subset(combinedRelGebvs
,
102 if (gebvsSelectionIndexFile
!= 0) {
103 if (!is
.null(combinedRelGebvs
)) {
104 fwrite(combinedRelGebvs
,
105 file
= gebvsSelectionIndexFile
,
113 if (!is
.null(selectionIndexFile
)) {
114 if (!is
.null(selectionIndex
)) {
115 fwrite(selectionIndex
,
116 file
= selectionIndexFile
,
124 q(save
= "no", runLast
= FALSE)