-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.py
203 lines (143 loc) · 5.76 KB
/
bootstrap.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/python3
#
# bootstrap.py
# Author: Noah Dove
# Usage: bootstrap.py outName run runs...
#
# outName specifies the name of the ouput subdirectory.
# Each run should be in the format <tissue>Sig<sig> where <tissue> is a 2- to 4-character TCGA
# tumor type abbreviation (e.g. paad for Pancreatic Adenocarcinoma) and <sig> identifies a
# genomic mutation signature, e.g. 6
#
# reads from:
# ../data/sig_data.csv
# ../data/all_RNAseq.data
# ../data/cancer_barcodes.csv
# ../data/cancer_abbrevs.table
#
# writes to:
# ../out/outName/<runs>
# ../data/subsets/<tissue>.csv (when generating new cache files)
import os
import gc
import sys
import time
import numpy as np
import pandas as pd
from numpy import random as nprand
from sklearn.linear_model import LogisticRegressionCV
from tcgacorpus import TCGATissueCorpus
# Prevent a seemingly unavoidable FutureWarning from LogisticRegressionCV from clogging the output
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
outDir = '../out/'
def timePrint(*args, **kwargs):
print(time.ctime(), *args, **kwargs)
class Run:
def __init__(self, run, **kwargs):
self.name = run
self.tissue, self.signature = self.name.split("Sig")
self.signature = "Signature {}".format(self.signature)
kwargs = kwargs.copy()
self.nBs = kwargs.pop('nBs', 1000)
self.nPermute = kwargs.pop('nPermute', 50)
self.lrcvParams = kwargs.pop('lrcvParams', {})
if kwargs:
raise ValueError("Unexpected keyword argument(s): " + ', '.join(kwargs.keys()))
def loadFromCorpus(self, corpus):
self.genes = corpus.genes
self.features, self.labels= corpus.getData(self.signature)
self.labels = self.labels > 0
def regress(self, permute=False, sampleFrac=None):
X = self.features
y = self.labels
if sampleFrac is not None and sampleFrac < 1:
posIdx = np.flatnonzero(y)
negIdx = np.flatnonzero(np.logical_not(y))
subsample = np.concatenate(
[nprand.choice(group, round(sampleFrac*len(group))) for group in (posIdx, negIdx)])
X, y = X.iloc[subsample], y.iloc[subsample]
if permute:
nprand.shuffle(y)
lrcv = LogisticRegressionCV(**self.lrcvParams)
lrcv.fit(X, y)
return lrcv.score(X, y), lrcv.coef_
def randomLabelTest(self):
permute = {'test': False, 'control': True}
aucs = pd.DataFrame(columns=permute.keys(), index=pd.RangeIndex(self.nPermute))
for i in range(self.nPermute):
for test in permute:
auc, _ = self.regress(permute=permute[test])
aucs.loc[i, test] = auc
return aucs
class _CoefMatBS:
def __init__(self, genes, nBs):
self.data = pd.DataFrame(columns=genes, index=pd.RangeIndex(nBs))
self.index = 0
def incorporate(self, coefs):
self.data.iloc[self.index] = coefs
self.index += 1
class _SignCountBS:
signs = {'npos':1, 'nneg':-1, 'nz':0}
def __init__(self, genes):
self.data = pd.DataFrame(0.0, columns=_SignCountBS.signs.keys(), index=genes)
def incorporate(self, i, coefs):
coefSigns = np.sign(coefs)
for sign in _SignCountBS.signs:
self.data[sign] += (coefSigns == _SignCountBS.signs[sign])
def bootstrap(self, format='signcounts'):
if format == 'coefmat':
bs = Run._CoefMatBS(self.genes, self.nBs)
elif format == 'signcounts':
bs = Run._SignCountBS(self.genes)
else:
raise ValueError("Unrecognized result format: " + format)
for i in range(self.nBs):
_, coefs = self.regress(sampleFrac=0.8)
bs.incorporate(coefs)
return bs.data
def significantPredictors(self):
coefSignCounts = self.bootstrap('signcounts')
class MultiRun:
def __init__(self, runs, outName, runParams={}):
self.runs = [Run(run, **runParams) for run in runs]
self.runs.sort(key=lambda run: run.tissue)
self.outName = outName
os.mkdir('/'.join([outDir, self.outName]))
def runAll(self, doControl=True, doBootstrap=True, doSigPreds=True):
tissue = None
for run in self.runs:
timePrint("Starting", run.name)
outPrefix = '/'.join([outDir, self.outName, run.name])
os.mkdir(outPrefix)
if run.tissue != tissue:
timePrint("Loading tissue corpus:", run.tissue)
corpus = TCGATissueCorpus(run.tissue)
timePrint("Trying to clear up memory.")
gc.collect()
run.loadFromCorpus(corpus)
if doControl:
timePrint("Running negative control.")
run.randomLabelTest().to_csv('/'.join([outPrefix, 'auctest.csv']))
if doBootstrap:
timePrint("Running bootstrap.")
run.bootstrap('coefmat').to_csv('/'.join([outPrefix, 'coefmat.csv']))
timePrint("Trying to clear up memory.")
gc.collect()
timePrint("Finished", run.name)
def main():
timePrint("Script started.")
runs = None
try:
execName, outName, *runs= sys.argv
except:
outName = ''
print("Output subdir:", outName)
if not runs:
print("No runs specificied.")
sys.exit(1)
allRuns = MultiRun(runs, outName, dict(nBs=5, nPermute=20, lrcvParams=dict(cv=5, scoring='roc_auc', solver='liblinear', penalty='l1')))
allRuns.runAll()
timePrint("Script finished.")
if __name__ == "__main__":
main()