forked from MatteoSchiavinato/manticore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanticore
executable file
·1726 lines (1324 loc) · 52.2 KB
/
manticore
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/env python3
# version
VERSION = '''
MANTICORE
v 1.0.1
--------------------------------------------------------
Matteo Schiavinato, Alexandrina Bodrug
BOKU - University of Natural Resources and Life Sciences
Department of Biotechnology
Institute of Computational Biology
Vienna (AT)
--------------------------------------------------------
[email protected]:MatteoSchiavinato/manticore.git
'''
# modules
import pandas as pd
import dask
import dask.dataframe as dd
import pysam
import argparse as ap
import time
from time import asctime as at
import sys
import os
from shutil import copyfile
import os.path
from operator import itemgetter
from itertools import combinations
import subprocess
from math import floor, ceil
from Bio import SeqIO
import resource
start_time = time.time()
### help ###
if len(sys.argv) == 1:
sys.argv.append("-h")
if sys.argv[1] == "-h":
sys.exit('''{0}
### This is a quick help. For complete help use [--help], or browse the program manual ###
Usage:
manticore \\
--species-name STRING \\
--reads-type PE \\
--reads PATH1,PATH2 PATH3,PATH4 \\
(parentA) (parentB)
--names STRING STRING \\
(A) (B)
--reference PATH \\
--output-dir PATH \\
[ ... other options ... ]
[INPUT OPTIONS]
--species-name Label for output files and plot titles [required]
--reads FASTQ read files, see --help for how to structure [required]
--reads-type Type of the input reads (PE|SE) [PE]
--names Names of read files, see --help for how to structure [required]
--reference Hybrid genome in FASTA format [required]
--output-dir Output directory [required]
--region-beds Regions to use in analysis, see --help for details [-]
--region-names Names to use for each file in --region-beds [-]
[COVERAGE ANALYSIS]
--window-size Size of the intermixing windows (in bp) [500000]
--n-breaks Number of window breaks for Jaccard distance [10]
--min-feat-length Min. window positions contained in a --region-bed [1000]
--min-frac-pos Min. fraction of positions covered [0.1]
--min-cov-pos Min. number of window positions with coverage [1000]
--min-cov Min. coverage to retain position [1]
[TABLES & PLOTTING]
--exclude Sequence names to exclude from the analysis [-]
--max-jacc-uniq Max. Jaccard distance for uniquely assigned window [0.5]
--max-plot-cov Upper limit for coverage plot [100]
[MISCELLANEOUS]
--max-mem Gigabytes of RAM allowed [4G]
--threads Number of parallel threads [1]
--cleanup Delete heavy intermediate files at the end [off]
--filter-reference Don't process sequences shorter than --window-size [off]
--isize-read-num Read pairs to use in insert size distribution [10000]
--isize-dist-width Width of the allowed insert size range [150]
--hisat2-path Path to hisat2 executable [hisat2]
--hisat2-map-pars Mapping parameters passed to hisat2 [--score-min L,0.0,-0.6
(consult hisat2 help) --no-softclip
--no-spliced-alignment]
--samtools-filters Arguments to pass to samtools view [-F 0x4 -F 0x0100]
--rscript-path Path to Rscript executable [Rscript]
--version Print version of Manticore and exit [off]
--restart Restart whole pipeline from the beginning [off]
'''.format(VERSION))
if sys.argv[1] in ["-h", "--help", "-help", "getopt", "usage"]:
sys.exit('''
{0}
Usage:
manticore \\
--species-name STRING \\
--reads PATH1,PATH2 PATH3,PATH4 \\
(parentA) (parentB)
--names STRING STRING \\
(A) (B)
--reference PATH \\
--output-dir PATH \\
[ ... other options ... ]
[INPUT OPTIONS]
--species-name Name of the hybrid species studied, to be used for plotting
(No whitespaces)
[required]
--reads SPACE-separated list of read files (separate files of read pairs by comma)
Example: --reads G1.read_1.fq,G1.read_2.fq G2.read_1.fq,G2.read_2.fq ...
[required]
--reads-type Specify either 'PE' for paired-end reads or 'SE' for single-end reads.
Note that this affects the [--reads] argument too, since in case of
single-end reads you will have to specify only one file per parent
[PE]
--names SPACE-separated list of names corresponding to read files
Example: --names Subgenome_A Subgenome_B Subgenome_C ...
[required]
--reference FASTA file where to assess subgenomic intermixing
[required]
--output-dir All files will be generated within this directory
[required]
--region-beds SPACE-separated list of BED files with regions that have to be considered
(A separate coverage analysis will be generated for each one)
[-]
--region-names SPACE-separated list of names to associate to BED files which should
define the type of data they contain
(Example: \'CDS\', \'nonrep\', ...)
(These names are only used as labels)
[-]
[COVERAGE ANALYSIS]
--window-size Size of the windows on which to study intermixing
(Hint: the larger, the more intermixing is observed, but the more data is lost)
[500000]
--n-breaks Number of window breaks to compute Jaccard distance from.
[--window-size] divided by [--n-breaks] has to return an integer
(Hint: the smaller, the more likely intermixing is observed)
[10]
--min-feat-length Minimum length of annotated features within windows of size --window-size.
This applies SEPARATELY to all annotations provided with --region-beds.
[1000]
--min-frac-pos Minimum fraction of positions that have to be covered within the --min-feat-length
of a window.
[0.1]
--min-cov-pos Minimum number of positions that have to be covered within a window.
[1000]
--min-cov Minimum position coverage to consider a position in the analysis
[1]
[TABLES & PLOTTING]
--exclude Sequence names to exclude from the analysis. The result tables will be generated for
all sequences, and then they will be filtered removing lines (i.e. windows) corresponding
to these sequences (Hint: remove sequenecs that are not clearly assigned to a subgenome).
[off]
--max-jacc-uniq Genome intervals are assigned to one parent specifically (i.e. low intermixing regions)
based on the Jaccard distance produced in the interval. Intervals producing a
Jaccard distange > [--max-jacc-uniq] are not considered low-intermixing.
(Hint: This value ranges from 0 to 1)
[0.5]
--max-plot-cov The final plots will be limited to this maximum coverage
[100]
[MISCELLANEOUS]
--max-mem Maximum memory that can be used by the program (use only \'#G\')
(N threads will get \'--maxmem / N\' memory each)
[4G]
--threads Number of parallel threads
[4]
--cleanup Heavy intermediate files are deleted when the program has finished
[off]
--filter-reference The reference file is filtered, keeping only sequences longer than the
specified --window-size
[off]
--isize-read-num Number of read pairs from which to estimate the insert size distribution
[10000]
--isize-dist-width Width of the allowed insert size range when mapping
(-I <x-N> -X <x+N> where x=avg.TLEN from mapping of --isize-read-num reads)
[150]
--hisat2-path Path to the \"HISAT2\" executable (only specify if not present in the `$PATH`)
[hisat2]
--hisat2-map-pars Mapping parameters to be passed to HISAT2
[-k 5 --score-min L,0.0,-0.6 --mp 6,2 --rdg 5,3 --rfg 5,3 --no-softclip --no-spliced-alignment]
--samtools-filters Arguments to pass to samtools view
[-F 0x0100 -F 0x4]
--rscript-path Path to the \"Rscript\" executable (only specify if not present in the `$PATH`)
[Rscript]
--version Print version of the program and exit
[off]
--restart Restart whole pipeline from the beginning, ignoring *.done files
(each directory contains a *.done file that signals to skip the step)
[off]
'''.format(VERSION))
p = ap.ArgumentParser()
# mandatory
p.add_argument("--species-name", type=str, required=True)
p.add_argument("--reads", nargs="*", type=str, required=True)
p.add_argument("--names", nargs="*", type=str, required=True)
p.add_argument("--reference", type=str, required=True)
p.add_argument("--output-dir", type=str, required=True)
# optional
p.add_argument("--reads-type", choices=["SE", "PE"], default="PE", type=str)
p.add_argument("--version", action="store_true", default=False)
p.add_argument("--restart", action="store_true", default=False)
p.add_argument("--filter-reference", action="store_true", default=False)
p.add_argument("--max-mem", type=str, default="4G")
p.add_argument("--threads", type=int, default=4)
p.add_argument("--cleanup", action="store_true", default=False)
# mapping
p.add_argument("--hisat2-map-pars", default="-k 5 --score-min L,0.0,-0.6 --mp 6,2 --rdg 5,3 --rfg 5,3 --no-softclip --no-spliced-alignment")
p.add_argument("--isize-read-num", default=10000, type=int)
p.add_argument("--isize-dist-width", default=150, type=int)
p.add_argument("--samtools-filters", default="-F 0x0100 -F 0x4", type=str)
# analysis
p.add_argument("--region-beds", nargs="*", type=str)
p.add_argument("--region-names", nargs="*", type=str)
p.add_argument("--window-size", default=500000, type=int)
p.add_argument("--n-breaks", default=10, type=int)
p.add_argument("--min-feat-length", default=1000, type=int)
p.add_argument("--min-frac-pos", default=0.1, type=float)
p.add_argument("--min-cov-pos", default=1000, type=int)
p.add_argument("--min-cov", type=int, default=1)
p.add_argument("--exclude", nargs="*", default=[])
p.add_argument("--max-jacc-uniq", type=float, default=0.5)
p.add_argument("--max-plot-cov", type=int, default=100)
# paths
p.add_argument("--hisat2-path", type=str, default="hisat2")
p.add_argument("--rscript-path", type=str, default="Rscript")
args = p.parse_args()
### functions ###
def memory_limit(user_limit):
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
hard_limit = min(float(hard), float(user_limit))
resource.setrlimit(resource.RLIMIT_AS, (get_memory() * 1024 / 2, hard_limit))
def get_memory():
with open('/proc/meminfo', 'r') as mem:
free_memory = 0
for i in mem:
sline = i.split()
if str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'):
free_memory += int(sline[1])
return free_memory
def run_cmd(cmd, stdout_file, stderr_file):
STDOUT_FILE = open(stdout_file, "w")
STDERR_FILE = open(stderr_file, "w")
code = subprocess.call(cmd, stdout=STDOUT_FILE, stderr=STDERR_FILE)
STDOUT_FILE.close()
STDERR_FILE.close()
return code
def run_pipeline(cmd, stdout_file, stderr_file):
STDOUT_FILE = open(stdout_file, "w")
STDERR_FILE = open(stderr_file, "w")
code = subprocess.call(cmd, stdout=STDOUT_FILE, stderr=STDERR_FILE, shell=True)
STDOUT_FILE.close()
STDERR_FILE.close()
return code
def link_file(source, destination):
cmd = [ "ln",
"-s",
source,
destination ]
code = subprocess.call(cmd)
return code
def unlink_file(file_path):
cmd = [ "unlink",
file_path ]
code = subprocess.call(cmd)
return code
def filter_reference(outdir, reference, window_size):
INPUT = open(reference, "r")
OUTPUT = open("{0}/genome.fa".format(outdir), "w")
ERROR = open("{0}/genome.fa.stderr".format(outdir), "w")
retained = 0
discarded = 0
for record in SeqIO.parse(INPUT, "fasta"):
if len(record.seq) >= int(window_size):
OUTPUT.write(">" + str(record.id) + "\n" + str(record.seq) + "\n")
retained += len(record.seq)
else:
discarded += len(record.seq)
total = retained + discarded
retained_frac = float(retained) / float(total) * 100
discarded_frac = float(discarded) / float(total) * 100
ERROR.write("Retained: {0} bp ({1}%)\nDiscarded: {2} bp ({3}%)\n".format( retained,
retained_frac,
discarded,
discarded_frac ) )
return True
def build_hisat2_indexes(outdir, threads, hisat2_path, cwd):
# build indexes for hisat2 for the genome that is linked in the folder
# if exist
cmd = [ "{0}-build".format(hisat2_path),
"-p {0}".format(threads),
"{0}/genome.fa".format(outdir),
"{0}/genome.fa".format(outdir) ]
code = run_cmd( cmd,
"{0}/build_hisat2_indexes.stdout".format(outdir),
"{0}/build_hisat2_indexes.stderr".format(outdir) )
if code == 0:
return True
else:
return False
def generate_reads_dictionary(reads_arg, names_arg):
Reads = {}
k=0
while (k <= len(reads_arg)-1):
entry = reads_arg[k]
name = names_arg[k]
x = entry.split(",")
Reads[name] = [ read_file for read_file in x ]
k += 1
return Reads
def generate_beds_dictionary(beds_arg, names_arg, cwd):
Beds = {}
k=0
while (k <= len(beds_arg)-1):
entry = beds_arg[k]
if entry[0:1] != "/":
entry = str(cwd) + "/" + entry
name = names_arg[k]
Beds[name] = entry
k += 1
return Beds
def link_read_files(Reads, outdir):
for name in Reads:
if len(Reads[name]) == 1:
code = link_file(Reads[name][0], "{0}/{1}.fastq".format(outdir, name))
elif len(Reads[name]) == 2:
code = link_file(Reads[name][0], "{0}/{1}.1.fastq".format(outdir, name))
code = link_file(Reads[name][1], "{0}/{1}.2.fastq".format(outdir, name))
else:
sys.exit("ERROR: more than two files specified for {0}:\n{1}\n\n".format(name,
Reads[name]))
return True
def estimate_insert_size(hisat2_index, Reads, mapping_pars, hisat2_path, R_path, \
isize_reads, dist_width, outdir, threads, script_dir):
Insert_sizes = { name:0 for name in Reads.keys() }
open("{0}/isize_ranges.table".format(outdir), "w").close()
open("{0}/peak_insert_size.stdout".format(outdir), "w").close()
open("{0}/peak_insert_size.stderr".format(outdir), "w").close()
if os.path.exists("{0}.1.ht2l".format(hisat2_index)) == True:
index_type = "--large-index"
else:
index_type = ""
for name in Reads.keys():
# map reads
read_1 = Reads[name][0]
read_2 = Reads[name][1]
cmd = [ "{0}".format(hisat2_path),
index_type,
"-p {0}".format(threads),
"--upto {0}".format(isize_reads),
mapping_pars,
"-I", str(0),
"-X", str(2000),
"-x {0}".format(hisat2_index),
"-1 {0}".format(read_1),
"-2 {0}".format(read_2),
"-S {0}/{1}.isize_est.sam".format(outdir, name) ]
cmd = " ".join(cmd)
code = run_pipeline( cmd,
"{0}/peak_insert_size.stdout".format(outdir),
"{0}/peak_insert_size.stderr".format(outdir) )
if code != 0:
break
# extract average TLEN
INPUT = open("{0}/{1}.isize_est.sam".format(outdir, name), "r")
Values = [ line.rstrip("\n\r\b").split("\t") for line in INPUT if line[0:1] != "#" ]
Values = [ lst for lst in Values if len(lst) >= 9 ]
Values = [ int(lst[8]) for lst in Values ]
INPUT.close()
Values = [ float(x) for x in Values if ((float(x) > 0) and (float(x) <= 1000)) ]
OUTPUT = open("{0}/{1}.isize_est.sam.tlen".format(outdir, name), "w")
for x in Values:
OUTPUT.write(str(x) + "\n")
OUTPUT.close()
cmd = [ "{0}".format(R_path),
"{0}/scripts/plot-insert-size.Rscript".format(script_dir),
"{0}/{1}.isize_est.sam.tlen".format(outdir, name) ]
code = run_cmd( cmd,
"{0}/{1}.isize_est.sam.tlen.stdout".format(outdir, name),
"{0}/{1}.isize_est.sam.tlen.stderr".format(outdir, name) )
if code != 0:
break
isize_avg = float(sum(Values)) / float(len(Values))
isize_low = str(max(floor(float(isize_avg) - float(dist_width)), 0))
isize_high = str(ceil(float(isize_avg) + float(dist_width)))
OUTPUT = open("{0}/isize_ranges.table".format(outdir), "a")
OUTPUT.write("\t".join([name, isize_low, isize_high]) + "\n")
OUTPUT.close()
if code == 0:
return True
else:
return False
def check_if_reads_were_mapped(outdir, name):
if os.path.exists("{0}/{1}.sam".format(outdir, name)) and \
os.path.exists("{0}/{1}.sam.stderr".format(outdir, name)):
INPUT = open("{0}/{1}.sam.stderr".format(outdir, name), "r")
try:
tail = [ line.rstrip("\n\b\r") for line in INPUT ][-1]
except IndexError:
tail = ""
INPUT.close()
if ("overall alignment rate" in tail):
return True
else:
return False
else:
return False
def map_reads( hisat2_index, Reads, reads_type, mapping_pars, \
hisat2_path, isize_file, outdir, threads):
if os.path.exists("{0}.1.ht2l".format(hisat2_index)) == True:
index_type = "--large-index"
else:
index_type = ""
if reads_type == "PE":
# read insert sizes
INPUT = open(isize_file, "r")
Lsts = [ line.rstrip("\n\b\r").split("\t") for line in INPUT ]
INPUT.close()
Insert_sizes = { x[0]:(str(x[1]), str(x[2])) for x in Lsts }
for name in Reads.keys():
status = check_if_reads_were_mapped(outdir, name)
if status == False:
cmd = [ "{0}".format(hisat2_path),
index_type,
"-p {0}".format(threads),
mapping_pars,
"--minins {0} --maxins {1}".format( Insert_sizes[name][0],
Insert_sizes[name][1] ),
"-x {0}".format(hisat2_index),
"-1 {0}".format(Reads[name][0]),
"-2 {0}".format(Reads[name][1]),
"-S {0}/{1}.sam".format(outdir, name) ]
cmd = " ".join(cmd)
code = run_pipeline( cmd,
"{0}/{1}.sam.stdout".format(outdir, name),
"{0}/{1}.sam.stderr".format(outdir, name) )
if code != 0:
break
elif reads_type == "SE":
for name in Reads.keys():
cmd = [ "{0}".format(hisat2_path),
index_type,
"-p {0}".format(threads),
mapping_pars,
"-x {0}".format(hisat2_index),
"-U {0}".format(Reads[name][0]),
"-S {0}/{1}.sam".format(outdir, name) ]
cmd = " ".join(cmd)
code = run_pipeline( cmd,
"{0}/{1}.sam.stdout".format(outdir, name),
"{0}/{1}.sam.stderr".format(outdir, name) )
if code != 0:
break
if code == 0:
return True
else:
return False
def filter_reads(samtools_filters, Reads, outdir, threads, thread_mem):
for name in Reads.keys():
code = 1
if os.path.exists("{0}/{1}.filtered.ok".format(outdir, name)) == False:
# filter
cmd = "-h -b {0} -@ {1} -o {2}/{3}.f.bam {2}/{3}.sam".format( samtools_filters,
threads,
outdir,
name )
errfile = "{0}/{1}.f.bam.stderr".format(outdir, name)
cmd = cmd.split(" ")
# run filtering
tmp = pysam.view(*cmd, catch_stdout=False, save_stderr=errfile)
open("{0}/{1}.filtered.ok".format(outdir, name), "w").close()
if os.path.exists("{0}/{1}.sorted.ok".format(outdir, name)) == False:
# sort
cmd = "-@ {0} -m {1} -O bam -T {2}/{3} -o {2}/{3}.fs.bam {2}/{3}.f.bam".format(
threads,
thread_mem,
outdir,
name )
errfile = "{0}/{1}.fs.bam.stderr".format(outdir, name)
cmd = cmd.split(" ")
# run sort
tmp = pysam.sort(*cmd, catch_stdout=False, save_stderr=errfile)
open("{0}/{1}.sorted.ok".format(outdir, name), "w").close()
code = 0
if code == 0:
return True
else:
return False
def extract_coverage(Beds, Reads, outdir, mapdir):
code = 0
for name in Reads.keys():
if len(Beds) > 0:
for region in Beds.keys():
outfile = "{0}/{1}.{2}.depth".format(outdir, name, region)
errfile = "{0}/{1}.{2}.depth.stderr".format(outdir, name, region)
OUTPUT = open(outfile, "w")
ERROR = open(errfile, "w")
code = 0
cmd = [ "-b",
"{0}".format(Beds[region]),
"{0}/{1}.fs.bam".format(mapdir, name) ]
try:
pysam.depth(*cmd, save_stdout=outfile, save_stderr=errfile)
except:
code = 1
OUTPUT.close()
ERROR.close()
if code != 0:
break
else:
cmd = [ "{0}/{1}.fs.bam".format(mapdir, name) ]
region = "whole"
outfile = "{0}/{1}.{2}.depth".format(outdir, name, region)
errfile = "{0}/{1}.{2}.depth.stderr".format(outdir, name, region)
OUTPUT = open(outfile, "w")
ERROR = open(errfile, "w")
code = 0
try:
pysam.depth(*cmd, save_stdout=outfile, save_stderr=errfile)
except:
code = 1
OUTPUT.close()
ERROR.close()
if code != 0:
break
if code != 0:
break
if code == 0:
return True
else:
return False
def filter_coverage_file(cov_dir, Names, Beds, min_cov):
code = 0
if int(min_cov) > 1:
for name in Names:
for region in Beds.keys():
code = 0
try:
raw_cov_file = "{0}/{1}.{2}.depth".format(cov_dir, name, region)
dest_cov_file = "{0}/{1}.{2}.{3}x.depth".format(cov_dir, name, region, min_cov)
INPUT = open(raw_cov_file, "r")
OUTPUT = open(dest_cov_file, "w")
for line in INPUT:
lst = line.rstrip("\b\r\n").split("\t")
if int(lst[2]) >= int(min_cov):
OUTPUT.write(line)
INPUT.close()
OUTPUT.close()
except:
code = 1
break
if code != 0:
break
else:
for name in Names:
for region in Beds.keys():
code = 0
try:
raw_cov_file = "{0}/{1}.{2}.depth".format(cov_dir, name, region)
dest_cov_file = "{0}/{1}.{2}.{3}x.depth".format(cov_dir, name, region, min_cov)
os.rename(raw_cov_file, dest_cov_file)
except:
code = 1
break
if code != 0:
break
if code == 0:
return True
else:
return False
def get_fasta_lengths(fasta):
INPUT = open(fasta, "r")
Lengths = [ (str(record.id), len(str(record.seq))) for record in SeqIO.parse(INPUT, "fasta") ]
INPUT.close()
Lengths = sorted(Lengths, key=itemgetter(0))
return Lengths
def get_genome_file(reference, outdir):
Lengths = get_fasta_lengths(reference)
OUTPUT = open("{0}/genome.fa.lengths".format(outdir), "w")
for x in Lengths:
line = str(x[0]) + "\t" + str(x[1]) + "\n"
OUTPUT.write(line)
OUTPUT.close()
return True
def make_executable(path):
mode = os.stat(path).st_mode
mode |= (mode & 0o444) >> 2
os.chmod(path, mode)
def run_coverage_analysis( script_dir, species_name, cov_dir, outdir_absolute, Names, bed_files,
bed_names, genome_file, window_size, threads, min_frac_pos, min_cov_pos,
min_feat_length, min_cov, n_breaks ):
cmd = [ "{0}".format(sys.executable),
"{0}/src/analyze-windows.py".format(script_dir),
"--species-name", str(species_name),
"--cov-dir", str(cov_dir),
"--output-dir", str(outdir_absolute),
"--cov-names", " ".join(Names),
"--beds", " ".join(bed_files),
"--beds-names", " ".join(bed_names),
"--scaf-lengths", str(genome_file),
"--window-size", str(window_size),
"--threads", str(threads),
"--min-frac-pos", str(min_frac_pos),
"--min-cov-pos", str(min_cov_pos),
"--min-length", str(min_feat_length),
"--min-coverage", str(min_cov),
"--n-breaks", str(n_breaks) ]
# join and split again to get the bed files in separate entries
cmd = " ".join(cmd).split(" ")
code = run_cmd( cmd,
"{0}/{1}.windows.table".format(outdir_absolute, species_name),
"{0}/{1}.windows.table.stderr".format(outdir_absolute, species_name) )
if code == 0:
return True
else:
return False
def filter_tables(species_name, tables_dir, Regions, Names, Excluded_sequences):
# read the output tables produced in run_coverage_analysis()
# ...
# remove lines corresponding to scaffolds in Excluded sequences
# ...
# return filtered tables
try:
infile = "{0}/{1}.combined.results.txt".format(tables_dir, species_name)
outfile = "{0}/RES.{1}.combined.results.txt".format(tables_dir, species_name)
x = pd.read_csv(infile, sep="\t")
x = x[~x["Sequence"].isin(Excluded_sequences)]
x.to_csv(outfile, sep="\t", index=False)
for region in Regions:
for name in Names:
infile = "{0}/{1}.{2}.{3}.txt".format(tables_dir, species_name, name, region)
outfile = "{0}/RES.{1}.{2}.{3}.txt".format(tables_dir, species_name, name, region)
x = pd.read_csv(infile, sep="\t")
x = x[~x["Sequence"].isin(Excluded_sequences)]
x.to_csv(outfile, sep="\t", index=False)
return True
except:
return False
def generate_metrics_file(species_name, output_dir, tables_dir, Regions, Names, max_jacc_uniq, output_file, window_size):
status = False
Metrics = { region:{} for region in Regions }
for region in Regions:
# union, intersection, uncovered
infile = "{0}/RES.{1}.combined.results.txt".format(tables_dir, species_name)
x = pd.read_table(infile)
union = x[x['Feature']==region].loc[(x["Jaccard"] >= 0.0) , ["Sequence", "W_start", "W_end", "Union"]].drop_duplicates()['Union'].sum()
intersection = x[x['Feature']==region].loc[(x["Jaccard"] >= 0.0) , ["Sequence", "W_start", "W_end", "Intersection"]].drop_duplicates()['Intersection'].sum()
uncovered = x[x['Feature']==region].loc[(x["Jaccard"] >= 0.0) , ["Sequence", "W_start", "W_end", "Uncovered"]].drop_duplicates()['Uncovered'].sum()
# NA positions
# positions where nothing could be inferred from the jaccard calculation (-0.1)
# this means uncovered or too poorly covered
na_positions = int(x[x['Feature']==region].loc[(x["Jaccard"] < 0.0) , ["Sequence", "W_start", "W_end", "Uncovered"]].shape[0]) * window_size
total_length = union + uncovered
Metrics[region]["Total"] = total_length
Metrics[region]["Union"] = union
Metrics[region]["Intersection"] = intersection
Metrics[region]["Uncovered"] = uncovered
Metrics[region]["NA_positions"] = na_positions
# subgenomes
for name in Names:
Metrics[region][name] = {}
infile = "{0}/RES.{1}.{2}.{3}.txt".format(tables_dir, species_name, name, region)
y = pd.read_table(infile)
intersection_sub = y.loc[(y["Jaccard"] >= 0.0) , ["Sequence", "W_start", "W_end", "Intersection"]].drop_duplicates()['Intersection'].sum()
# the unique value is computed only for those windows that have a Jaccard index sufficiently low (specified via command line)
mask = (y["Jaccard"] >= 0.0) & (y["Jaccard"] <= max_jacc_uniq)
unique_sub = y.loc[mask , ["Sequence", "W_start", "W_end", "Unique"]].drop_duplicates()['Unique'].sum()
# total, unique, mean cov, mean frac pos, 0-50, 50-100
Metrics[region][name]['Total'] = y.shape[0] * window_size
Metrics[region][name]["Unique_(J<={0})".format(max_jacc_uniq)] = unique_sub
OUTPUT = open(output_file, "w")
for region in Metrics.keys():
for metric in ["Total", "Union", "Intersection", "Uncovered", "NA_positions"]:
line = "\t".join([region, "-", metric, str(Metrics[region][metric])]) + "\n"
OUTPUT.write(line)
for name in Names:
for metric in [ "Total", "Unique_(J<={0})".format(max_jacc_uniq)]:
line = "\t".join([region, name, metric, str(Metrics[region][name][metric])])+"\n"
OUTPUT.write(line)
OUTPUT.close()
status = True
return status
def get_genome_size(genome_file):
INPUT = open(genome_file, "r")
genome_size = sum([ int(line.rstrip("\b\r\n").split("\t")[1]) for line in INPUT ])
INPUT.close()
return int(genome_size)
def get_relative_cov_frac(matrix, window_size, genome_file, tables_dir, region):
try:
# get genome length
genome_size = get_genome_size("{0}.lengths".format(hisat2_index))
# read lines excluding header
df = pd.read_csv(matrix, sep="\t")
df = df[df["Feature"]==region]
# compute values
# tot_win_len can be larger than genome size because of the windows at the end of a scaffold
# who could exceed a scaffold length
tot_windows = df.loc[:,["Sequence", "W_start"]].drop_duplicates().shape[0]
analysed_size = min(genome_size, int(tot_windows) * int(window_size))
# this value represents those lines with a valid jaccard index
# that is, those where J is >= 0
non_NA_windows = df[df["Jaccard"]>=0].loc[:,["Sequence", "W_start"]].drop_duplicates().shape[0]
non_NA_analysed_size = min(genome_size, int(non_NA_windows) * int(window_size))
# this value represents the total positions represented in the windows with a valid Jaccard
real_cov_positions = df[df["Jaccard"]>=0].loc[:,["Sequence", "W_start", "Real_length"]].drop_duplicates()["Real_length"].sum()
informative_positions = min(genome_size, real_cov_positions)
# compute relative covered fractions
rel_analysed_size = float(analysed_size) / float(genome_size) * 100
rel_non_NA_analysed_size = float(non_NA_analysed_size) / float(genome_size) * 100
rel_informative_positions = float(informative_positions) / float(genome_size) * 100
# write to file
relative_lengths_file = "{0}/RES.{1}.relative_lengths.table".format(tables_dir, region)
OUTPUT = open(relative_lengths_file, "w")
OUTPUT.write("\n".join([
"\t".join(("genome_size", str(genome_size))),
"\t".join(("rel_genome_size", str(float(100)))),
"\t".join(("analysed_size", str(analysed_size))),
"\t".join(("rel_analysed_size", str(rel_analysed_size))),
"\t".join(("non_NA_analysed_size", str(non_NA_analysed_size))),
"\t".join(("rel_non_NA_analysed_size", str(rel_non_NA_analysed_size))),
"\t".join(("informative_positions", str(informative_positions))),
"\t".join(("rel_informative_positions", str(rel_informative_positions))) ]) + "\n")
OUTPUT.close()
# return
return (True, relative_lengths_file)
except:
relative_lengths_file = "{0}/RES.{1}.relative_lengths.table".format(tables_dir, region)
return (False, relative_lengths_file)
def run_coverage_plot(outdir, matrix, R_path, region, name, species_name, maxcov):
R_lines = [
'suppressWarnings(library(ggplot2))',
'',
'x <- read.table("{0}", header=T)'.format(matrix),
'x$Frac_pos <- as.numeric(as.character(x$Frac_pos))',
'x$Mean_cov <- as.numeric(as.character(x$Mean_cov))',
'x <- x[x$Jaccard >= 0 , ]',
'',
'P1 <- ggplot() + ',
'theme(plot.title=element_text(family="sans", face="bold", colour="grey20", size=13),',
'axis.title.x=element_text(family="Helvetica", face="plain", colour="grey20", size=10),',
'axis.title.y=element_text(family="Helvetica", face="plain", colour="grey20", size=10),',
'axis.text.x=element_text(family="sans", face="plain", colour="grey20", size=11),',
'axis.text.y=element_text(family="sans", face="italic", colour="grey20", size=11),',
'legend.text=element_text(family="sans", face="italic", colour="black", size=9),',
'legend.title=element_text(family="sans", face="plain", colour="black", size=9),',
'panel.background=element_rect(fill="grey90", colour="grey90"),',
'aspect.ratio=0.35) + ',
'ggtitle("Coverage, {0}, {1}") +'.format(name, region),
'xlab("") + ',
'ylab("Mean coverage [x]") +',
'geom_violin(data=x, mapping=aes(x=1, y=Mean_cov)) + ',
'geom_jitter(data=x, mapping=aes(x=3, y=Mean_cov, col=Frac_pos)) + ',
'scale_x_discrete(breaks=c(1,3), labels=c("","")) +',
'scale_y_continuous(limits=c(0, {0})) + '.format(maxcov),
'scale_color_gradient2(breaks=seq(0,100,20), limits=c(0,100), low="green3", high="darkorchid4", midpoint=50, name="% covered") +',
'coord_flip()',
'',
'basename <- paste("{0}", "cov", "{1}", "{2}", sep=".")'.format(species_name, name, region),
'',
'svg(paste("{0}", "svg", paste(basename, "svg", sep="."), sep="/"))'.format(outdir),
'plot(P1)',
'dev.off()',
'',
'png(paste("{0}", "png", paste(basename, "png", sep="."), sep="/"), height=400, width=1000, pointsize=11, units="px", type="cairo", res=200)'.format(outdir),
'suppressWarnings(plot(P1))',
'dev.off()' ]
Rplot = "{0}.{1}.{2}.cov.Rplot".format(species_name, name, region)
OUTPUT = open("{0}/scripts/{1}".format(outdir, Rplot), "w")
for line in R_lines:
OUTPUT.write(line + "\n")
OUTPUT.close()