-
Notifications
You must be signed in to change notification settings - Fork 5
/
pipeline.py
1417 lines (1192 loc) · 59.7 KB
/
pipeline.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
__author__ = 'Tomislav Ilicic & Frederik Otzen Bagger'
__copyright__ = 'Copyright (C) 2015 Tomislav Ilicic'
__license__ = 'GNU Public Licence'
__version__ = 0.9
import os
import sys
import glob
import re
import ConfigParser
import subprocess
import shutil
import logging
import os, fnmatch
import numpy
import pandas as pd
from operator import itemgetter
from itertools import groupby
from os.path import basename
#LIST ALL DIRECTORIES IN SELECTED DIRECTORY. NOT RECURSIVE
def list_dirs(dir):
walk = os.walk(dir)
if (len(list(os.walk(dir))) == 0):
return []
else :
return next(os.walk(dir))[1]
def print_error (message):
print "[ERROR]: " + message;
sys.exit(1);
#CHECK IF OBJECT HAS BEEN SPECIFIED
def check_object_specified (type, object, objects_available):
if (len(objects_available) == 0):
print_error("No " + type + " are available. Please create one.");
else:
#IF OBJECT IS NOT AVAILABLE
if (object not in objects_available):
counter = 0;
objects_available_num = [];
for key in objects_available:
counter = counter + 1
objects_available_num.append(str(counter) + ". " + str(key))
#AND SUGGEST TO USER
object = str(object)
print_error(type + " \"" + object + "\" not available or not specified. Create or choose from:\n" + "\n".join(objects_available_num));
#TOP LEVEL FUNCTION EXECUTING THE PIPELINE
def run(args):
logging.basicConfig(level=logging.INFO)
if (not len(sys.argv) > 1):
return False
logging.info("Staring pipeline")
code = 0
#USER WANTS TO CREATE REFERENCE GENOME INDEX FILE
if (args.fasta != None and args.gtf != None):
logging.info("Genome index creation has started")
code = run_ref_genome_index(args.mapping, args.fasta, args.gtf,args.cluster)
if(code == 1):
logging.error("Genome index creation unsuccessful")
logging.error("Pipeline broke.")
return(1)
logging.info("Genome index creation complete")
#ALL OTHER THINGS
else:
#INPUT NAME/DIR TO BE SET AT THIS POINT
if (args.input != None):
logging.info("Collecting input files")
files = collect_input(args.input)
else:
print_error("Input folder (-i) not specified")
user_out=ConfigSectionMap("DIRECTORIES")['temp_dir']+args.input+"/"+args.output
#PIPELINE MAIN OUTPUT DIRECTORIES
preprocessing_root = user_out+"/" + ConfigSectionMap("DIRECTORIES")['preprocessed_root']
mapping_root = user_out+"/" + ConfigSectionMap("DIRECTORIES")['mapping_root']
counts_root = user_out+"/" + ConfigSectionMap("DIRECTORIES")['quantification_root']
stats_root = user_out+"/" + ConfigSectionMap("DIRECTORIES")['stats_root']
#MAPPING CHOOSED
if (args.mapping != None):
logging.info("Mapping started")
code = run_mapping(args.mapping, args.mapping_args, mapping_root, stats_root, args.input, files, args.genome, args.cluster,args.overwrite)
if(code == 1):
logging.error("Mapping unsuccessful")
logging.error("Pipeline broke.")
return(1)
else:
logging.info("Mapping sucessful")
#QUANTIFICATION CHOOSED
if (args.quantification != None):
logging.info("Quantification started")
code = run_quantification(args.quantification, args.quant_args, counts_root, mapping_root, args.input, files[1], args.genome, args.cluster, args.overwrite)
if (code == 1):
logging.error("Quantification unsuccessful")
logging.error("Pipeline broke.")
return(1)
else:
logging.info("Quantification sucessful")
if (args.range != None):
range = args.range.split(",")
range = map(int, range)
files_process_index = set(range).intersection(files_process_index)
logging.info("Pipeline is done.")
return code
def write_stats(used_genome, sample_name,files_process_index, stats_root, mapping_root, cluster, overwrite):
GTF = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + used_genome + ConfigSectionMap("EXTENSIONS")['ext_gtf']
log_file = stats_root + "/stats.%I.log"
sorted_sam_files = mapping_root + "/sam/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
_write_mapping_stats(GTF, files_process_index, sorted_sam_files, stats_root, sample_name, log_file, cluster, overwrite)
def _write_mapping_stats(GTF_file, files_process_index, sorted_sam_files, stats_root, sample_name, log_file, cluster, overwrite):
stats_output_merged = stats_root + "/" + sample_name + ".stats"
logging.info("Generating mapping statistics.")
expected_output_stats = list_expected_output(files_process_index, ".stats", stats_root, sample_name)
if(overwrite == False and pre_checks(expected_output_stats) == True and os.path.exists(stats_output_merged)):
return (0)
stats_output = stats_root + "/" + sample_name + "_{#}.stats"
commands = []
commands.append("python")
commands.append(os.path.dirname(sys.argv[0])+ "/lib/stats.py") # change PATH
commands.append("-i")
commands.append(sorted_sam_files)
commands.append("-o")
commands.append(stats_output)
commands.append("-n")
commands.append(sample_name + "_{#}.counts")
commands.append("-g")
commands.append(GTF_file)
code = execute(files_process_index, commands, log_file, cluster)
if (code !=0):
return code
commands = []
commands.append("echo")
s=""
for i in range(0,10):
s += "S_"+str(i) + ","
s += "S_10+"
commands.append("cell,total,mapped,unmapped,unique,multi,intergenic,intragenic,exonic,intronic,ambigious,exonic_unique,exonic_multi,alignments,multi-intergenic,multi-intragenic,multi-exonic,multi-intronic,multi-ambigious,perfect,partly_perfect,mapped_no_correct,"+s+",I,D,INDEL")
commands.append(">")
commands.append(stats_output_merged)
proc = subprocess.Popen(" ".join(commands), stdout=subprocess.PIPE, shell=True)
output = proc.stdout.read()
commands = []
commands.append("cat")
commands.append(stats_root + "/" + sample_name + "_*.stats")
commands.append(">>")
commands.append(stats_output_merged)
proc = subprocess.Popen(" ".join(commands), stdout=subprocess.PIPE, shell=True)
output = proc.stdout.read()
return (code)
#TOP LEVEL FUNCTION TO RUN QUANTIFICATION
def run_quantification(quantification, quant_args, counts_root, mapping_root, sample_name, files_process_index, genome, cluster, overwrite):
available_quants = list_dirs(ConfigSectionMap("DIRECTORIES")['tools_quantification']);
available_genomes = list_dirs(ConfigSectionMap("DIRECTORIES")['ref_dir']);
#THRO ERROR IF QUANTIFICATION TOOL IS NOT AVAILABLE
check_object_specified("Quantification tool (-q)", args.quantification, available_quants)
#THROW ERROR IF GENOME NOT AVAILABLE
if (args.genome == None):
print_error("Reference genome (-g) option not specified")
check_object_specified("Reference genome (-g)", genome, available_genomes);
q_args = ""
if (quant_args != None):
q_args = quant_args[0].split(" ")
del quant_args[0]
q_args = q_args + quant_args
return(_run_quantification(quantification, counts_root, mapping_root, sample_name, files_process_index, cluster, overwrite, genome, q_args))
#RUNS ALL QUANTIFICATION RELATED STUFF
def _run_quantification(quantifier, counts_dir, mapping_root, sample_name, files_process_index, cluster, overwrite, genome, quant_args):
logging.info("Quantification tool:" + quantifier)
logging.info("Passed parameters:" + quant_args)
#Count dir example: /counts/htseq
#Check if counts already exist
quant_output_dir = counts_dir + "/" + quantifier.replace(".", "_")
expected_output_counts = list_expected_output(files_process_index, ConfigSectionMap("EXTENSIONS")['ext_counts'], quant_output_dir, sample_name)
output_file = quant_output_dir + "/" + sample_name + "_" + quantifier.replace(".", "_") + ConfigSectionMap("EXTENSIONS")['ext_counts']
#If doesn't exist, create dir and run
if(pre_checks(expected_output_counts) == False or overwrite == True or os.path.exists(output_file)==False):
make_dir(quant_output_dir)
#Check if required sorted bam files exist
sorted_bam_dir = mapping_root + "/" + ConfigSectionMap("DIRECTORIES")['sorted_bam_root']
sorted_bam_files = sorted_bam_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sorted']
#HACK
#sorted_bam_files = mapping_root + "/sam/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
expected_input_sorted_bam = list_expected_output(files_process_index, ConfigSectionMap("EXTENSIONS")['ext_sorted'], sorted_bam_dir, sample_name)
log_file = quant_output_dir + "/" + sample_name + "_%I" + ConfigSectionMap("EXTENSIONS")['ext_log']
output_counts = quant_output_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_counts']
#If sorted bam files do not exist, return 1
if(not pre_checks(expected_input_sorted_bam, verbose=True)):
print("Sorted bam files needed for quantification, but do not exist under dir:" + sorted_bam_dir)
return (1)
#Run selected quantification tool
if("HTSeq" in quantifier):
code = execute_htseq(sample_name,quantifier, quant_args, genome, sorted_bam_files, output_counts, files_process_index, log_file, cluster)
print(output_file)
merge(expected_output_counts, output_file,[1], -1)
elif("cufflinks" in quantifier):
code = 0
code = execute_cufflink(sample_name, quantifier, quant_args, genome, sorted_bam_files, quant_output_dir, output_counts, files_process_index, log_file, cluster)
merge(expected_output_counts, output_file,[9], 1)
#Return code
if(code != 0):
logging.error("Please check log files for more information: "+ quant_output_dir)
if(code != 0 or not post_checks(expected_output_counts)):
return 1
else:
output_file = quant_output_dir + "/" + sample_name + "_" + quantifier.replace(".", "_") + ConfigSectionMap("EXTENSIONS")['ext_counts']
#Return code
return 0
def merge(files, output_file, columns_list, skip_row_i):
#COLUMNS TO EXTRACT FROM FILE
columns = columns_list
#SKIP ROW
if (skip_row_i != -1):
data = pd.read_table(files[0], skiprows=skip_row_i)
else:
data = pd.read_table(files[0], header=None)
d = data.iloc[:, columns]
row_names = data.iloc[:,0]
merged = pd.DataFrame(d)
file_names = []
file_name = os.path.basename(files[0])
file_names.append(file_name)
for i in range(1, len(files)):
f = files[i]
file_name = os.path.basename(f)
file_names.append(file_name)
if (skip_row_i != -1):
data = pd.read_table(f, skiprows=skip_row_i)
else:
data = pd.read_table(f, header=None)
d = data.iloc[:, columns]
merged = pd.concat([merged,d], axis=1)
merged.columns = file_names
merged.index = row_names
merged.index.name = "Genes"
merged.to_csv(output_file, sep="\t")
return (merged)
#EXECUTES CUFFLINK: USING SORTED BAM FILES BY NAME
def execute_cufflink(sample_name, quantifier, quant_args, genome_name, sorted_bam_files, quant_output_dir, output_counts, files_process_index, log_file, cluster):
CUFFLINKS = ConfigSectionMap("DIRECTORIES")['tools_quantification'] + "/"+ quantifier + "/" + ConfigSectionMap("SOFTWARE")['cufflinks']
gtf_file = ConfigSectionMap("DIRECTORIES")['ref_dir'] + "/" + genome_name + "/" + genome_name + ConfigSectionMap("EXTENSIONS")['ext_gtf']
commands = []
commands.append(CUFFLINKS)
commands.append("-p")
commands.append(args.cpu)
commands.append("--output-dir")
commands.append( quant_output_dir + "/{#}")
commands.append("-G")
commands.append(gtf_file)
commands.append(sorted_bam_files)
code = execute(files_process_index, commands, log_file, cluster)
#Tophat produces sam files where you cannot change the name. So one needs to manually change name and location
if(code == 0):
move_files(files_process_index, quant_output_dir + "/{#}/genes.fpkm_tracking", quant_output_dir+"/"+sample_name, ConfigSectionMap("EXTENSIONS")['ext_counts'])
return 0
#EXECUTES HTSEQI: USING SORTED BAM FILES BY NAME
def execute_htseq(sample_name, quantifier, quant_args, genome_name, sorted_bam_files, output_counts, files_process_index, log_file, cluster):
HTSEQ = ConfigSectionMap("DIRECTORIES")['tools_quantification'] + "/"+ quantifier + "/" + ConfigSectionMap("SOFTWARE")['htseqtool']
gtf_file = ConfigSectionMap("DIRECTORIES")['ref_dir'] + "/" + genome_name + "/" + genome_name + ConfigSectionMap("EXTENSIONS")['ext_gtf']
commands = []
commands.append(HTSEQ)
commands.append("-f bam")
commands.append("-r pos")
commands.append("-a 0")
commands.append("-s no")
commands.append(" ".join(convert_to_default_map_param(quant_args)))
commands.append(sorted_bam_files)
commands.append(gtf_file)
commands.append(">")
commands.append(output_counts)
return(execute(files_process_index, commands, log_file, cluster))
#TOP LEVEL FUNCTION MAPPING ALGORITHM
def run_mapping(mapper,mapping_args, mapping_root, stats_root, sample_name, files, genome, cluster, overwrite):
files_process_index = files[1]
files_index_holder = files[2]
paired = files[3]
read_length = files[4]
available_mapper = list_dirs(ConfigSectionMap("DIRECTORIES")['tools_mapping']);
available_genomes = list_dirs(ConfigSectionMap("DIRECTORIES")['ref_dir']);
mapper_for_ref = mapper
#THE PROBLEM WITH TOPHAT IS THAT IT RELIES ON BOWTIE
#HENCE IT CAN'T BE TREATED INDIVIDUALLY.
#THEREFORE IF TOPHAT WAS SELECTED, IT LOOKS UP WHICH
#BOWTIE VERSION IS THE NEWEST AND GETS ALL AVAILABLE
#REFERENCE GENOME FOR BOWTIE
#if "tophat" not in mapper:
if("tophat" in mapper):
mapper_for_ref= get_latest_mapper("bowtie2")
logging.info("Tophat will use Bowtie version: "+ mapper_for_ref)
check_object_specified("Mapping tool (-m)", mapper_for_ref, available_mapper)
#ONLY LIST GENOMES THAT ARE AVAIALBLE FOR A SPECIFIC MAPPING TOOL
mapper_available_genomes = []
for key in available_genomes:
g = list_dirs(ConfigSectionMap("DIRECTORIES")['ref_dir'] + "/" + key);
if (mapper_for_ref in g):
mapper_available_genomes.append(key)
#THROW ERROR IF GENOME NOT AVAILABLE
if (args.genome == None):
print_error("Reference genome (-g) option not specified")
check_object_specified("Reference genome (-g)", genome, mapper_available_genomes);
map_args = ""
if (mapping_args != None):
map_args = mapping_args[0].split(" ")
del mapping_args[0]
map_args = map_args + mapping_args
files_index_holder = sorted(files_index_holder)
#EXECUTE MAPPING
code = (_run_mapping_all_steps(mapper,map_args, genome, files_index_holder, files_process_index, paired, read_length, mapping_root, sample_name, cluster, overwrite))
if (code == 0):
make_dir(stats_root)
write_stats(genome, sample_name, files_process_index, stats_root, mapping_root, args.cluster, overwrite)
return(code)
#TOP LEVEL FUNCTION TO CREATE REF GENOME INDEX FILES
def run_ref_genome_index(mapper, fasta_file, gtf, cluster):
#CHECK IF GTF AND GENOME FASTA FILE HAVE BEEN SET
available_mapper = list_dirs(ConfigSectionMap("DIRECTORIES")['tools_mapping']);
if (not os.path.isfile(fasta_file)):
print_error("FASTA sequencing file\" " + fasta_file + "\" does not exist");
if (gtf == None):
print_error("GTF file (-gtf) not specified.");
if (not os.path.isfile(gtf)):
print_error("GTF file \'" + gtf + "\' does not exist.");
if (mapper == None):
print_error("Mapping tool (-m) not specified.");
check_object_specified("Mapping tool (-m)", mapper, available_mapper);
code = _run_ref_genome_index_all_steps(mapper, fasta_file, gtf, cluster)
return(code)
#RUNS THE MAPPING OPTION
# + SORTING AND CREATING BAM FILES
def _run_mapping_all_steps(mapper, mapper_args, used_genome, files_index_holder, files_process_index,paired, read_length, mapping_root, sample_name, cluster, overwrite):
#MAPPINGOUTPUT DIRECTORIES
mapping_dir = mapping_root+"/"+ConfigSectionMap("DIRECTORIES")['sam_root'];
sorted_dir = mapping_root+"/"+ConfigSectionMap("DIRECTORIES")['sorted_bam_root'];
logging.info("Mapper: " + mapper)
logging.info("Passed parameters: " + " ".join(mapper_args))
code = 0
#1. MAPPING
#CHECK IF FILES ALREADY EXIST
expected_output_sam = list_expected_output(files_process_index, ConfigSectionMap("EXTENSIONS")['ext_sam'], mapping_dir, sample_name)
if(pre_checks(expected_output_sam) == False or overwrite == True):
make_dir(mapping_dir)
used_genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + mapper
code = exec_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster)
if(code != 0):
logging.error("Please check log files for more information: "+ mapping_dir)
return 1
if(not post_checks(expected_output_sam)):
return 1
logging.info("Sorting (as bam)...")
#2. CONVERT TO BAM AND SORT IT
#CHECK IF FILES ALREADY EXIST
expected_output_sorted = list_expected_output(files_process_index, ConfigSectionMap("EXTENSIONS")['ext_sorted'], sorted_dir, sample_name)
if(pre_checks(expected_output_sorted) == False or overwrite == True):
make_dir(sorted_dir)
code = sort_sam_to_bam(files_process_index, mapping_dir, sorted_dir, sample_name, cluster)
if(code != 0):
logging.error("Please check log files for more information: "+ mapping_dir)
if(not post_checks(expected_output_sorted)):
return 1
logging.info("Output-directory:" + mapping_root)
return 0
#SORT A SAM FILE AND CONVERT IT TO A BAM FILE
def sort_sam_to_bam(files_process_index, mapper_dir, sorted_dir, sample_name, cluster):
output_bam_file = sorted_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sorted']
input_sam_file = mapper_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
commands = []
commands.append("java -jar")
commands.append(ConfigSectionMap("SOFTWARE")['picard_tool'] + "/SortSam.jar")
commands.append("INPUT="+input_sam_file);
commands.append("OUTPUT="+ output_bam_file);
#commands.append("SORT_ORDER=queryname");
commands.append("SORT_ORDER=coordinate");
commands.append("CREATE_INDEX=true");
commands.append("VALIDATION_STRINGENCY=LENIENT");
log_file = sorted_dir + "/sorting_%I"+ ConfigSectionMap("EXTENSIONS")['ext_log']
code = execute(files_process_index, commands, log_file, cluster, True)
return(code)
#CREATE A DIRECTORY RECURSIVELY
def make_dir(dir):
if not os.path.exists(dir):
os.makedirs(dir)
#RUNS CREATING INDEX FILE FOR A GENOME
def _run_ref_genome_index_all_steps(mapper, fasta_file, gtf, cluster):
code = 0
genome_name = basename(fasta_file)
genome_name = genome_name.replace(".", "_")
genome_root_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + "/" + genome_name
genome_mapper_dir = genome_root_dir + "/" + mapper
exists = False
#SINCE TOPHAT IS SPECIAL, DONT CREATE A DIR
if ("tophat" in mapper):
#CHECK TOPHAT TRANSCRITPOME INDEX IF IT EXISTS (under /bowtie2 index files)
bowtie2_mapper = get_latest_mapper("bowtie2")
bowtie2_transcritome_dir = (genome_root_dir + "/"+ bowtie2_mapper + "/transcriptome_data")
genome_mapper_dir = bowtie2_transcritome_dir
#CHECK IF GENOME MAPPING DIR ALREADY EXISTS
if(os.path.isdir(genome_mapper_dir)):
logging.warning("Genome index exists. Delete manually: "+ genome_mapper_dir)
exists = True
else:
if("tophat" not in mapper):
make_dir(genome_mapper_dir)
else:
make_dir(genome_root_dir)
gtf_dest = genome_root_dir + "/" + genome_name + ConfigSectionMap("EXTENSIONS")['ext_gtf']
if(not os.path.exists(gtf_dest)):
#Copy GTF file to ref genome root dir
shutil.copy2(gtf, gtf_dest)
fasta_file_dest = genome_root_dir + "/" + genome_name + ConfigSectionMap("EXTENSIONS")['ext_fasta']
if(not os.path.exists(fasta_file_dest)):
#Copy fasta file to ref genome root dir
shutil.copy2(fasta_file, fasta_file_dest)
#TO DO
#CREATE SEQUENCE DIRECTORY FILE
seq_directory_file = fasta_file_dest + ConfigSectionMap("EXTENSIONS")['ext_dict']
if(not os.path.exists(seq_directory_file)):
logging.info("Creating sequence directory")
code = create_seq_dir(fasta_file_dest, cluster)
if(code != 0 or not os.path.exists(seq_directory_file)):
logging.error("File not created:" + seq_directory_file)
logging.info("Creating sequence directory was successful")
log_file = genome_root_dir + "/" + mapper + "_index.log"
if (exists):
return 0
logging.info("Creating index files...(will take a while)")
if ("bowtie-1" in mapper):
code = create_bowtie1_index(mapper, fasta_file, gtf, genome_mapper_dir, genome_name,cluster, log_file)
elif ("bowtie2" in mapper):
code = create_bowtie2_index(mapper, fasta_file, gtf, genome_mapper_dir, genome_name,cluster, log_file)
elif ("bwa" in mapper):
code = create_bwa_index(mapper, fasta_file, gtf, genome_mapper_dir, genome_name,cluster, log_file)
elif ("gmap" in mapper):
code = create_gmap_index(mapper, fasta_file, gtf, genome_mapper_dir, genome_name,cluster, log_file)
elif ("STAR" in mapper):
code = create_star_index(mapper, fasta_file, gtf, genome_mapper_dir, genome_name,cluster, log_file)
elif ("tophat" in mapper):
code = create_tophat_index(mapper, fasta_file_dest, gtf_dest, genome_mapper_dir, genome_name, cluster, log_file)
if (code == 0):
logging.info("Creating index files was successful:" + genome_mapper_dir)
else:
logging.error("Creating index files failed. Check logs:" + log_file)
return(code)
#CREATES SEQUENCE DIRECTORY FILE NEEDED FOR QUANTIFICATION
def create_seq_dir(fasta_file_dest, cluster):
log_file = fasta_file_dest + ConfigSectionMap("EXTENSIONS")['ext_dict'] + ConfigSectionMap("EXTENSIONS")['ext_log']
picard = ConfigSectionMap("SOFTWARE")['picard_tool']
commands = []
commands.append("java -jar")
commands.append(picard+"/CreateSequenceDictionary.jar")
commands.append("REFERENCE=" + fasta_file_dest)
commands.append("OUTPUT=" + fasta_file_dest + ConfigSectionMap("EXTENSIONS")['ext_dict'])
return(execute([], commands, log_file, cluster))
#LIST EXPECTED OUTPUT
def list_expected_output(files_process, ext, out_dir, sample_name):
expected = []
for i in files_process:
#dir/sampleName_index_.ext
expected.append(out_dir + "/" + sample_name + "_" + str(i) + ext)
return expected
#EXECUTE A GIVEN MAPPING ALGORITHM ON A GIVEN CLUSTER SYSTEM
def exec_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster):
code = 0
if ("bowtie-1" in mapper):
code = exec_bowtie1_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster)
elif ("bowtie2" in mapper):
code = exec_bowtie2_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster)
elif ("bwa" in mapper):
code = exec_bwa_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster)
elif ("gmap" in mapper):
code = exec_gmap_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster)
elif ("STAR" in mapper):
code = exec_star_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster)
elif ("tophat" in mapper):
code = exec_tophat_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster)
return(code)
def exec_star_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster):
star = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['star']
genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + mapper
code = 0
commands = []
GTF = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + used_genome + ConfigSectionMap("EXTENSIONS")['ext_gtf']
log_file1 = mapping_dir + "/" + sample_name + "_%I.mapping.log"
output_sam_file = mapping_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
commands.append(star)
commands.append("--outFileNamePrefix")
commands.append(mapping_dir + "/" + sample_name + "_{#}.")
commands.append("--genomeDir")
commands.append(genome_dir)
commands.append("--readFilesIn")
commands.append(" ".join(files_index_holder))
commands.append("--genomeLoad")
commands.append("NoSharedMemory")
commands.append("--outSAMunmapped")
commands.append("Within")
commands.append("--outStd")
commands.append("SAM")
#TEMP FOLDER WHERE EVERYTHING IS STORED
#NEEDS TO BE DELETED OTHERWISE ERROR FROM STAR
TEMP = mapping_dir + "/" + sample_name + "_{#}" + "/"
for i in files_process_index:
f = TEMP.replace("{#}", str(i))
if(os.path.isdir(f)):
shutil.rmtree(f)
#commands.append("--outTmpDir")
#commands.append(TEMP)
options = convert_to_default_map_param(mapper_args)
splice = "-splice" in options
if (splice):
commands.append("--sjdbGTFfile")
commands.append(GTF)
options.remove("-splice")
commands.append(" ".join(options))
commands.append(">")
commands.append(output_sam_file)
return(execute(files_process_index, commands, log_file1, cluster, True))
def exec_gmap_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster):
gmap = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['gsnap']
genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + mapper
code = 0
commands = []
fasta_size = os.stat(ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + used_genome + ConfigSectionMap("EXTENSIONS")['ext_fasta']).st_size
fasta_size_GB = fasta_size / 1024.0 /1024.0 / 1024.0
if (fasta_size_GB >= 5.0) :
gmap = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['gsnapl']
output_sam_file = mapping_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
commands.append(gmap)
commands.append("-A sam -B 5")
commands.append("-t")
commands.append(args.cpu)
options = convert_to_default_map_param(mapper_args)
splice = "-splice" in options
splice_f = genome_dir + "/" + used_genome + ConfigSectionMap("EXTENSIONS")['ext_gsnap_splice']
#Only add splice file if not empty. Otherwise gmap results in a weired error
if(splice == True and os.stat(splice_f).st_size > 0):
commands.append("-s")
commands.append(genome_dir + "/" + used_genome)
options.remove("-splice")
commands.append(" ".join(options))
commands.append("-d")
commands.append(used_genome)
commands.append("-D")
commands.append(genome_dir)
#COMMAND
log_file1 = mapping_dir + "/" + sample_name + "_%I.mapping.log"
#Peform two alignments and sampe command and convert to .sam file
if(paired):
commands.append(files_index_holder[0])
commands.append(files_index_holder[1])
else:
commands.append(files_index_holder[0])
commands.append(">")
commands.append(output_sam_file)
return(execute(files_process_index, commands, log_file1, cluster, True))
def exec_bowtie1_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster):
BOWTIE1 = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['bowtie1']
genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + mapper
code = 0
commands = []
output_sam_file = mapping_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
commands.append("export BOWTIE_INDEXES=" +genome_dir+ " && ")
commands.append(BOWTIE1)
commands.append("-S")
commands.append(genome_dir + "/" + used_genome)
#COMMAND
log_file1 = mapping_dir + "/" + sample_name + "_%I.mapping.log"
#Peform two alignments and sampe command and convert to .sam file
if(paired):
commands.append("-1")
commands.append(files_index_holder[0])
commands.append("-2")
commands.append(files_index_holder[1])
else:
commands.append(files_index_holder[0])
commands.append(" ".join(convert_to_default_map_param(mapper_args)))
commands.append(">")
commands.append(output_sam_file)
return(execute(files_process_index, commands, log_file1, cluster, True))
def exec_bowtie2_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster):
BOWTIE1 = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['bowtie2']
genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + mapper
code = 0
commands = []
output_sam_file = mapping_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
commands.append("export BOWTIE2_INDEXES=" +genome_dir+ " && ")
commands.append(BOWTIE1)
commands.append("-x")
commands.append(genome_dir + "/" + used_genome)
#COMMAND
log_file1 = mapping_dir + "/" + sample_name + "_%I.mapping.log"
#Peform two alignments and sampe command and convert to .sam file
if(paired):
commands.append("-1")
commands.append(files_index_holder[0])
commands.append("-2")
commands.append(files_index_holder[1])
else:
commands.append(files_index_holder[0])
commands.append(" ".join(convert_to_default_map_param(mapper_args)))
commands.append(">")
commands.append(output_sam_file)
return(execute(files_process_index, commands, log_file1, cluster, True))
def pre_checks(expected_output, verbose=False):
not_exists = []
for exp in expected_output:
if(not os.path.exists(exp)):
not_exists.append(exp)
if (len(not_exists) == 0):
if(not verbose):
logging.info("Output already exists:" + ",".join(expected_output))
return True
return False
def post_checks(expected_output):
not_exists = []
for exp in expected_output:
if(not os.path.exists(exp)):
not_exists.append(exp)
if (len(not_exists) != 0):
logging.error("Following files don't exists:" + ",".join(not_exists))
return False
return True
#CREATE BOWTIE2 INDEX FILES
def create_gmap_index(mapper, fasta_file, gtf, genome_dir, genome_name, cluster, log_file):
build = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['gsnap_build']
gtf_iit_tool = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['gtf_iit_tool']
gtf_splice_tool = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['gtf_splice_tool']
gtf_out = genome_dir + "/" +genome_name + ConfigSectionMap("EXTENSIONS")['ext_gsnap_splice']
logging.info("Creating splice sites file...")
commands = []
commands.append("cat")
commands.append(gtf)
commands.append(" | " + gtf_splice_tool + " -")
commands.append(">")
commands.append(gtf_out)
code = execute([], commands, log_file, cluster)
if(code == 0):
logging.info("Successful")
else:
logging.error("Unsuccessful")
return code
splice_file = genome_dir + "/" + genome_name + ConfigSectionMap("EXTENSIONS")['ext_gsnap_iit'];
logging.info("Creating splice sites file II ...")
commands = []
commands.append("cat")
commands.append(gtf_out)
commands.append(" | " + gtf_iit_tool)
commands.append("-o " + splice_file)
code = execute([], commands, log_file, cluster)
if(code == 0):
logging.info("Successful")
else:
logging.error("Unsuccessful")
return code
commands = []
commands.append(build)
commands.append("-d")
commands.append(genome_name)
commands.append(fasta_file)
commands.append("-D")
commands.append(genome_dir)
code = execute([], commands, log_file, cluster, True)
return(code)
#CREATE BOWTIE2 INDEX FILES
def create_bowtie2_index(mapper, fasta_file, gtf, genome_dir, genome_name, cluster, log_file):
bowtie_build = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['bowtie2_build']
commands = []
commands.append("export BOWTIE_INDEXES=" + genome_dir + " && (")
commands.append(bowtie_build)
commands.append(fasta_file)
commands.append(genome_name)
commands.append(";mv " + genome_name + "*.bt2 "+ genome_dir + ")")
code = execute([], commands, log_file, cluster, True)
return(code)
#CREATE BOWTIE1 INDEX FILES
def create_bowtie1_index(mapper, fasta_file, gtf, genome_dir, genome_name, cluster, log_file):
bowtie_build = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['bowtie1_build']
commands = []
commands.append("export BOWTIE_INDEXES=" + genome_dir + " && (")
commands.append(bowtie_build)
commands.append(fasta_file)
commands.append(genome_name)
commands.append(";mv " + genome_name + "*.ebwt "+ genome_dir + ")")
code = execute([], commands, log_file, cluster, True)
return(code)
#CREATE BOWTIE1 INDEX FILES
def create_star_index(mapper, fasta_file, gtf, genome_dir, genome_name, cluster, log_file):
build = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['star_build']
commands = []
commands.append(build)
commands.append("--runMode genomeGenerate")
commands.append("--genomeDir")
commands.append(genome_dir)
commands.append("--genomeFastaFiles")
commands.append(fasta_file)
commands.append("--limitGenomeGenerateRAM 186718572928")
commands.append("--genomeChrBinNbits 18")
code = execute([], commands, log_file, cluster, True)
return(code)
#CREATE BWA INDEX FILES
def create_bwa_index(mapper, fasta_file, gtf, genome_dir, genome_name, cluster, log_file):
build = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['bwa_build']
commands = []
commands.append("(")
commands.append(build)
commands.append("index")
commands.append("-a bwtsw")
commands.append("-p")
commands.append(genome_name)
commands.append(fasta_file)
move = genome_name + "*.amb " +genome_name + "*.ann " +genome_name + "*.pac " +genome_name + "*.bwt " +genome_name + "*.sa "
commands.append(";mv " + move + " "+ genome_dir + ")")
code = execute([], commands, log_file, cluster, True)
return(code)
#CREATE TOPHAT INDEX FILES
def create_tophat_index(mapper, fasta_file, gtf, genome_dir, genome_name, cluster, log_file):
bowtie2_mapper = get_latest_mapper("bowtie2")
TOPHAT = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['tophat']
#IMPORTANT TO USE THE / AT THE END!!
used_genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + genome_name + "/" + bowtie2_mapper + "/"
if(not os.path.isdir(used_genome_dir)):
logging.error("Genome index: "+ genome_name + " not available for Tophat. Please select a bowtie2 mapper to generate genome index first, before creating transcriptome.")
return(1)
#TOPHAT NEEDS FASTA FILE NEXT TO OTHER BOWTIE2 INDEX FILES
shutil.copy2(fasta_file, used_genome_dir)
code = 0
commands = []
commands.append("export BOWTIE2_INDEXES=" + used_genome_dir + " && ")
commands.append("export PATH=\$PATH:"+ ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ bowtie2_mapper + " && ")
commands.append(TOPHAT)
commands.append("-G " + gtf)
commands.append("--transcriptome-index " + "transcriptome_data/known")
commands.append(genome_name)
#Important to change current working dir. As all files will be created there
current_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(used_genome_dir)
code = execute([], commands, log_file, cluster, True)
os.chdir(current_dir)
return(code)
#MAP USING TOPHAT2
#MAPS FIRST TO GENOME AND THEN TO TRANSCRIPTOME
def exec_tophat_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster):
bowtie2_mapper = get_latest_mapper("bowtie2")
log_file = mapping_dir + "/" + sample_name + "_%I.mapping.log"
TOPHAT = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['tophat']
#IMPORTANT TO USE THE / AT THE END!!
used_genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + bowtie2_mapper + "/"
code = 0
commands = []
commands.append("export BOWTIE2_INDEXES=" + used_genome_dir + " && ")
commands.append("export PATH=\$PATH:"+ ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ bowtie2_mapper + " && ")
commands.append(TOPHAT)
commands.append("-o " + mapping_dir + "/{#}")
#commands.append("--no-convert-bam")
commands.append("-p")
commands.append(args.cpu)
options = convert_to_doublets_map_param(mapper_args)
splice = "--splice" in options
if (splice):
options.remove("--splice")
commands.append("--transcriptome-index " + used_genome_dir+"/transcriptome_data/known")
commands.append(" ".join(options))
commands.append(used_genome)
commands.append(" ".join(files_index_holder))
code = execute(files_process_index, commands, log_file, cluster, True)
log_file = mapping_dir + "/" + sample_name + "_%I.merge_bam.log"
#Tophat produces sam files where you cannot change the name. So one needs to manually change name and location
if(code == 0):
code = bam_to_sam(files_process_index, [mapping_dir + "/{#}/accepted_hits.bam", mapping_dir + "/{#}/unmapped.bam"], mapping_dir, sample_name, ConfigSectionMap("EXTENSIONS")['ext_sam'], cluster, log_file)
return(code)
#CONVERT BAM TO FILES
def bam_to_sam(files_process_index, files, dest_dir, prefix, ext, cluster, log_file):
input_bam_file = files
output_sam_file = dest_dir + "/" + prefix + "_{#}" + ext
commands = []
commands.append("java -jar")
commands.append(ConfigSectionMap("SOFTWARE")['picard_tool'] + "/MergeSamFiles.jar")
for f in files:
commands.append("INPUT="+f);
commands.append("OUTPUT="+ output_sam_file);
commands.append("SORT_ORDER=unsorted");
commands.append("VALIDATION_STRINGENCY=LENIENT");
code = execute(files_process_index, commands, log_file, cluster)
return(code)
#CONCATINATE FILES
def concat_files(files_process_index, files, dest, ext):
all_files = []
files_process_index_t = list(files_process_index)
for i in files_process_index_t:
temp_f = []
for f in range(0, len(files)):
temp_f.append(files[f].replace("{#}", str(i)))
all_files.append(temp_f)
#CONCATINATE
for i in range(0, len(files_process_index_t)):
output_file = dest+"_"+str(files_process_index_t[i])+ext
temp_f = all_files[i]
with open(output_file, 'w') as outfile:
for fname in temp_f:
with open(fname) as infile:
for line in infile:
outfile.write(line)
outfile.close()
def move_files(files_process_index, source_dir, dest, ext):
for i in files_process_index:
s = source_dir.replace("{#}", str(i))
d = dest+"_"+str(i)+ext
shutil.move(s, d)
#ONVERT PARAM TO DOUBLE WAY: e.g. --i X
def convert_to_doublets_map_param(mapper_args):
m_args = []
for arg in mapper_args:
m_args.append("--"+arg.replace("=", " "))
return(m_args)
#CONVERT PARAM TO DEFAULT WAY: e.g. -i X
def convert_to_default_map_param(mapper_args):
m_args = []
for arg in mapper_args:
m_args.append("-"+arg.replace("=", " "))
return(m_args)
#FOR A GIVEN ALGORITHM, GET THE LATEST VERSION AVAILABLE LOCALLY
def get_latest_mapper(mapper):
available_mapper = list_dirs(ConfigSectionMap("DIRECTORIES")['tools_mapping']);
available_mapper = sorted(available_mapper)
for i in range(0, len(available_mapper)):
latest = available_mapper[i]
if(mapper in latest):
return(latest)
return("")
#EXECUTE BWA SHORT OR LONG MAPPING
#DECIDES AUTOMATLICALLY IF SHORT OR LONG
def exec_bwa_mapping(mapper, mapper_args, read_length, used_genome, files_index_holder, files_process_index,paired,mapping_dir, sample_name, cluster):
BWA = ConfigSectionMap("DIRECTORIES")['tools_mapping'] + "/"+ mapper + ConfigSectionMap("SOFTWARE")['bwa']
used_genome_dir = ConfigSectionMap("DIRECTORIES")['ref_dir'] + used_genome + "/" + mapper
code = 0
commands = []
output_sam_file = mapping_dir + "/" + sample_name + "_{#}" + ConfigSectionMap("EXTENSIONS")['ext_sam']
#IF READ SHORT, USE ALN COMMAND
if (read_length < 30):
commands.append(BWA)
commands.append("aln")
commands.append("-t")
commands.append(args.cpu)