-
Notifications
You must be signed in to change notification settings - Fork 1
/
ezmap.py
290 lines (228 loc) · 11.6 KB
/
ezmap.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# =================================================
# EzMAP
# Version: 1.0
#
# Author: Patrick Czeczko
# Made at: de Koning Lab
# Link: http://lab.jasondk.io
# Github:
#
# Additional documentation can be found on the github page.
# =================================================
__author__ = 'patrickczeczko'
import sys, os, subprocess
import Scripts.arguments as arguments
import Scripts.configOptions as config
import Scripts.fileManager as fileman
import Scripts.ensureScriptPermissions as ensureScriptPermissions
import Scripts.prinseqPrep as prinseq
import Scripts.bowtie2Prep as bowtie2
import Scripts.hisat2Prep as hisat2
import Scripts.samtoolsPrep as samtools
import Scripts.blastprep as blast
import Scripts.emalPrep as emal
import Scripts.finalScript as final
serialSample = False
# Cretaes a txt file of all importnat output files for final report
def outputFileList(files, projdir, projName):
outputfile = open(projdir + '6-FinalResult-' + projName + '/information/' + 'filelist.txt', 'w+')
outputfile.write(
'<table class="table table-striped table-bordered"><thead><tr><td>Filename</td><td>Path</td></tr></thead>')
outputfile.write('<tbody>')
for x in files:
outputfile.write('<tr><td>' + files[x].origFileName + '</td><td>' + files[x].origFilePath.replace('/',
'/') + '</td></tr>')
outputfile.write('</tbody></table>')
def executeDesktopModeScript (pathToScript):
print('Executing Desktop Mode Script...')
os.system('bash '+pathToScript)
# Runs EzMap on a single time point using the SLURM job manager
def runEzMapOnTimePointSLURM(configOptions, startAtStep, inputFileDir, projdir):
startStep = startAtStep
print(inputFileDir)
print(projdir)
if not inputFileDir.endswith("/"):
inputFileDir = inputFileDir + '/'
if not projdir.endswith("/"):
projdir = projdir + '/'
# Get list of FASTQ files to process
origFiles = fileman.getListOfOriginalFiles(inputFileDir, projdir)
# Check if there is at least one file to process
if len(origFiles) < 1:
print('No files found to process!')
print('Exiting...')
return
# Create subdirectories where intermediate files will live
createdSubDir = fileman.createSubFolders(projdir, configOptions['project-name'])
# If sub directories don't exist, don't continue
if createdSubDir == False:
print("Error unable to create intermediate file directories")
return
# Get the project directory to pass to functions that follow
# projdir = list(origFiles.values())[0].projDirectory
fileman.copyReportFiles(projdir, configOptions['project-name'])
# Generate Job script for step 1 and run all jobs
prinseqJobIDS = []
if (startStep == 1):
prinseq.generateSLURMScript(origFiles, projdir, configOptions)
prinseqJobIDS = prinseq.processAllFiles(projdir, configOptions, origFiles)
startStep += 1
# Generate Job script for step 2 and run all jobs
alignerJobIDS = []
if (startStep == 2):
if configOptions['aligner-to-use'] == 'bowtie2':
bowtie2.generateSLURMScirpt(origFiles, projdir, configOptions, prinseqJobIDS)
alignerJobIDS = bowtie2.processAllFiles(projdir, configOptions, origFiles)
elif configOptions['aligner-to-use'] == 'hisat2':
hisat2.generateSLURMScirpt(origFiles, projdir, configOptions, prinseqJobIDS)
alignerJobIDS = hisat2.processAllFiles(projdir, configOptions, origFiles)
startStep += 1
# Generate Job script for step 3 and run all jobs
samtoolsJobIDS = []
if (startStep == 3):
samtools.generateSLURMScript(origFiles, projdir, configOptions, alignerJobIDS)
samtoolsJobIDS = samtools.processAllFiles(projdir, configOptions, origFiles)
startStep += 1
# Generate Job script for step 4 and run all jobs
blastJobIDS = []
if (startStep == 4):
blast.generateSLURMScript(origFiles, projdir, configOptions, samtoolsJobIDS)
blastJobIDS = blast.processAllFiles(projdir, configOptions, origFiles)
startStep += 1
# Generate Job script for step 5 and run all jobs
emalPreJobIDS = []
if (startStep == 5):
emalPre = emal.generatePreScript(origFiles, projdir, configOptions, blastJobIDS)
emalPreJobIDS = emal.processAllFiles(projdir, configOptions, origFiles, 1, emalPre)
startStep += 1
# Generate Job script for step 6 and run all jobs
emalMainJobIDS = []
if (startStep == 6):
emalMain = emal.generateMainScript(origFiles, projdir, configOptions, emalPreJobIDS)
emalMainJobIDS = emal.processAllFiles(projdir, configOptions, origFiles, 2, emalMain)
startStep += 1
# Generate Job script for step 7 and run all jobs
emalPostJobIDS = []
if (startStep == 7):
emalPost = emal.generatePostScript(origFiles, projdir, configOptions, emalMainJobIDS)
emalPostJobIDS = emal.processAllFiles(projdir, configOptions, origFiles, 3, emalPost)
startStep += 1
# Output file list and gather all results throughout the pipeline run and compile into final report
outputFileList(origFiles, projdir, configOptions['project-name'])
if not serialSample:
final.collectPipelineResult(configOptions['project-name'], projdir, configOptions, emalPostJobIDS)
else:
print(configOptions['project-name'] + '-' + os.path.basename(os.path.normpath(projdir)))
final.collectPipelineResult(configOptions['project-name'] + '-' + os.path.basename(os.path.normpath(projdir)),
projdir, configOptions, emalPostJobIDS)
print('\nAll required jobs for ' + inputFileDir + ' have been queued...\n')
# Run EzMap on a single time point in desktop mode
def runEzMapOnTimePointDesktopMode(configOptions, startAtStep, inputFileDir, projdir):
startStep = startAtStep
print(inputFileDir)
print(projdir)
if not inputFileDir.endswith("/"):
inputFileDir = inputFileDir + '/'
if not projdir.endswith("/"):
projdir = projdir + '/'
# Get list of FASTQ files to process
origFiles = fileman.getListOfOriginalFiles(inputFileDir, projdir)
# Check if there is at least one file to process
if len(origFiles) < 1:
print('No files found to process!')
print('Exiting...')
return
# Create subdirectories where intermediate files will live
createdSubDir = fileman.createSubFolders(projdir, configOptions['project-name'])
# If sub directories don't exist, don't continue
if createdSubDir == False:
print("Error unable to create intermediate file directories")
return
# Get the project directory to pass to functions that follow
# projdir = list(origFiles.values())[0].projDirectory
fileman.copyReportFiles(projdir, configOptions['project-name'])
if (startStep == 1):
prinseq.generateSHScript(origFiles, projdir, configOptions)
startStep += 1
# Generate Job script for step 2 and run all jobs
if (startStep == 2):
if configOptions['aligner-to-use'] == 'bowtie2':
bowtie2.generateSHScript(origFiles, projdir, configOptions)
elif configOptions['aligner-to-use'] == 'hisat2':
hisat2.generateSHScript(origFiles, projdir, configOptions)
startStep += 1
# Generate Job script for step 3 and run all jobs
if (startStep == 3):
samtools.generateSHScript(origFiles, projdir, configOptions)
startStep += 1
# Generate Job script for step 4 and run all jobs
if (startStep == 4):
blast.generateSHScript(origFiles, projdir, configOptions)
startStep += 1
# Generate Job script for step 5 and run all jobs
if (startStep == 5):
emalPre = emal.generateSHPreScript(origFiles, projdir, configOptions)
startStep += 1
# Generate Job script for step 6 and run all jobs
if (startStep == 6):
emalMain = emal.generateSHMainScript(origFiles, projdir, configOptions)
startStep += 1
# Generate Job script for step 7 and run all jobs
if (startStep == 7):
emalPost = emal.generateSHPostScript(origFiles, projdir, configOptions)
startStep += 1
executeDesktopModeScript(projdir+'ezmapScript.sh')
# Output file list and gather all results throughout the pipeline run and compile into final report
outputFileList(origFiles, projdir, configOptions['project-name'])
if not serialSample:
final.collectPipelineResultDM(configOptions['project-name'], projdir, configOptions)
else:
print(configOptions['project-name'] + '-' + os.path.basename(os.path.normpath(projdir)))
final.collectPipelineResult(configOptions['project-name'] + '-' + os.path.basename(os.path.normpath(projdir)), projdir, configOptions) # Main Function
if __name__ == "__main__":
# Get all user set arguments before starting pipeline
print("\nEzMap V1.0\n\nParsing config file...")
allArgs = arguments.parseCommandLineArguments()
configOptions = config.parseConfigOptions()
# If a commandline option was set to provide a project name override the one within the config file to the
# commandline project name
if 'projectName' in allArgs:
configOptions['project-name'] = allArgs['projectName']
ensureScriptPermissions.checkForScriptPermisions(configOptions)
startStep = int(configOptions['start-at-step'])
# If running in desktop mode
if allArgs['desktopMode']:
print ('Running EzMap in desktop mode\n')
if allArgs['serialSample'] == True:
serialSample = True
# Get list of directories within the sample directory
for name in os.listdir(allArgs['directory']):
if os.path.isdir(os.path.join(allArgs['directory'], name)):
if not allArgs['directory'].endswith('/'):
allArgs['directory'] = allArgs['directory'] + '/'
if not os.path.exists(allArgs['projDir'] + name):
print(allArgs['projDir'] + name)
os.mkdir(allArgs['projDir'] + name, 0o777)
runEzMapOnTimePointDesktopMode(configOptions, startStep, allArgs['directory'], allArgs['projDir'])
else:
runEzMapOnTimePointDesktopMode(configOptions, startStep, allArgs['directory'], allArgs['projDir'])
else:
if allArgs['serialSample'] == True:
serialSample = True
# Get list of directories within the sample directory
for name in os.listdir(allArgs['directory']):
if os.path.isdir(os.path.join(allArgs['directory'], name)):
if not allArgs['directory'].endswith('/'):
allArgs['directory'] = allArgs['directory'] + '/'
if not os.path.exists(allArgs['projDir'] + name):
print(allArgs['projDir'] + name)
os.mkdir(allArgs['projDir'] + name, 0o777)
runEzMapOnTimePointSLURM(configOptions, startStep, allArgs['directory'] + name,
allArgs['projDir'] + name)
else:
runEzMapOnTimePointSLURM(configOptions, startStep, allArgs['directory'], allArgs['projDir'])
print('All required jobs have been created and queued...')
print('You can now check the status of your jobs using the following command: ')
print('squeue\n')
print('Once those jobs have completed you can view your results in:\n' + allArgs['projDir'] + '6-FinalResult-' +
configOptions['project-name'] + '/')