-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_script.py
195 lines (161 loc) · 7.15 KB
/
build_script.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
'''
Build script moved to python for better extendability and interoperability.
'''
import os
import subprocess
import pandas as pd
import argparse
def collectFiles(data_dir='https://raw.githubusercontent.com/PNNL-CompBio/srpAnalytics/main/data',filename='srp_build_files.csv'):
'''
every time the build file is updated, this script will collect the files and return
a dictionary of files to be fed into each module
'''
df = pd.read_csv(data_dir+'/'+filename)
return df
def fitCurveFiles(morpho_behavior_tuples):
'''
get new curve fits, list of tuples of morpho/behavior pairs
'''
def combineFiles(location_list,ftype):
'''
helper function to combine duplicates
'''
dflist=[]
required_columns = {'bmd':['Chemical_ID','End_Point','Model','BMD10','BMD50',"Min_Dose","Max_Dose",\
"AUC_Norm","DataQC_Flag","BMD_Analysis_Flag"],#,"BMD10_Flag","BMD50_Flag{"),
'dose':['Chemical_ID',"End_Point","Dose","Response","CI_Lo","CI_Hi"],\
'fit':['Chemical_ID',"End_Point","X_vals","Y_vals"]}
print('concatenating '+ftype)
for loc in location_list.location:
f = pd.read_csv(loc)[required_columns[ftype]]
dflist.append(f)
fulldf=pd.concat(dflist)
fulldf = fulldf.drop_duplicates()
return fulldf.drop_duplicates()
def runSampMap(is_sample=False,drcfiles=[],smap='',cid='',\
emap='',cclass='',ctfile='',fses='',desfile=''):
'''
run sample mapping
'''
if is_sample:
cmd = "Rscript sampleChemMapping/mapSamplesToChems.R --sample --drcFiles="+','.join(drcfiles)+\
' --sampId='+smap+' --chemId='+cid+' --epMap='+emap+' --chemClass='+cclass+\
' --compToxFile='+ctfile+' --sampleFiles='+fses+' --chemDesc='+desfile+\
' --sampMap='+smap
elif len(drcfiles)>0:
cmd = "Rscript sampleChemMapping/mapSamplesToChems.R --chemical --drcFiles="+','.join(drcfiles)+\
' --sampId='+smap+' --chemId='+cid+' --epMap='+emap+' --chemClass='+cclass+\
' --compToxFile='+ctfile+' --sampleFiles='+fses+' --chemDesc='+desfile+\
' --sampMap='+smap
else:
cmd = "Rscript sampleChemMapping/mapSamplesToChems.R --sampId="+smap+' --chemId='+cid+\
' --epMap='+emap+' --chemClass='+cclass+\
' --compToxFile='+ctfile+' --sampleFiles='+fses+' --chemDesc='+desfile+\
' --sampMap='+smap
print(cmd)
os.system(cmd)
print('ls -la .')
##now we validate the files that came out.
dblist=['/tmp/samples.csv','/tmp/chemicals.csv','/tmp/sampleToChemicals.csv']
for ftype in ['XYCoords.csv','DoseResponse.csv','BMDs.csv']:
dblist.append('/tmp/zebrafishChem'+ftype)
dblist.append('/tmp/zebrafishSamp'+ftype)
return(dblist)
#runSchemaCheck(dblist)
def runExposome(chem_id_file):
'''
run exposome data pull
'''
cmd = 'Rscript exposome/exposome_summary_stats.R '+chem_id_file
print(cmd)
os.system(cmd)
return(['/tmp/exposomeGeneStats.csv'])
def runExpression(gex,chem,ginfo):
'''
run expression parsing
'''
cmd = 'Rscript zfExp/parseGexData.R '+gex+' '+chem+' '+ginfo
print(cmd)
os.system(cmd)
return(['/tmp/srpDEGPathways.csv','/tmp/srpDEGStats.csv','/tmp/allGeneEx.csv'])
def runSchemaCheck(dbfiles=[]):
'''
run schema checking
'''
##TODO: make this work with internal calls
for filename in dbfiles:
classname = os.path.basename(filename).split('.')[0]
cmd = 'linkml-validate --schema srpAnalytics.yaml '+filename+' --target-class '+classname
print(cmd)
os.system(cmd)
def main():
'''
this wrapping script is placed into every docker image to pull the files
from the repo and initiate the appropriate call to the underlying code.
'''
df = collectFiles()
####
# file parsing - collects all files we might need for the tool below
####
##first find the morphology and behavior pairs for chemical sources
chemdf = df.loc[df.sample_type=='chemical']
morph = chemdf.loc[chemdf.data_type=='morphology']
beh = chemdf.loc[chemdf.data_type=='behavior']
tupes =[]
for n in morph.name:
tupes.append([morph.loc[morph.name==n].location,beh.loc[beh.name==n].location])
##now map sample information
sid = list(df.loc[df.name=='sampId'].location)[0]
cid = list(df.loc[df.name=='chemId'].location)[0]
cclass = list(df.loc[df.name=='class1'].location)[0]
emap = list(df.loc[df.name=='endpointMap'].location)[0]
fses = ','.join(list(df.loc[df.data_type=='sample'].location))
ctfile = list(df.loc[df.name=='compTox'].location)[0]
desfile = list(df.loc[df.name=='chemdesc'].location)[0]
smap = list(df.loc[df.name=='sampMap'].location)[0]
gex1 = ','.join(list(df.loc[df.data_type=='expression'].location))
ginfo = list(df.loc[df.name=='geneInfo'].location)[0]
###now we can call individiual commands
parser = argparse.ArgumentParser('Pull files from github list of files and call appropriate command')
parser.add_argument('--bmd', dest='bmd',action='store_true', default=False, help='Re-run benchmark dose calculation and dependent commands')
parser.add_argument('--samps', dest='samps', action='store_true', default=False, help='Re run sample-chem mapping')
parser.add_argument('--expo', dest='expo', action='store_true', default=False, help='Re run exposome sample collection')
parser.add_argument('--geneEx', dest='geneEx', action='store_true', default=False, help='Re run gene expression generation')
args = parser.parse_args()
##call bmdrc on all morphology/behavior pairs for sample sources
if args.bmd:
print("Re-running benchmark dose collection")
newbmds,newfits,newdoses =[],[],[]
fitCurveFilesls()
if args.bmd or args.samps: ### need to rerun samples if we have created new bmds
#add chemical BMDS, fits, curves to existing data
chemfiles=[]
sampfiles=[]
#print(fses)
for st in ['chemical','extract']:
for dt in ['bmd','fit','dose']:
fdf = combineFiles(df.loc[df.sample_type==st].loc[df.data_type==dt],dt)
fname = '/tmp/tmp_'+st+'_'+dt+'.csv'
fdf.to_csv(fname,index=False)
if st=='chemical':
chemfiles.append(fname)
else:
sampfiles.append(fname)
res1=runSampMap(True,sampfiles,smap,cid,emap,cclass,ctfile,fses,desfile)
res2=runSampMap(False,chemfiles,smap,cid,emap,cclass,ctfile,fses,desfile)
res3=runSampMap(False,[],smap,cid,emap,cclass,ctfile,fses,desfile)
res = res1+res2+res3
res = list(set(res))
for f in sampfiles+chemfiles:
os.system('rm '+f)
##now we run validation
runSchemaCheck(res)
if args.expo:
res=runExposome(cid)
runSchemaCheck(res)
if args.geneEx:
if not os.path.exists("/tmp/chemicals.csv"):
runSampMap(False,[],smap,cid,emap,cclass,ctfile,fses,desfile)
res=runExpression(gex1,'/tmp/chemicals.csv',ginfo)
runSchemaCheck(res)
main()