-
Notifications
You must be signed in to change notification settings - Fork 64
/
phenomeScan.r
156 lines (121 loc) · 6.53 KB
/
phenomeScan.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# The MIT License (MIT)
# Copyright (c) 2017 Louise AC Millard, MRC Integrative Epidemiology Unit, University of Bristol
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction, including without
# limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions
# of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
##
## main phenome scan file
library("optparse")
option_list = list(
make_option(c("-f", "--phenofile"), type="character", default=NULL, help="phenotype dataset file name", metavar="character"),
make_option(c("-g", "--traitofinterestfile"), type="character", default=NULL, help="trait of interest dataset file name", metavar="character"),
make_option(c("-v", "--variablelistfile"), type="character", default=NULL, help="variablelistfile file name (should be tab separated)", metavar="character"),
make_option(c("-d", "--datacodingfile"), type="character", default=NULL, help="datacodingfile file name (should be comma separated)", metavar="character"),
make_option(c("-e", "--traitofinterest"), type="character", default=NULL, help="traitofinterest option should specify trait of interest variable name", metavar="character"),
make_option(c("-r", "--resDir"), type="character", default=NULL, help="resDir option should specify directory where results files should be stored", metavar="character"),
make_option(c("-u", "--userId"), type="character", default="userId", help="userId option should specify user ID column in trait of interest and phenotype files [default= %default]", metavar="character"),
make_option(c("-t", "--test"), action="store_true", default=FALSE, help="run test phenome scan on test data (see test subfolder) [default= %default]"),
make_option(c("-s", "--sensitivity"), action="store_true", default=FALSE, help="run sensitivity phenome scan [default= %default]"),
make_option(c("-a", "--partIdx"), type="integer", default=NULL, help="part index of phenotype (used to parellise)"),
make_option(c("-b", "--numParts"), type="integer", default=NULL, help="number of phenotype parts (used to parellise)"),
make_option(c("-j", "--genetic"), action="store", default=TRUE, help="trait of interest is genetic, e.g. a SNP or genetic risk score [default= %default]"),
make_option(c("-z", "--save"), action="store_true", default=FALSE, help="Save generated phenotypes to a file rather than testing associations [default= %default]")
);
opt_parser = OptionParser(option_list=option_list);
opt = parse_args(opt_parser);
source("processArgs.r")
processArgs();
source("initFunctions.r")
loadSource();
## load the files we write to and use
counters=initCounters();
if (opt$save==FALSE) {
initResultsFiles();
}
vl=initVariableLists();
## load data
d <- loadData()
data=d$datax
confounders=d$confounders
indicatorFields=d$inds
numPreceedingCols = ncol(confounders)-1+2; # confounders,minus id column, plus trait of interest and user ID
phenoStartIdx = numPreceedingCols+1;
print("LOADING DONE")
phenoVars=colnames(data);
# remove user id and age and sex columns
phenoVars = phenoVars[-c(1,2)]; # first and second columns are the id and snpScore, respectively, as determined in loadData.r
currentVar="";
currentVarShort="";
first=TRUE;
if (opt$save == TRUE) {
derivedBinary <- data.frame(userID=data$userID)
derivedCont <- data.frame(userID=data$userID)
derivedCatOrd <- data.frame(userID=data$userID)
derivedCatUnord <- data.frame(userID=data$userID)
resLogFile = paste(opt$resDir,"data-log-",opt$varTypeArg,".txt",sep="")
sink(resLogFile)
} else {
modelFitLogFile = paste(opt$resDir,"modelfit-log-",opt$varTypeArg,".txt",sep="")
sink(modelFitLogFile)
sink()
resLogFile = paste(opt$resDir,"results-log-",opt$varTypeArg,".txt",sep="")
sink(resLogFile)
}
phenoIdx=0; # zero because then the idx is the position of the previous variable, i.e. the var in currentVar
for (var in phenoVars) {
sink()
# print(var)
sink(resLogFile, append=TRUE)
varx = gsub("^x", "", var);
varx = gsub("_[0-9]+$", "", varx);
varxShort = gsub("^x", "", var);
varxShort = gsub("_[0-9]+_[0-9]+$", "", varxShort);
## test this variable
if (currentVar == varx) {
thisCol = data[,eval(var)]
thisCol = replaceNaN(thisCol)
currentVarValues = cbind.data.frame(currentVarValues, thisCol);
}
else if (currentVarShort == varxShort) {
## different time point of this var so skip
}
else {
## new variable so run test for previous (we have collected all the columns now)
if (first==FALSE) {
thisdata = makeTestDataFrame(data, confounders, currentVarValues)
testAssociations(currentVar, currentVarShort, thisdata)
}
first=FALSE;
## new variable so set values
currentVar = varx;
currentVarShort = varxShort;
currentVarValues = data[,eval(var)]
currentVarValues = replaceNaN(currentVarValues)
}
phenoIdx = phenoIdx + 1;
}
# last variable so test association
thisdata = makeTestDataFrame(data, confounders, currentVarValues)
testAssociations(currentVar, currentVarShort, thisdata)
sink()
# save counters of each path in variable flow
saveCounts()
if (opt$save == TRUE) {
write.table(derivedBinary, file=paste(opt$resDir,"data-binary-",opt$varTypeArg,".txt", sep=""), append=FALSE, quote=FALSE, sep=",", na="", row.names=FALSE, col.names=TRUE);
write.table(derivedCont, file=paste(opt$resDir,"data-cont-",opt$varTypeArg,".txt", sep=""), append=FALSE, quote=FALSE, sep=",", na="", row.names=FALSE, col.names=TRUE);
write.table(derivedCatOrd, file=paste(opt$resDir,"data-catord-",opt$varTypeArg,".txt", sep=""), append=FALSE, quote=FALSE, sep=",", na="", row.names=FALSE, col.names=TRUE);
write.table(derivedCatUnord, file=paste(opt$resDir,"data-catunord-",opt$varTypeArg,".txt", sep=""), append=FALSE, quote=FALSE, sep=",", na="", row.names=FALSE, col.names=TRUE);
}
warnings()