-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathassemble.py
821 lines (768 loc) · 34.9 KB
/
assemble.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
from __future__ import absolute_import
from __future__ import print_function
import sys, os, yaml, glob
import subprocess
import string
import sys
from nougat import common
def run(global_config, sample_config):
sorted_libraries_by_insert = common._sort_libraries_by_insert(sample_config)
#Check if the user has specified tools, if not select default list of tools
if "tools" not in sample_config or len(sample_config["tools"]) == 0:
sample_config["tools"] = ["soapdenovo"]
#Execute the commands now
for command in sample_config["tools"]:
command_fn = getattr( sys.modules[__name__] ,
"_run_{}".format(command))
sample_config = command_fn(global_config, sample_config,
sorted_libraries_by_insert)
def _run_abyss(global_config, sample_config, sorted_libraries_by_insert):
########## ACQUIRE ALL THE INFO AND CREATE THE ASSEMBLY FOLDER
assembler = "abyss"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
# in abyss case there is no exectuable
programBIN = global_config["Tools"][assembler]["bin"]
program_options = global_config["Tools"][assembler]["options"]
sorted_libraries_by_insert = common._sort_libraries_by_insert(sample_config)
if _prepare_folder_structure("abyss", assemblyDirectory) == 0:
os.chdir(assemblyDirectory)
else:
return sample_config
########### HERE IT START THE SPECIFIC ASSEMBLER PART
assembler_stdOut = open("abyss.stdOut", "a")
assembler_stdErr = open("abyss.stdErr", "a")
program=os.path.join(programBIN, "abyss-pe")
command = ""
command += "{} ".format(program)
threads = 8 # default for UPPMAX
if "threads" in sample_config :
threads = sample_config["threads"]
command += "np={} ".format(threads)
kmer = 54
if "kmer" in sample_config:
kmer = sample_config["kmer"]
command += "k={} ".format(kmer)
libraries = {}
for library, libraryInfo in sorted_libraries_by_insert:
read1 = libraryInfo["pair1"]
read2 = libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
if orientation=="innie" or orientation=="none":
if read2 is None:
# check if this is the first time I insert a se file
if "se" not in libraries:
libraries["se"] = "se=\'"
libraries["se"] = libraries["se"] + read1
else:
if not "lib" in libraries:
libraries["lib"] = {}
libName = insert # lib name is the insert size
if not libName in libraries["lib"]:
libraries["lib"][libName] = ""
libraries["lib"][libName] += "{} {} ".format(read1, read2)
else:
if not "mp" in libraries:
libraries["mp"] = {}
libName = format(insert)
if not libName in libraries["mp"]:
libraries["mp"][libName] = ""
libraries["mp"][libName] += "{} {} ".format(read1, read2)
#now create the command
command += "name={} ".format(outputName)
librariesSE = ""
librariesPE = ""
librariesMP = ""
if "se" in libraries:
libraries["se"] = libraries["se"] + "\'"
librariesSE = libraries["se"]
if "lib" in libraries:
lib="lib=\'"
for libPE, libPEreads in sorted(libraries["lib"].items()):
lib = lib + "lib{} ".format(libPE)
librariesPE += " lib{}=\'{}\' ".format(libPE,libPEreads)
lib=lib + "\' "
command += "{} ".format(lib)
if "mp" in libraries:
mp="mp=\'"
for libMP, libMPreads in sorted(libraries["mp"].items()):
mp = mp + "lib{} ".format(libMP)
librariesMP += " lib{}=\'{}\' ".format(libMP,libMPreads)
mp=mp + "\' "
command += "{} ".format(mp)
command += "{} ".format(librariesSE)
command += "{} ".format(librariesPE)
command += "{} ".format(librariesMP)
common.print_command(command)
if common.check_dryrun(sample_config):
os.chdir("..")
return sample_config
os.makedirs(os.path.join(assemblyDirectory, "runABySS"))
os.chdir("runABySS")
returnValue = 0
returnValue = subprocess.call(command, stdout=assembler_stdOut,
stderr=assembler_stdErr, shell=True)
os.chdir("..")
flags = sample_config.get("flags", [])
if returnValue == 0 and not common.check_dryrun(sample_config):
if os.path.exists(os.path.join("runABySS","{}-contigs.fa".format(
outputName))):
subprocess.call(["cp", os.path.join("runABySS",
"{}-contigs.fa".format(outputName)),
"{}.ctg.fasta".format(outputName) ])
subprocess.call(["cp", os.path.join("runABySS",
"{}-scaffolds.fa".format(outputName)),
"{}.scf.fasta".format(outputName) ])
if not "keep_tmp_files" in flags:
subprocess.call(["rm", "-r", "runABySS"])
elif not common.check_dryrun(sample_config):
print("something wrong with ABySS -> no contig file generated")
return sample_config
else:
print("ABySS terminated with an error. Please check running folder",
"for more informations")
os.chdir("..")
return sample_config
def _run_allpaths(global_config, sample_config, sorted_libraries_by_insert):
########## ACQUIRE ALL THE INFO AND CREATE THE ASSEMBLY FOLDER
assembler = "allpaths"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
# in abyss case there is no exectuable
programBIN = global_config["Tools"][assembler]["bin"]
program_options = global_config["Tools"][assembler]["options"]
sorted_libraries_by_insert = common._sort_libraries_by_insert(sample_config)
if _prepare_folder_structure("allpaths", assemblyDirectory) == 0:
os.chdir(assemblyDirectory)
else:
return sample_config
inGroups_file = open("in_groups.csv", "w")
inLibs_file = open("in_libs.csv", "w")
inGroups_file.write("group_name, library_name, file_name\n")
inLibs_file.write("library_name, project_name, organism_name, type, "
"paired, frag_size, frag_stddev, insert_size, insert_stddev, "
"read_orientation,genomic_start, genomic_end\n")
librariesForInLibs = []
librariesForInLibsDict = {}
group_name = 1;
for library, libraryInfo in sorted_libraries_by_insert:
read1 =libraryInfo["pair1"]
read2 =libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
if orientation=="innie":
path, fqfile=os.path.split(read1)
if "_1.fastq" in fqfile:
fqfile = fqfile.replace("_1.fastq", "_?.fastq")
elif "_R1_" in fqfile:
fqfile = fqfile.replace("_R1_", "_R?_")
else:
print("error file format not supported {}".format(fqfile))
return sample_config
inGroups_file.write("PE{}, lib{}, {}\n".format(group_name, insert,
os.path.join(path, fqfile)))
group_name += 1
if insert not in librariesForInLibsDict:
librariesForInLibsDict[insert] = insert
librariesForInLibs.append("lib{}, genome, genome, fragment, 1, "
"{}, {}, , , inward, 0, 0\n".format(insert,insert, std))
elif orientation=="outtie":
path, fqfile = os.path.split(read1)
if "_1.fastq" in fqfile:
fqfile = fqfile.replace("_1.fastq", "_?.fastq")
elif "_R1_" in fqfile:
fqfile = fqfile.replace("_R1_", "_R?_")
else:
print("error file format not supported {}".format(file))
return sample_config
inGroups_file.write("MP{}, lib{}, {}\n".format(group_name, insert,
os.path.join(path, fqfile)))
group_name += 1
if insert not in librariesForInLibsDict:
librariesForInLibsDict[insert] = insert
librariesForInLibs.append("lib{}, genome, genome, fragment, 1, "
", , {}, {}, outward, 0, 0\n".format(insert,insert, std))
else:
print("all paths support only innies and outties")
inGroups_file.close()
for lib in librariesForInLibs:
inLibs_file.write(lib)
inLibs_file.close()
#NOW RUN ALLPATHS FOR REAL
program=os.path.join(programBIN, "PrepareAllPathsInputs.pl")
os.mkdir("data_dir")
data_dir = os.path.join(os.getcwd(), "data_dir")
ploidy = "PLOIDY=1"
if len(program_options) > 0:
if len(program_options) >1:
print("Running ALlpaths only one parameter accepted as option",
"here: PLOIDY=2")
return sample_config
if program_options[0] == "PLOIDY=2":
ploidy = "PLOIDY=2"
else:
print("Running ALlpaths only one parameter accepted as option",
"here: PLOIDY=2")
return sample_config
command = [program , "DATA_DIR={}".format(data_dir), ploidy,
"PICARD_TOOLS_DIR={}".format(
global_config["Tools"]["picard"]["bin"]),
"FORCE_PHRED=True", "PHRED_64=False"]
if common.check_dryrun(sample_config):
common.print_command(command)
program = os.path.join(programBIN, "RunAllPathsLG")
command = [program, "PRE=.", "REFERENCE_NAME=.", "DATA_SUBDIR=data_dir",
"RUN=allpaths", "SUBDIR=run"]
common.print_command(command)
os.chdir("..")
return sample_config
assembler_stdOut = open("allpaths_PrepareAllPathsInputs.stdOut", "w")
assembler_stdErr = open("allpaths_PrepareAllPathsInputs.stdErr", "w")
common.print_command(command)
returnValue = subprocess.call(command, stdout=assembler_stdOut,
stderr=assembler_stdErr)
assembler_stdOut.close()
assembler_stdErr.close()
flags = sample_config.get("flags", [])
if returnValue == 0:
program = os.path.join(programBIN, "RunAllPathsLG")
command = [program, "PRE=.", "REFERENCE_NAME=.", "DATA_SUBDIR=data_dir",
"RUN=allpaths", "SUBDIR=run", "HAPLOIDIFY=True"]
common.print_command(command)
assembler_stdOut = open("allpaths_RunAllPathsLG.stdOut", "w")
assembler_stdErr = open("allpaths_RunAllPathsLG.stdErr", "w")
returnValue = subprocess.call(command, stdout=assembler_stdOut,
stderr=assembler_stdErr)
if returnValue != 0:
print("ALLPATHS RunAllPathsLG terminated with an error. Please",
"check running folder for more informations")
os.chdir("..")
return sample_config
else: # save results
assembly_dir = os.path.join("data_dir", "allpaths", "ASSEMBLIES",
"run")
if os.path.exists(os.path.join(assembly_dir,
"final.assembly.fasta")):
exit_code = subprocess.call(["cp", os.path.join(assembly_dir,
"final.contigs.fasta"), "{}.ctg.fasta".format(outputName)])
exit_code += subprocess.call(["cp", os.path.join(assembly_dir,
"final.assembly.fasta"), "{}.scf.fasta".format(outputName)])
if not "keep_tmp_files" in flags and exit_code == 0:
subprocess.call(["rm", "-r", "data_dir"])
else:
print("something wrong with Allpaths > no contig file generated")
os.chdir("..")
return sample_config
else:
print("ALLPATHS PrepareAllPathInputs terminated with an error. "
"Please check running folder for more informations")
os.chdir("..")
return sample_config
os.chdir("..")
return sample_config
def _run_cabog(global_config, sample_config, sorted_libraries_by_insert):
########## ACQUIRE ALL THE INFO AND CREATE THE ASSEMBLY FOLDER
assembler = "cabog"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
# in cabog case there is no exectuable
programBIN = global_config["Tools"][assembler]["bin"]
program_options = global_config["Tools"][assembler]["options"]
sorted_libraries_by_insert = common._sort_libraries_by_insert(sample_config)
if _prepare_folder_structure(assembler, assemblyDirectory) == 0:
os.chdir(assemblyDirectory)
else:
return sample_config
########### HERE IT START THE SPECIFIC ASSEMBLER PART
sys.path.insert(0, programBIN)
libraries = 1
for library, libraryInfo in sorted_libraries_by_insert:
command_fastqToCA = os.path.join(programBIN, "fastqToCA")
read1=libraryInfo["pair1"]
read2=libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
command_fastqToCA += " -libraryname "
command_fastqToCA += " {}_{}".format(outputName, libraries)
command_fastqToCA += " -insertsize "
command_fastqToCA += " {} {} ".format(insert,std)
command_fastqToCA += " -technology "
command_fastqToCA += " illumina "
command_fastqToCA += " -type "
command_fastqToCA += " illumina "
if orientation=="innie" or orientation=="none" :
command_fastqToCA += " -innie "
if read2 is None:
command_fastqToCA += " -reads "
command_fastqToCA += " {} ".format(read1)
else:
command_fastqToCA += " -mates "
command_fastqToCA += " {},{} ".format(read1, read2)
elif orientation=="outtie":
command_fastqToCA += " -outtie "
command_fastqToCA += " -mates "
command_fastqToCA += " {},{} ".format(read1, read2)
command_fastqToCA += " > "
command_fastqToCA += " {}_{}.frg ".format(outputName, libraries)
common.print_command(command_fastqToCA)
if not common.check_dryrun(sample_config):
cabog_stdOut = open("cabog_fastqToCA.stdOut", "w")
cabog_stdErr = open("cabogfastqToCA.stdErr", "w")
subprocess.call(command_fastqToCA, stderr=cabog_stdErr, shell=True)
cabog_stdOut.close()
cabog_stdErr.close()
libraries += 1
command_runCA = os.path.join(programBIN, "runCA")
command_runCA += " -d runCABOGfolder -p {} *frg".format(outputName)
common.print_command(command_runCA)
if common.check_dryrun(sample_config):
return sample_config
returnValue = 0
cabog_stdOut = open("cabog_runCA.stdOut", "w")
cabog_stdErr = open("cabog_runCA.stdErr", "w")
returnValue = subprocess.call(command_runCA, stdout=cabog_stdOut,
stderr=cabog_stdErr, shell=True)
flags = sample_config.get("flags", [])
if returnValue == 0:
#assembly succed, remove files and save assembly
if os.path.exists(os.path.join("runCABOGfolder","9-terminator",
"{}.ctg.fasta".format(outputName))):
subprocess.call(["cp", os.path.join("runCABOGfolder","9-terminator",
"{}.ctg.fasta".format(outputName)),
"{}.ctg.fasta".format(outputName)])
subprocess.call(["cp", os.path.join("runCABOGfolder","9-terminator",
"{}.scf.fasta".format(outputName)),
"{}.scf.fasta".format(outputName)])
if not "keep_tmp_files" in flags:
subprocess.call(["rm", "-r", "runCABOGfolder"])
else:
print("something wrong with CABOG -> no contig file generated")
else:
print("CABOG terminated with an error. Please check running folder",
"for more informations")
os.chdir("..")
return sample_config
def _run_masurca(global_config, sample_config,sorted_libraries_by_insert):
########## ACQUIRE ALL THE INFO AND CREATE THE ASSEMBLY FOLDER
assembler = "masurca"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
# in cabog case there is no exectuable
programBIN = global_config["Tools"][assembler]["bin"]
program_options = global_config["Tools"][assembler]["options"]
sorted_libraries_by_insert = common._sort_libraries_by_insert(sample_config)
if _prepare_folder_structure(assembler, assemblyDirectory) == 0:
os.chdir(assemblyDirectory)
else:
return sample_config
########### HERE IT START THE SPECIFIC ASSEMBLER PART
masurca_config_file = open("configuration.txt", "w")
masurca_config_file.write("DATA\n")
allTheLetters = string.lowercase
libraryPE = "p"
libraryPEnum = 0
libraryMP = "m"
libraryMPnum = 0
#TODO: single ended reads
for library, libraryInfo in sorted_libraries_by_insert:
read1=libraryInfo["pair1"]
read2=libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
if orientation=="innie":
if read2 is not None:
configurationLine = "PE = {}{} {} {} {} {}".format(libraryPE,
allTheLetters[libraryPEnum], insert, std, read1, read2)
masurca_config_file.write("{}\n".format(configurationLine))
libraryPEnum += 1
#TODO: check when more than 21 PE libraries ae specified
elif orientation=="outtie":
configurationLine = "JUMP = {}{} {} {} {} {}".format(libraryMP,
allTheLetters[libraryMPnum], insert, std, read1, read2)
masurca_config_file.write("{}\n".format(configurationLine))
libraryMPnum += 1
#TODO: check when more than 21 PE libraries ae specified
masurca_config_file.write("END\n")
masurca_config_file.write("\n")
masurca_config_file.write("PARAMETERS\n")
#this is k-mer size for deBruijn graph values between 25 and 101 are
#supported, auto will compute the optimal size based on the read data
#and GC content
masurca_config_file.write("GRAPH_KMER_SIZE=auto\n")
#set this to 1 for Illumina-only assemblies and to 0 if you have 2x or
#more long (Sanger, 454) reads
masurca_config_file.write("USE_LINKING_MATES=1\n")
#this parameter is useful if you have too many jumping library mates.
#See manual for explanation about settings based on genome length
if sample_config["genomeSize"] > 10000000:
masurca_config_file.write("LIMIT_JUMP_COVERAGE = 1000\n")
else:
masurca_config_file.write("LIMIT_JUMP_COVERAGE = 60\n")
#these are the additional parameters to Celera Assembler. do not worry
#about performance, number or processors or batch sizes -- these are
#computed automatically. for mammals do not set cgwErrorRate above 0.15!!!
if sample_config["genomeSize"] > 1500000000:
masurca_config_file.write("CA_PARAMETERS = ovlMerSize=30 \
cgwErrorRate=0.15 ovlMemory=4GB\n")
else:
masurca_config_file.write("CA_PARAMETERS = ovlMerSize=30 \
cgwErrorRate=0.25 ovlMemory=4GB\n")
#auto-detected number of cpus to use
threads = 8 # default for UPPMAX
if "threads" in sample_config :
threads = sample_config["threads"]
masurca_config_file.write("NUM_THREADS= {}\n".format(threads))
#this is mandatory jellyfish hash size ---- jellyfish hash size,
#set this to about 10x the genome size.
JF_SIZE = sample_config["genomeSize"] * 11
masurca_config_file.write("JF_SIZE={}\n".format(JF_SIZE))
#this specifies if we do (1) or do not (0) want to trim long runs of
#homopolymers (e.g. GGGGGGGG) from 3' read ends, use it for high GC genomes
masurca_config_file.write("DO_HOMOPOLYMER_TRIM=0\n")
masurca_config_file.write("END\n")
masurca_config_file.write("\n")
masurca_config_file.close()
if common.check_dryrun(sample_config):
os.chdir("..")
return sample_config
masurca_stdOut = open("masurca.stdOut", "w")
masurca_stdErr = open("masurca.stdErr", "w")
os.mkdir("runMASURCA")
os.chdir("runMASURCA")
command = [os.path.join(programBIN,"bin/masurca") , "../configuration.txt"]
common.print_command(command)
subprocess.call(command, stdout=masurca_stdOut, stderr=masurca_stdErr)
if not os.path.exists("assemble.sh"):
print("MaSuRCA: assemble.sh not created. Unknown failure")
return sample_config
command = ["./assemble.sh"]
common.print_command(command)
returnValue = subprocess.call(command, stdout=masurca_stdOut,
stderr=masurca_stdErr)
os.chdir("..")
flags = sample_config.get("flags", [])
if returnValue == 0:
if os.path.exists(os.path.join(
"runMASURCA","CA/10-gapclose/genome.scf.fasta")):
subprocess.call(["cp", os.path.join(
"runMASURCA","CA/10-gapclose/genome.ctg.fasta"),
"{}.ctg.fasta".format(outputName) ])
subprocess.call(["cp", os.path.join(
"runMASURCA","CA/10-gapclose/genome.scf.fasta"),
"{}.scf.fasta".format(outputName) ])
if not "keep_tmp_files" in flags:
subprocess.call(["rm", "-r", "runMASURCA"])
else:
print("something wrong with MaSuRCA -> no contig file generated")
else:
print("MaSuRCA terminated with an error. Please check running folder",
"for more informations")
return sample_config
os.chdir("..")
return sample_config
def _run_soapdenovo(global_config, sample_config, sorted_libraries_by_insert):
########## ACQUIRE ALL THE INFO AND CREATE THE ASSEMBLY FOLDER
assembler = "soapdenovo"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
# in cabog case there is no exectuable
programBIN = global_config["Tools"][assembler]["bin"]
program_options = global_config["Tools"][assembler]["options"]
sorted_libraries_by_insert = common._sort_libraries_by_insert(sample_config)
if _prepare_folder_structure(assembler, assemblyDirectory) == 0:
os.chdir(assemblyDirectory)
else:
return sample_config
########### HERE IT START THE SPECIFIC ASSEMBLER PART
kmer = 54
if "kmer" in sample_config:
kmer = sample_config["kmer"]
threads = ["-p", "8"] # default for UPPMAX
if "threads" in sample_config:
threads = ["-p", "{}".format(sample_config["threads"])]
soap_config_file = open("configuration.txt", "w")
soap_config_file.write("max_rd_len=100\n")
#TODO make this a parameter in the options
rank = 1
for library, libraryInfo in sorted_libraries_by_insert:
soap_config_file.write("[LIB]\n")
read1 = libraryInfo["pair1"]
read2 = libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
soap_config_file.write("avg_ins={}\n".format(insert))
soap_config_file.write("rank={}\n".format(rank))
rank += 1
soap_config_file.write("map_len=30\n")
if orientation=="innie" or orientation=="none":
soap_config_file.write("asm_flags=3\n")
soap_config_file.write("pair_num_cutoff=3\n")
soap_config_file.write("reverse_seq=0\n")
if read2 is None:
soap_config_file.write("q={}\n".format(read1))
else:
soap_config_file.write("q1={}\n".format(read1))
soap_config_file.write("q2={}\n".format(read2))
elif orientation=="outtie":
soap_config_file.write("asm_flags=2\n")
soap_config_file.write("pair_num_cutoff=5\n")
soap_config_file.write("reverse_seq=1\n")
soap_config_file.write("q1={}\n".format(read1))
soap_config_file.write("q2={}\n".format(read2))
soap_config_file.close()
assembler_stdOut = open("soap.stdOut", "w")
assembler_stdErr = open("soap.stdErr", "w")
os.makedirs(os.path.join(assemblyDirectory, "runSOAP"))
os.chdir("runSOAP")
#TODO : lots of missing options
command = [programBIN , "all", "-s", "../configuration.txt", "-K",
"{}".format(kmer), "-L", "500", "-o", "soapAssembly", threads[0],
threads[1] ]
common.print_command(command)
returnValue = 0
if not common.check_dryrun(sample_config):
subprocess.call(command, stdout=assembler_stdOut,
stderr=assembler_stdErr)
else:
os.chdir("..")
os.chdir("..")
return sample_config
os.chdir("..")
flags = sample_config.get("flags", [])
if returnValue == 0:
if(os.path.exists(os.path.join("runSOAP","soapAssembly.scafSeq"))):
subprocess.call(["cp", os.path.join("runSOAP",
"soapAssembly.scafSeq"), "{}.scf.fasta".format(outputName)])
subprocess.call(["cp", os.path.join("runSOAP",
"soapAssembly.contig"), "{}.ctg.fasta".format(outputName)])
if not "keep_tmp_files" in flags:
subprocess.call(["rm", "-r", "runSOAP"])
else:
print("something wrong with SOAPdenovo -> no contig file generated")
else:
print("SOAPdenovo terminated with an error. Please check running",
"folder for more informations")
os.chdir("..")
return sample_config
os.chdir("..")
return sample_config
def _run_spades(global_config, sample_config, sorted_libraries_by_insert):
########## ACQUIRE ALL THE INFO AND CREATE THE ASSEMBLY FOLDER
assembler = "spades"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
# in cabog case there is no exectuable
programBIN = global_config["Tools"][assembler]["bin"]
program_options = global_config["Tools"][assembler]["options"]
sorted_libraries_by_insert = common._sort_libraries_by_insert(sample_config)
if _prepare_folder_structure(assembler, assemblyDirectory) == 0:
os.chdir(assemblyDirectory)
else:
return sample_config
########### HERE IT START THE SPECIFIC ASSEMBLER PART
command = ""
command += "{} ".format(programBIN)
for option in program_options:
command += "{} ".format(option)
#creates the command on-the-fly
peLibrary = 1
mpLibrary = 1
for library, libraryInfo in sorted_libraries_by_insert:
read1 = libraryInfo["pair1"]
read2 = libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
if orientation=="innie" or orientation=="none":
if read2 is None:
command += "--pe{}-s {} ".format(peLibrary, read1)
else:
command += "--pe{}-1 {} --pe{}-2 {} ".format(peLibrary, read1,
peLibrary, read2)
peLibrary += 1
elif orientation=="outtie":
command += "--mp{}-1 {} --mp{}-2 {} ".format(mpLibrary, read1,
mpLibrary, read2)
mpLibrary += 1
else:
print("orientation{} not supported.... why the program did not",
"failed earlier?".format(orientation))
command += "-o {} ".format(outputName)
common.print_command(command)
returnValue = 0
if not common.check_dryrun(sample_config):
assembler_stdOut = open("spades.stdOut", "a")
assembler_stdErr = open("spades.stdErr", "a")
returnValue = subprocess.call(command, stdout=assembler_stdOut,
stderr=assembler_stdErr, shell=True)
else:
return sample_config
flags = sample_config.get("flags", [])
if returnValue == 0:
if os.path.exists(os.path.join(outputName,"contigs.fasta")):
subprocess.call(["cp", os.path.join(outputName,"contigs.fasta"),
"{}.ctg.fasta".format(outputName)])
subprocess.call(["cp", os.path.join(outputName,"scaffolds.fasta"),
"{}.scf.fasta".format(outputName)])
if not "keep_tmp_files" in flags:
subprocess.call(["rm", "-r", outputName])
else:
print("something wrong with SPADES -> no contig file generated")
else:
print("SPADES terminated with an error. Please check running folder",
"for more informations")
os.chdir("..")
return sample_config
def _run_trinity(global_config, sample_config, sorted_libraries_by_insert):
print("running trinity ...")
assembler = "trinity"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
if common.directory_exists(assemblyDirectory):
return sample_config
os.chdir(assemblyDirectory) # now I am in the assembly directory
sorted_libraries_by_insert = common.prepare_folder_structure(
sorted_libraries_by_insert)
# in masurca case there is no exectuable as a make file must be created
programBIN = global_config["Tools"][assembler]["bin"] + "Trinity"
program_options = global_config["Tools"][assembler]["options"]
if assembler in sample_config:
program_options=sample_config[assembler]
########### HERE IT START THE SPECIFIC ASSEMBLER PART
command = [programBIN]
command.extend(["--seqType", "fq"])
command.extend(["--JM", "100G"])
if "threads" in sample_config:
command.extend(["--CPU", str(sample_config["threads"])])
for library, libraryInfo in sorted_libraries_by_insert:
read1 = libraryInfo["pair1"]
read2 = libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
if read2 is None:
command.append("--single")
command.append("{}".format(read1))
elif orientation=="innie":
command.append("--left")
command.append("{}".format(read1))
command.append("--right")
command.append("{}".format(read2))
else:
print("trinity: somthing wrong or unexpected in the sample",
"config file")
return sample_config
command.extend(["--output", "trinity"])
assembler_stdOut = open("trinity.stdOut", "w")
assembler_stdErr = open("trinity.stdErr", "w")
print(" ".join(command))
returnValue = subprocess.call(" ".join(command), stdout=assembler_stdOut,
stderr=assembler_stdErr, shell=True)
# now align reads back to transcripts and estimate abundance
os.chdir("trinity")
programBIN = global_config["Tools"][assembler]["bin"] + \
"util/align_and_estimate_abundance.pl"
command = [programBIN]
command.extend(["--transcripts", "Trinity.fasta"])
command.extend(["--seqType", "fq"])
for library, libraryInfo in sorted_libraries_by_insert:
read1 = libraryInfo["pair1"]
read2 = libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
if read2 is not None and orientation == "innie":
command.append("--left")
command.append("{}".format(os.path.splitext(read1)[0]))
command.append("--right")
command.append("{}".format(os.path.splitext(read2)[0]))
command.extend(["--aln_method", "bowtie"])
command.extend(["--est_method", "RSEM"])
command.append("--debug")
command.append("--trinity_mode")
command.append("--prep_reference")
if "threads" in sample_config:
command.extend(["--thread_count", str(sample_config["threads"])])
print(" ".join(command))
returnValue = subprocess.call(" ".join(command), stdout=assembler_stdOut,
stderr=assembler_stdErr, shell=True)
#now copy results
os.chdir("..")
subprocess.call(["cp", "trinity/Trinity.fasta",
"{}.fasta".format(outputName)])
subprocess.call(["cp", "trinity/RSEM.isoforms.results",
"{}.isoforms.results".format(outputName)])
subprocess.call(["cp", "trinity/RSEM.genes.results",
"{}.genes.results".format(outputName)])
os.chdir(currentDirectory)
return sample_config
def _prepare_folder_structure(assembler,assemblyDirectory):
if common.directory_exists(assemblyDirectory):
print("Assembler {} asumer already computed as folder {} exists".format(
assembler,assemblyDirectory))
return 1
return 0
def _run_abyss_mergePairs(global_config, sample_config,
sorted_libraries_by_insert):
print("running abyss-mergepairs ...")
assembler = "abyss_mergePairs"
outputName = sample_config["output"]
currentDirectory = os.getcwd()
assemblyDirectory = os.path.join(currentDirectory, assembler)
if common.directory_exists(assemblyDirectory):
return sample_config
os.chdir(assemblyDirectory) # now I am in the assembly directory
sorted_libraries_by_insert = common.prepare_folder_structure(
sorted_libraries_by_insert)
# in abyss case there is no exectuable
programBIN = global_config["Tools"][assembler]["bin"]
program_options = global_config["Tools"][assembler]["options"]
if assembler in sample_config:
program_options=sample_config[assembler]
########### HERE IT START THE SPECIFIC ASSEMBLER PART
program=programBIN
command = []
command.append(program)
for option in program_options:
command.append(option)
libraries = {}
for library, libraryInfo in sorted_libraries_by_insert:
read1 = libraryInfo["pair1"]
read2 = libraryInfo["pair2"]
orientation = libraryInfo["orientation"]
insert = libraryInfo["insert"]
std = libraryInfo["std"]
outputNameArray = read1.split('/')[-1].split('_')
outputName = "{}_{}".format(outputNameArray[0], outputNameArray[1])
if orientation=="innie":
if read2 is not None:
currentCommand = command;
currentCommand.append('-o')
currentCommand.append(outputName)
currentCommand.append(read1)
currentCommand.append(read2)
abyss_stdOut = open("mergePairs_{}.stdOut".format(outputName),
"a")
abyss_stdErr = open("mergePairs_{}.stdErr".format(outputName),
"a")
print(command)
subprocess.call(command, stdout=abyss_stdOut,
stderr=abyss_stdErr)
command_mv = ["mv", "mergePairs_{}.stdErr".format(outputName),
"{}.txt".format(outputName)]
subprocess.call(command_mv)
os.chdir("..")
return sample_config