/
41b_ABAOVRmulticlass.py
259 lines (202 loc) · 9.29 KB
/
41b_ABAOVRmulticlass.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Use a one v. rest approach to create a multiclass classifier from LASSO
Shaina Lu
Zador & Gillis Labs
April 2021
"""
from __future__ import division
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import scipy as sp
from scipy import stats
from sklearn import metrics
from sklearn.linear_model import Lasso
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split #stratify train/test split
import random
#file paths
ALLEN_FILT_PATH = "/home/slu/spatial/data/ABAISH_filt_v6_avgdup.h5"
ONTOLOGY_PATH = "/data/slu/allen_adult_mouse_ISH/ontologyABA.csv"
#ST_CANTIN_FILT_PATH = "/home/slu/spatial/data/cantin_ST_filt_v2.h5"
######################################################################################
def read_ABAdata():
"""read in all ABA datasets needed using pandas"""
metabrain = pd.read_hdf(ALLEN_FILT_PATH, key='metabrain', mode='r')
voxbrain = pd.read_hdf(ALLEN_FILT_PATH, key='avgvoxbrain', mode='r')
propontvox = pd.read_hdf(ALLEN_FILT_PATH, key='propontology', mode='r')
#geneIDName = pd.read_hdf(ALLEN_FILT_PATH, key='geneIDName', mode='r')
return metabrain, voxbrain, propontvox
#ST
def read_STdata():
"""read in all ST datasets needed using pandas"""
STspotsmeta = pd.read_hdf(ST_CANTIN_FILT_PATH, key='STspotsmeta', mode='r')
STspots = pd.read_hdf(ST_CANTIN_FILT_PATH, key='STspots', mode='r')
STpropont = pd.read_hdf(ST_CANTIN_FILT_PATH, key='STpropont', mode='r')
return STspotsmeta, STspots, STpropont
def read_ontology():
ontology = pd.read_csv(ONTOLOGY_PATH)
ontology = ontology.drop([ontology.columns[5], ontology.columns[6]], axis=1)
ontology = ontology.fillna(-1) #make root's parent -1
return ontology
def filterproponto(sampleonto):
"""pre-processing for propogated ontology"""
#remove brain areas that don't have any samples
sampleonto_sums = sampleonto.apply(lambda col: col.sum(), axis=0)
sampleonto = sampleonto.loc[:,sampleonto_sums > 5] #greater than 5 becuase less is not enough for train/test split to have non-zero areas
return sampleonto
def getleaves(propontvox, ontology):
"""helper function to get only leaf brain areas"""
#leaves are brain areas in the ontology that never show up in the parent column
allareas = list(propontvox)
parents = list(ontology.parent)
for i in range(len(parents)): #convert parents from float to int, ids are ints
parents[i] = int(parents[i])
#remove parents from all areas
leaves = []
for area in allareas:
if int(area) not in parents:
leaves.append(area)
print("number of leaf areas: %d" %len(leaves))
return leaves
def findoverlapareas(STonto, propontvox, ontology):
"""find leaf brain areas overlapping between the two datasets"""
leafST = getleaves(STonto, ontology)
leafABA = getleaves(propontvox, ontology)
leafboth = []
for i in range(len(leafABA)):
if leafABA[i] in leafST:
leafboth.append(leafABA[i])
STonto = STonto.loc[:,leafboth]
propontvox = propontvox.loc[:,leafboth]
return STonto, propontvox
def zscore(voxbrain):
"""zscore voxbrain or subsets of voxbrain (rows: voxels, cols: genes)"""
#z-score on whole data set before splitting into test and train
scaler = StandardScaler(copy=True, with_mean=True, with_std=True)
scaler.fit(voxbrain)
z_voxbrain = scaler.transform(voxbrain)
#store z-scored voxbrain as pandas dataframe
z_voxbrain = pd.DataFrame(z_voxbrain)
z_voxbrain.columns = voxbrain.columns
z_voxbrain.index = voxbrain.index
return z_voxbrain
def analytical_auroc(featurevector, binarylabels):
"""analytical calculation of auroc
inputs: feature (mean rank of expression level), binary label (ctxnotctx)
returns: auroc
"""
#sort ctxnotctx binary labels by mean rank, aescending
s = sorted(zip(featurevector, binarylabels))
feature_sort, binarylabels_sort = map(list, zip(*s))
#get the sum of the ranks in feature vector corresponding to 1's in binary vector
sumranks = 0
for i in range(len(binarylabels_sort)):
if binarylabels_sort[i] == 1:
sumranks = sumranks + feature_sort[i]
poslabels = binarylabels.sum()
neglabels = (len(binarylabels) - poslabels)
auroc = ((sumranks/(neglabels*poslabels)) - ((poslabels+1)/(2*neglabels)))
return auroc
def getoverlapgenes(STspots, ABAvox):
ABAgenes = list(ABAvox)
STgenes = list(STspots)
#get overlapping genes
overlap = []
for i in range(len(ABAgenes)):
if ABAgenes[i] in STgenes:
overlap.append(ABAgenes[i])
print("number of overlapping genes: %d" %len(overlap))
#index datasets to keep only genes that are overlapping
STspots = STspots.loc[:,overlap]
ABAvox = ABAvox.loc[:,overlap]
return STspots, ABAvox
######################################################################################
def applyLASSO(Xtrain, Xtest, ytrain, ytest):
"""apply LASSO regression"""
#lasso_reg = Lasso(alpha=0.01, max_iter=10000)
model = LinearRegression()
model.fit(Xtrain, ytrain)
#train
predictions_train = model.predict(Xtrain)
#auroc_train = analytical_auroc(sp.stats.mstats.rankdata(predictions_train), ytrain)
#test
predictions_test = model.predict(Xtest)
#auroc_test = analytical_auroc(sp.stats.mstats.rankdata(predictions_test), ytest)
return predictions_train, predictions_test
def allbyall(mod_data,mod_propont):
trainpreds = pd.DataFrame(columns=list(mod_propont))
testpreds = pd.DataFrame(columns=list(mod_propont))
areas = list(mod_propont)
#for each column, brain area
for i in range(420, mod_propont.shape[1]):
print("col %d" %i)
area1 = areas[i]
#get binary label vectors
ylabels = mod_propont.loc[:, area1]
#split train test for X data and y labels
#split data function is seeded so all will split the same way
Xtrain, Xtest, ytrain, ytest = train_test_split(mod_data, ylabels, test_size=0.5,\
random_state=42, shuffle=True,\
stratify=ylabels)
#z-score train and test folds
zXtrain = zscore(Xtrain)
zXtest = zscore(Xtest)
#fit LASSO on train set for 1 v all
currpred_train,currpred_test = applyLASSO(zXtrain, zXtest, ytrain, ytest)
#key = "%s,%s" %(str(i))
trainpreds[area1] = currpred_train
testpreds[area1] = currpred_test
#break
if i%10 == 0:
trainpreds.to_csv("ABAtrainpreds_040821_nrpart4.csv", sep=',',header=True,index=False)
testpreds.to_csv("ABAtestpreds_040821_nrpart4.csv", sep=',',header=True,index=False)
return trainpreds,testpreds
def getaurocs(mod_data,mod_propont,trainpreds,testpreds):
print("STARTING AUROCS")
#get aurocs
trainauroc = {}
testauroc = {}
areas = list(mod_propont)
#for each column, brain area
for i in range(mod_propont.shape[1]):
if i %10 == 0:
print("col %d" %i)
area1 = areas[i]
#get binary label vectors
ylabels = mod_propont.loc[:, area1]
#split train test for X data and y labels
#split data function is seeded so all will split the same way
Xtrain, Xtest, ytrain, ytest = train_test_split(mod_data, ylabels, test_size=0.5,\
random_state=42, shuffle=True,\
stratify=ylabels)
#get auroc of new prediction vector
trainauroc[area1] = analytical_auroc(sp.stats.mstats.rankdata(trainranks[area1]), ytrain)
testauroc[area1] = analytical_auroc(sp.stats.mstats.rankdata(testranks[area1]), ytest)
return trainauroc,testauroc
######################################################################################
def main():
#read in data
ontology = read_ontology()
ABAmeta, ABAvox, ABApropont = read_ABAdata()
#pre-processing
ABApropont = filterproponto(ABApropont)
#get leaf areas only
leaves = getleaves(ABApropont,ontology)
ABApropont = ABApropont.loc[ABAmeta.ids.isin(leaves),leaves] #subset propontvox for leaf areas
ABAvox = ABAvox.loc[ABAmeta.ids.isin(leaves),:] #subset voxbrain for voxels from leaves
trainpreds,testpreds = allbyall(ABAvox, ABApropont)
trainpreds.to_csv("ABAtrainpreds_040821_nrpart4.csv", sep=',',header=True,index=False)
testpreds.to_csv("ABAtestpreds_040821_nrpart4.csv", sep=',',header=True,index=False)
#rank rows
#trainranks = sp.stats.mstats.rankdata(trainpreds.to_numpy(),axis=1)
#testranks = sp.stats.mstats.rankdata(testpreds.to_numpy(),axis=1)
#convert back to pandas
#trainranks = pd.DataFrame(trainranks, index=trainpreds.index, columns=trainpreds.columns)
#testranks = pd.DataFrame(testranks, index=testpreds.index, columns=testpreds.columns)
#trainauroc,testauroc = getaurocs(leafvoxbrain, leafpropontvox, trainpreds, testpreds)
#np.save("ABAtrainauroc_040521.npy", trainauroc)
#np.save("ABAtestauroc_040521.npy", testauroc)
main()