This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_abridge
executable file
·427 lines (368 loc) · 27.6 KB
/
run_abridge
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#! /usr/bin/env python3
########################################################################################################################################################
# Script for running abridge within docker container
########################################################################################################################################################
import argparse
import logging
import os
import pprint
import sys
import re
import time
import multiprocessing
import random
import glob
import time
import subprocess
from pprint import pformat
from argparse import RawTextHelpFormatter
def parseCommandLineArguments():
parser = argparse.ArgumentParser( prog = "abridge", description = "Compress alignments for storage, decompress from compressed file, view alignments from random locations and generate coverages", formatter_class = RawTextHelpFormatter )
required_named = parser.add_argument_group( 'Required arguments' )
optional_named = parser.add_argument_group( 'Optional arguments' )
# Required arguments
required_named.add_argument( "-o", "--output_directory", help = "Enter the name of the output directory. If nothing is specified then the compressed file will be put in the same location as the input samfile" )
input_group = parser.add_mutually_exclusive_group()
input_group.add_argument( "-isam", "--inputfilename", help = "Enter the name of the alignment file you wish to compress. Alignments in SAM format only is expected. Ensure that the file is sorted by coordinate. Also, files must have the header section with the reference information available. You can compress only one file at a time." )
input_group.add_argument( "-iabr", "--inputabrfilenames", help = "Enter the name of the compressed alignment files you wish to merge. These files must be compressed using abridge. You can decompress only one file at a time." )
required_named.add_argument( "-g", "--genome", help = "Enter a single fasta file for the reference" )
compress_decompress_group = parser.add_mutually_exclusive_group()
compress_decompress_group.add_argument( "-cmp", "--compress", help = "Set this option if you wish to compress the alignment file", action = "store_true" )
compress_decompress_group.add_argument( "-dcmp", "--decompress", help = "Set this option if you wish to decompress the alignment file", action = "store_true" )
compress_decompress_group.add_argument( "-r", "--random", help = "Retrieve alignments from random locations", action = "store_true" )
compress_decompress_group.add_argument( "-H", "--header", help = "Print only the header of reference sequences during decompression", action = "store_true" )
# Optional arguments
# optional_named.add_argument( "-l", "--level", help = "This can accept an integer from the set (1,2,3). If level is set to 1 then abridge will perform the fastest but low compression. abridge will use brotli to compress. Decompression will be fast. Setting level to 2 will prompt abridge to perform the medium level compression using 7z. Compression will take time but decompression will be fast. If level is set to 3 then abridge will perform the best compression using 7paq. Both compression and decompression will take average time to complete", type = int, default = 2 )
optional_named.add_argument( "-ss", "--ignore_alignment_scores", help = "Request abrigde to store the quality scores and the alignment score", action = "store_true" )
optional_named.add_argument( "-igqual", "--ignore_quality_scores", help = "Ignore all quality scores", action = "store_true" )
optional_named.add_argument( "-qual", "--quality", help = "Enter dummy quality scores while decompressing", default = 'I' )
optional_named.add_argument( "-gsc", "--ignore_soft_clippings", help = "No soft clippings will be stored. Read will be trimmed down to only the portion which matched to nucleotides in the reference", action = "store_true" )
optional_named.add_argument( "-gm", "--ignore_mismatches", help = "All mismatches will be ignored", action = "store_true" )
optional_named.add_argument( "-gs", "--ignore_sequence", help = "No nucleotide sequence will be produced during decompression", action = "store_true" )
optional_named.add_argument( "-gu", "--ignore_unmapped_reads", help = "Request abridge to discard all reads that are unmapped", action = "store_true" )
# optional_named.add_argument( "-sq", "--save_all_quality_scores", help = "Request abridge to save all quality scores", action = 'store_true' )
# optional_named.add_argument( "-aq", "--save_exact_quality_scores", help = "Adjust quality scores for matched bases to achieve better encoding. For more details please check ...", action = "store_true" )
optional_named.add_argument( "-q", "--quiet", help = "Prevent abridge from printing any log information. By default logging is enables", action = "store_true" )
optional_named.add_argument( "-n", "--cpu", help = "Enter the number of CPU cores to be used. This option will be used during compression or decompression.", default = 1 )
optional_named.add_argument( "-run_diagnostics", "--run_diagnostics", help = "abridge will run diagnostics on the cigar compression and decompression. It will exit on discovery of any discrepancies", action = "store_true" )
optional_named.add_argument( "-p", "--positions", help = "Enter the position as chromosome:start-end from which reads will be retrieved" )
optional_named.add_argument( "-rp", "--read_prefix", help = "Enter a read prefix for decompression - valid only for random access" )
optional_named.add_argument( "--keep_intermediate_error_files", "-keep_intermediate_error_files", help = "Set this argument if you wish to preserve the intermediate error files to assess time and memory usage. Default behaviour is to delete those", action = "store_true" )
optional_named.add_argument( "--test", "-test", help = "Set this option to test run the software with example data", action = "store_true" )
optional_named.add_argument( "--error_directory", "-edir", help = "Enter a directory where all error files will be stored. If nothing is specified then error files will be stored in the output directory" )
optional_named.add_argument( "--framework", "-fm", help = "Enter your choice of framework", choices = ["docker", "singularity"], default = "docker" )
optional_named.add_argument( "--force", "-force", help = "Setting this argument will remove the output directory and start the computation from scratch", action = "store_true" )
# Suppressed arguments
parser.add_argument( "--logfilename", "-logfilename", help = argparse.SUPPRESS ) # Name of the logfile
parser.add_argument( "--files_for_removal", "-files_for_removal", help = argparse.SUPPRESS ) # Files will be removed later
parser.add_argument( "--softwares", "-softwares", help = argparse.SUPPRESS ) # Software paths
parser.add_argument( "--single_ended", "-single_ended", help = argparse.SUPPRESS )
parser.add_argument( "--reference_to_length", "-num_of_reference_sequences", help = argparse.SUPPRESS )
parser.add_argument( "--outputfilename", "-outputfilena", help = argparse.SUPPRESS )
parser.add_argument( "--compile_programs", "-compile_programs", action = "store_true", help = argparse.SUPPRESS ) # Force abridge to compile the C programs
# Future enhancements
compress_decompress_group.add_argument( "-ov", "--generate_overlapping_coverage", help = argparse.SUPPRESS, action = "store_true" ) # Future - This option can be used in conjuction with --positions to construct coverage from a specific location # help="Enter the name of the compressed file from which you wish to generate an overlapping coverage of reads ",
compress_decompress_group.add_argument( "-nov", "--generate_non_overlapping_coverage", help = argparse.SUPPRESS, action = "store_true" ) # help="Enter the name of the compressed file from which you wish to generate a non-overlapping coverage of reads "
# Options for generating coverage
optional_named.add_argument( "-d", "--d", help = argparse.SUPPRESS, action = "store_true" ) # help = "Report the depth at each position in each A feature. Positions reported are one based. Each position and depth follow the complete A feature.",
optional_named.add_argument( "-bg", "--bg", help = argparse.SUPPRESS, action = "store_true" )
optional_named.add_argument( "-bga", "--bga", help = argparse.SUPPRESS, action = "store_true" )
optional_named.add_argument( "-split", "--split", help = argparse.SUPPRESS, action = "store_true" ) # help = "Treat \"split\" BAM or BED12 entries as distinct BED intervals.",
optional_named.add_argument( "-mem", "--max_memory", help = argparse.SUPPRESS, default = 10 ) # help="Enter the maximum memory allowed (in GB)"
optional_named.add_argument( "-t", "--produce_tags", help = argparse.SUPPRESS, nargs = "*" ) # help="Enter a comma separated list of tags that you want abridge to produce during decompression. By default abridge will generate NH, MD and XS tags."
return parser.parse_args()
def runDockerCommand( name, version, image_location, container_name, volumes, command , cpus = 1, memory = '1g' ):
"""
Runs the command in a docker container
"""
# Runs the main command
docker_cmd = f" docker run "
# docker_cmd += f" -ti "
docker_cmd += f" -v /var/run/docker.sock:/var/run/docker.sock"
docker_cmd += f" -v /usr/bin/docker:/usr/bin/docker --privileged=true "
docker_cmd += f" --rm "
docker_cmd += f" --cpus={cpus}"
docker_cmd += f" --memory='{memory}'"
# docker_cmd += f" --name {container_name}"
for mapping in volumes:
docker_cmd += f" -v {mapping}"
docker_cmd += f" {image_location}:{version} "
docker_cmd += f" bash -c {command}"
os.system( docker_cmd )
# print( docker_cmd )
def runSingularityCommand( name, version, image_location, container_name, volumes, command , cpus = 1, memory = '1g' ):
"""
Runs the command in a Singularity container
"""
# Runs the main command
singularity_cmd = f" singularity exec "
# singularity_cmd += f" --hostname {container_name}"
for mapping in volumes:
singularity_cmd += f" -B {mapping}"
singularity_cmd += f" abridge_{version}.sif "
singularity_cmd += f" bash -c {command}"
os.system( singularity_cmd )
# print( singularity_cmd )
def runDockerOrSingularityInParallel( eachinput ):
compress_cmd, decompress1_cmd, decompress2_cmd, container_name, volumes_list, framework_of_choice = eachinput
if framework_of_choice == "docker":
runDockerCommand( name = "abridge",
version = "1.0.0",
image_location = "sagnikbanerjee15/abridge",
container_name = f"{container_name}_compress",
volumes = volumes_list,
command = compress_cmd,
cpus = "1",
memory = '3g'
)
runDockerCommand( name = "abridge",
version = "1.0.0",
image_location = "sagnikbanerjee15/abridge",
container_name = f"{container_name}_compress",
volumes = volumes_list,
command = decompress1_cmd,
cpus = "1",
memory = '3g'
)
runDockerCommand( name = "abridge",
version = "1.0.0",
image_location = "sagnikbanerjee15/abridge",
container_name = f"{container_name}_compress",
volumes = volumes_list,
command = decompress2_cmd,
cpus = "1",
memory = '3g'
)
elif framework_of_choice == "singularity":
runSingularityCommand( name = "abridge",
version = "1.0.0",
image_location = "sagnikbanerjee15/abridge",
container_name = f"{container_name}_compress",
volumes = volumes_list,
command = compress_cmd,
cpus = "1",
memory = '3g'
)
runSingularityCommand( name = "abridge",
version = "1.0.0",
image_location = "sagnikbanerjee15/abridge",
container_name = f"{container_name}_decompress1",
volumes = volumes_list,
command = decompress1_cmd,
cpus = "1",
memory = '3g'
)
runSingularityCommand( name = "abridge",
version = "1.0.0",
image_location = "sagnikbanerjee15/abridge",
container_name = f"{container_name}_decompress2",
volumes = volumes_list,
command = decompress2_cmd,
cpus = "1",
memory = '3g'
)
def main():
commandLineArg = sys.argv
if len( commandLineArg ) == 1:
print( "Please use the --help option to get usage information" )
options = parseCommandLineArguments()
if options.test == False and ( options.genome is None or options.output_directory is None or ( options.inputfilename is None and options.compress is True ) or ( options.inputabrfilenames is None and options.decompress is True ) ):
print( "You must enter the genome, the samfile to compress and container name" )
return
else:
if options.output_directory is None:
print( "Please enter a valid output directory" )
return
if options.error_directory is None:
options.error_directory = options.output_directory
output_filename = f"{options.error_directory}.output"
error_filename = f"{options.error_directory}.error"
os.system( f"mkdir -p {options.output_directory}" )
os.system( f"which docker > {options.output_directory}/find_docker" )
os.system( f"which singularity > {options.output_directory}/find_singularity" )
docker_installed = 1 if "Command not found" not in open( f"{options.output_directory}/find_docker", "r" ).read() else 0
singularity_installed = 1 if "Command not found" not in open( f"{options.output_directory}/find_singularity", "r" ).read() else 0
if docker_installed == 0 and singularity_installed == 0:
print( "You need to have either docker or singularity installed" )
sys.exit()
framework_of_choice = ""
if options.framework == "docker":
if docker_installed == 1:
framework_of_choice = "docker"
else:
framework_of_choice = "singularity"
if options.framework == "singularity":
if singularity_installed == 1:
framework_of_choice = "singularity"
else:
framework_of_choice = "docker"
if options.force == True:
os.system( f"rm -rf {options.output_directory}" )
os.system( f"mkdir -p {options.output_directory}/singularity_images" )
########################################################################################################################
# Pull down docker or singularity images
########################################################################################################################
list_of_softwares_with_versions = {"samtools":"1.14",
"zpaq":"715",
"fclqc":"latest",
"abridge":"1.1.0"}
if framework_of_choice == "docker":
for software in list_of_softwares_with_versions:
version = list_of_softwares_with_versions[software]
os.system( f"docker pull ghcr.io/sagnikbanerjee15/docker_tools_and_pipelines/{software}:{version}" )
else:
for software in list_of_softwares_with_versions:
version = list_of_softwares_with_versions[software]
os.system( f"singularity pull {options.output_directory}/singularity_images/{software}:{version} docker://ghcr.io/sagnikbanerjee15/docker_tools_and_pipelines/{software}:{version}" )
if options.test == True:
pool = multiprocessing.Pool( processes = int( options.cpu ) )
allinputs = []
# Setting up commands for testing
for level in ["1", "2", "3"]: # 3 iterations
# if level != "3": continue
for inputfilename in ["/software/Abridge/example/SRR13711353_SE_100000.sam", "/software/Abridge/example/SRR13711353_PE_100000.sam"]: # 2 iterations
# if "PE" not in inputfilename:continue
for ignore_alignment_scores in [0, 1]: # 2 iterations
for ignore_quality_scores in [0, 1]: # 2 iterations
for ignore_soft_clippings in [0, 1]: # 2 iterations
for ignore_mismatches in [0, 1]: # 2 iterations
for ignore_unmapped_reads in [0, 1]: # 2 iterations
for save_all_quality_scores in [0, 1]: # 2 iterations
for save_exact_quality_scores in [0, 1]: # 2 iterations
command_set = []
inputfilename_without_location = inputfilename.split( "/" )[-1][:-4]
output_directory_name = f"{options.output_directory}/"
output_directory_name += f"{inputfilename_without_location}_"
output_directory_name += f"compress_level_{level}_"
output_directory_name += f"ignore_alignment_scores_{ignore_alignment_scores}_"
output_directory_name += f"ignore_quality_scores_{ignore_quality_scores}_"
output_directory_name += f"ignore_soft_clippings_{ignore_soft_clippings}_"
output_directory_name += f"ignore_mismatches_{ignore_mismatches}_"
output_directory_name += f"ignore_unmapped_reads_{ignore_unmapped_reads}_"
output_directory_name += f"save_all_quality_scores_{save_all_quality_scores}_"
output_directory_name += f"save_exact_quality_scores_{save_exact_quality_scores}_"
output_directory_name += f"{framework_of_choice}"
os.system( f"mkdir -p {output_directory_name}" )
os.system( f"chmod a+x {output_directory_name}" )
volumes_list = [f"{options.output_directory}:{options.output_directory}"]
cmd = f" \" "
cmd += f" abridge "
cmd += f" --keep_intermediate_error_files "
cmd += f" --compress "
cmd += f" --genome /software/Abridge/example/arath_1.fa "
cmd += f" --inputfilename {inputfilename} "
cmd += f" --output_directory {output_directory_name} "
cmd += f" --level {level} "
if ignore_alignment_scores == 1:
cmd += f" --ignore_alignment_scores "
if ignore_quality_scores == 1:
cmd += f" --ignore_quality_scores "
if ignore_soft_clippings == 1:
cmd += f" --ignore_soft_clippings "
if ignore_mismatches == 1:
cmd += f" --ignore_mismatches "
if ignore_unmapped_reads == 1:
cmd += f" --ignore_unmapped_reads "
if save_all_quality_scores == 1:
cmd += f" --save_all_quality_scores "
if save_exact_quality_scores == 1:
cmd += f" --save_exact_quality_scores "
cmd += f" 1> {output_directory_name}.output "
cmd += f" 2> {output_directory_name}.error "
cmd += f" \" "
command_set.append( cmd )
# print( cmd )
# os.system(cmd)
volumes_list = list( set( volumes_list ) )
output_directory_name_without_location = output_directory_name.split( "/" )[-1]
for ignore_sequence in [0, 1]: # 2 iterations
output_directory_name = f"{options.output_directory}/"
output_directory_name += f"{inputfilename_without_location}_"
output_directory_name += f"decompress_level_{level}_"
output_directory_name += f"ignore_alignment_scores_{ignore_alignment_scores}_"
output_directory_name += f"ignore_quality_scores_{ignore_quality_scores}_"
output_directory_name += f"ignore_soft_clippings_{ignore_soft_clippings}_"
output_directory_name += f"ignore_mismatches_{ignore_mismatches}_"
output_directory_name += f"ignore_unmapped_reads_{ignore_unmapped_reads}_"
output_directory_name += f"save_all_quality_scores_{save_all_quality_scores}_"
output_directory_name += f"save_exact_quality_scores_{save_exact_quality_scores}_"
output_directory_name += f"{framework_of_choice}_"
output_directory_name += f"ignore_sequence_{ignore_sequence}"
cmd = f" \" "
cmd += f" abridge "
cmd += f" --keep_intermediate_error_files "
cmd += f" --decompress "
cmd += f" --genome /software/Abridge/example/arath_1.fa "
cmd += f" --inputabrfilenames {output_directory_name.split('_ignore_sequence_')[0].replace('decompress','compress')}/{inputfilename_without_location}.abridge "
cmd += f" --output_directory {output_directory_name} "
if ignore_sequence == 1:
cmd += " --ignore_sequence "
cmd += f" 1> {output_directory_name}.output"
cmd += f" 2> {output_directory_name}.error"
cmd += f" \" "
# print( cmd )
command_set.append( cmd )
command_set.append( output_directory_name.replace( "/", "-" )[1:] )
command_set.append( volumes_list )
command_set.append( framework_of_choice )
# if len( allinputs ) < 1:
allinputs.append( command_set )
# if len( allinputs ) >= 10: break
pool.map( runDockerOrSingularityInParallel, allinputs )
else:
volumes_list = [f"{'/'.join(options.genome.split('/')[:-1])}:{'/'.join(options.genome.split('/')[:-1])}",
f"{options.output_directory}:{options.output_directory}",
f"{options.error_directory}:{options.error_directory}",
]
cmd = f" \" "
cmd += f" abridge "
if options.keep_intermediate_error_files == True:
cmd += f" --keep_intermediate_error_files "
if options.compress == True:
cmd += f" --compress "
cmd += f" --inputfilename {options.inputfilename} "
volumes_list.append( f"{'/'.join(options.inputfilename.split('/')[:-1])}:{'/'.join(options.inputfilename.split('/')[:-1])}", )
elif options.decompress == True:
cmd += f" --decompress "
cmd += f" --inputabrfilenames {options.inputabrfilenames} "
volumes_list.append( f"{'/'.join(options.inputabrfilenames.split('/')[:-1])}:{'/'.join(options.inputabrfilenames.split('/')[:-1])}", )
cmd += f" --genome {options.genome} "
cmd += f" --output_directory {options.output_directory} "
if options.ignore_alignment_scores == True:
cmd += f" --ignore_alignment_scores "
if options.ignore_quality_scores == True:
cmd += f" --ignore_quality_scores "
if options.ignore_soft_clippings == True:
cmd += f" --ignore_soft_clippings "
if options.ignore_mismatches == True:
cmd += f" --ignore_mismatches "
if options.ignore_unmapped_reads == True:
cmd += f" --ignore_unmapped_reads "
cmd += f" --framework {framework_of_choice}"
cmd += f" 1> {output_filename}"
cmd += f" 2> {error_filename}"
cmd += f" \" "
volumes_list = list( set( volumes_list ) )
# os.system( cmd )
if framework_of_choice == "docker":
runDockerCommand( name = "abridge",
version = list_of_softwares_with_versions['abridge'],
image_location = f"ghcr.io/sagnikbanerjee15/docker_tools_and_pipelines/abridge",
container_name = f"{options.output_directory.split('/')[-1]}",
volumes = volumes_list,
command = cmd,
cpus = options.cpu,
memory = '100g'
)
elif framework_of_choice == "singularity":
runSingularityCommand( name = "abridge",
version = list_of_softwares_with_versions['abridge'],
image_location = "ghcr.io/sagnikbanerjee15/docker_tools_and_pipelines/abridge",
container_name = f"{options.output_directory.split('/')[-1]}",
volumes = volumes_list,
command = cmd,
cpus = options.cpu,
memory = '100g'
)
if __name__ == "__main__":
main()