annotate coverage_stats-1b5523d3d2c2/tools/coverage_stats/coverage_stats.py @ 3:57b3ea22aff3 draft

Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
author peterjc
date Tue, 11 Aug 2020 18:23:05 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
1 #!/usr/bin/env python
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
2 """BAM coverage statistics using samtools idxstats and depth.
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
3
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
4 This script takes exactly three command line arguments:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
5 * Input BAM filename
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
6 * Input BAI filename (via Galaxy metadata)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
7 * Output tabular filename
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
8
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
9 Optional fourth argument:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
10 * Max coverage depth (integer)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
11
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
12 This messes about with the filenames to make samtools happy, then
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
13 runs "samtools idxstats" and "samtools depth", captures and combines
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
14 the output to the desired output tabular file.
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
15
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
16 Because "samtools depth" treats the max depth a little fuzzily, this
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
17 tool tries to account for this and applies a clear max-depth cut off.
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
18 """
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
19
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
20 import os
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
21 import subprocess
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
22 import sys
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
23 import tempfile
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
24
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
25 from optparse import OptionParser
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
26
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
27 usage = """Example usage:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
28
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
29 $ python coverage_stats.py -b mapped.bam -i mapped.bai -o summary.tsv -d 10000
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
30 """
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
31
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
32 parser = OptionParser(usage=usage)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
33 parser.add_option(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
34 "-b", "--bam", dest="input_bam", default=None, help="Input BAM file", metavar="FILE"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
35 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
36 parser.add_option(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
37 "-i",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
38 "--bai",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
39 dest="input_bai",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
40 default="-",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
41 help="Input BAI file (BAM index, optional, default or - infer from BAM filename)",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
42 metavar="FILE",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
43 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
44 parser.add_option(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
45 "-o",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
46 dest="output_tabular",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
47 default="-",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
48 help="Output tabular file (optional, default stdout)",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
49 metavar="FILE",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
50 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
51 parser.add_option(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
52 "-d",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
53 "--maxdepth",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
54 dest="max_depth",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
55 default=8000,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
56 help="Max coverage depth (integer, default 8000)",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
57 type="int",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
58 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
59 parser.add_option(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
60 "-v",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
61 "--version",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
62 dest="version",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
63 default=False,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
64 action="store_true",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
65 help="Show version and quit",
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
66 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
67
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
68 options, args = parser.parse_args()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
69
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
70 if options.version:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
71 # Galaxy seems to invert the order of the two lines
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
72 print("BAM coverage statistics v0.1.0")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
73 cmd = "samtools 2>&1 | grep -i ^Version"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
74 sys.exit(os.system(cmd))
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
75
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
76 bam_filename = options.input_bam
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
77 bai_filename = options.input_bai
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
78 tabular_filename = options.output_tabular
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
79 max_depth = options.max_depth
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
80
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
81 if args:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
82 sys.exit("Sorry, the legacy API has been dropped.")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
83
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
84 if not bam_filename:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
85 sys.exit("Input BAM filename is required.")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
86 if not os.path.isfile(bam_filename):
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
87 sys.exit("Input BAM file not found: %s" % bam_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
88 if bai_filename == "-":
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
89 if os.path.isfile(bam_filename + ".bai"):
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
90 bai_filename = bam_filename + ".bai"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
91 if not os.path.isfile(bai_filename):
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
92 if bai_filename == "None":
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
93 sys.exit("Error: Galaxy did not index your BAM file")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
94 sys.exit("Input BAI file not found: %s" % bai_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
95
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
96 try:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
97 max_depth = int(max_depth)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
98 except ValueError:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
99 sys.exit("Bad argument for max depth: %r" % max_depth)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
100 if max_depth < 0:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
101 sys.exit("Bad argument for max depth: %r" % max_depth)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
102
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
103 # fuzz factor to ensure can reach max_depth, e.g. region with
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
104 # many reads having a deletion present could underestimate the
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
105 # coverage by capping the number of reads considered
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
106 depth_margin = 100
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
107
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
108 # Assign sensible names with real extensions, and setup symlinks:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
109 tmp_dir = tempfile.mkdtemp()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
110 bam_file = os.path.join(tmp_dir, "temp.bam")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
111 bai_file = os.path.join(tmp_dir, "temp.bam.bai")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
112 idxstats_filename = os.path.join(tmp_dir, "idxstats.tsv")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
113 depth_filename = os.path.join(tmp_dir, "depth.tsv")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
114 os.symlink(os.path.abspath(bam_filename), bam_file)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
115 os.symlink(os.path.abspath(bai_filename), bai_file)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
116 assert os.path.isfile(bam_file), bam_file
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
117 assert os.path.isfile(bai_file), bai_file
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
118 assert os.path.isfile(bam_file + ".bai"), bam_file
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
119
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
120
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
121 def clean_up():
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
122 """Remove our temporary files and directory."""
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
123 os.remove(idxstats_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
124 os.remove(depth_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
125 os.remove(bam_file)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
126 os.remove(bai_file)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
127 os.rmdir(tmp_dir)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
128
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
129
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
130 def samtools_depth_opt_available():
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
131 """Determine if samtools depth supports maximum coverage argument."""
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
132 child = subprocess.Popen(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
133 ["samtools", "depth"],
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
134 universal_newlines=True,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
135 stdout=subprocess.PIPE,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
136 stderr=subprocess.STDOUT,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
137 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
138 # Combined stdout/stderr in case samtools is ever inconsistent
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
139 output, tmp = child.communicate()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
140 assert tmp is None
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
141 # Expect to find this line in the help text, exact wording could change:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
142 # -d/-m <int> maximum coverage depth [8000]
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
143 return " -d/-m " in output
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
144
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
145
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
146 depth_hack = False
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
147 if not samtools_depth_opt_available():
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
148 if max_depth + depth_margin <= 8000:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
149 sys.stderr.write(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
150 "WARNING: The version of samtools depth installed does not "
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
151 "support the -d option, however, the requested max-depth "
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
152 "is safely under the default of 8000.\n"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
153 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
154 depth_hack = True
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
155 else:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
156 sys.exit(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
157 "The version of samtools depth installed does not support the -d option."
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
158 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
159
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
160 # Run samtools idxstats:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
161 cmd = 'samtools idxstats "%s" > "%s"' % (bam_file, idxstats_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
162 return_code = os.system(cmd)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
163 if return_code:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
164 clean_up()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
165 sys.exit("Return code %i from command:\n%s" % (return_code, cmd))
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
166
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
167 # Run samtools depth:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
168 # TODO - Parse stdout instead?
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
169 if depth_hack:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
170 # Using an old samtools without the -d option, but hard coded default
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
171 # of 8000 should be fine even allowing a margin for fuzzy output
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
172 cmd = 'samtools depth "%s" > "%s"' % (bam_file, depth_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
173 else:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
174 cmd = 'samtools depth -d %i "%s" > "%s"' % (
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
175 max_depth + depth_margin,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
176 bam_file,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
177 depth_filename,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
178 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
179 return_code = os.system(cmd)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
180 if return_code:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
181 clean_up()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
182 sys.exit("Return code %i from command:\n%s" % (return_code, cmd))
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
183
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
184
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
185 def load_total_coverage(depth_handle, identifier, length):
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
186 """Parse some of the 'samtools depth' output for coverages.
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
187
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
188 Returns min_cov (int), max_cov (int) and mean cov (float).
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
189
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
190 Uses global variables to cache the first line of output from the
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
191 next reference sequence.
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
192 """
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
193 global depth_ref, depth_pos, depth_reads
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
194
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
195 # print("====")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
196 # print("%s coverage calculation, length %i, ..." % (identifier, length))
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
197
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
198 if depth_ref is None:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
199 # Right at start of file / new contig
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
200 line = depth_handle.readline()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
201 # Are we at the end of the file?
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
202 if not line:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
203 # Must be at the end of the file.
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
204 # This can happen if the file contig(s) had no reads mapped
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
205 return 0, 0, 0.0, 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
206 depth_ref, depth_pos, depth_reads = line.rstrip("\n").split()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
207 depth_pos = int(depth_pos)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
208 depth_reads = min(max_depth, int(depth_reads))
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
209 # Can now treat as later references where first line cached
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
210 elif identifier != depth_ref:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
211 # Infer that identifier had coverage zero,
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
212 # and so was not in the 'samtools depth'
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
213 # output.
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
214 # print("%s appears to have no coverage at all" % identifier)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
215 return 0, 0, 0.0, 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
216
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
217 # Good, at start of expected reference
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
218 bases = depth_reads
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
219 if depth_pos == 1:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
220 min_cov = depth_reads
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
221 else:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
222 # print("%s has no coverage at start" % identifier)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
223 min_cov = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
224 max_cov = depth_reads
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
225
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
226 last_pos = depth_pos
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
227 depth_ref = None
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
228 depth_pos = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
229 depth_reads = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
230 for line in depth_handle:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
231 ref, pos, depth = line.rstrip("\n").split()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
232 pos = int(pos)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
233 depth = min(max_depth, int(depth))
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
234 if ref != identifier:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
235 # Reached the end of this identifier's coverage
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
236 # so cache this ready for next identifier
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
237 depth_ref, depth_pos, depth_reads = ref, pos, depth
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
238 break
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
239 bases += depth
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
240 if last_pos + 1 < pos:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
241 # print("%s has no coverage between %i and %i"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
242 # % (identifier, last_pos, pos))
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
243 min_cov = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
244 else:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
245 min_cov = min(min_cov, depth)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
246 max_cov = max(max_cov, depth)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
247 last_pos = pos
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
248
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
249 # Reach the end of this identifier's coverage or end of file
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
250 if last_pos < length:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
251 # print("%s has no coverage at end" % identifier)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
252 min_cov = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
253 mean_cov = bases / float(length)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
254 return min_cov, max_cov, mean_cov, bases
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
255
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
256
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
257 # Parse and combine the output
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
258 if tabular_filename == "-":
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
259 out_handle = sys.stdout
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
260 else:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
261 out_handle = open(tabular_filename, "w")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
262 out_handle.write("#identifer\tlength\tmapped\tplaced\tmin_cov\tmax_cov\tmean_cov\n")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
263
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
264 idxstats_handle = open(idxstats_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
265 depth_handle = open(depth_filename)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
266
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
267 depth_ref = None
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
268 depth_pos = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
269 depth_reads = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
270
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
271 global_bases = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
272 global_length = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
273 for line in idxstats_handle:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
274 identifier, length, mapped, placed = line.rstrip("\n").split()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
275 length = int(length)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
276 mapped = int(mapped)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
277 placed = int(placed)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
278 if identifier == "*":
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
279 min_cov = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
280 max_cov = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
281 mean_cov = 0.0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
282 bases = 0
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
283 else:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
284 min_cov, max_cov, mean_cov, bases = load_total_coverage(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
285 depth_handle, identifier, length
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
286 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
287 if max_cov > max_depth:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
288 sys.exit(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
289 "Using max depth %i yet saw max coverage %i for %s"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
290 % (max_depth, max_cov, identifier)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
291 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
292 out_handle.write(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
293 "%s\t%i\t%i\t%i\t%i\t%i\t%0.2f\n"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
294 % (identifier, length, mapped, placed, min_cov, max_cov, mean_cov)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
295 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
296 if not (min_cov <= mean_cov <= max_cov):
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
297 out_handle.write("ERROR, expect min_cov <= mean_cov <= max_cov\n")
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
298 idxstats_handle.close()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
299 depth_handle.close()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
300 out_handle.close()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
301 clean_up()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
302 sys.exit(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
303 "Problem with coverage for %s, expect min_cov <= mean_cov <= max_cov"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
304 % identifier
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
305 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
306 global_length += length
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
307 global_bases += bases
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
308
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
309 idxstats_handle.close()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
310 depth_handle.close()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
311 if tabular_filename != "-":
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
312 out_handle.close()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
313
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
314 print(
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
315 "Total reference length %i with overall mean coverage %0.2f"
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
316 % (global_length, float(global_bases) / global_length)
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
317 )
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
318
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
319 # Remove the temp symlinks and files:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
320 clean_up()
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
321
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
322 if depth_ref is not None:
57b3ea22aff3 Uploaded v0.1.0 which was already on the Test Tool Shed. Included Python 3 support.
peterjc
parents:
diff changeset
323 sys.exit("Left over output from 'samtools depth'? %r" % depth_ref)