annotate vcfs2fasta.py @ 22:96f393ad7fc6 draft default tip

Uploaded
author ulfschaefer
date Wed, 23 Dec 2015 04:50:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
1 #!/usr/bin/env python
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
2 '''
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
3 Merge SNP data from multiple VCF files into a single fasta file.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
4
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
5 Created on 5 Oct 2015
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
6
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
7 @author: alex
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
8 '''
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
9 import argparse
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
10 from collections import OrderedDict
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
11 from collections import defaultdict
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
12 import glob
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
13 import itertools
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
14 import logging
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
15 import os
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
16
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
17 from Bio import SeqIO
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
18 from bintrees import FastRBTree
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
19
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
20 # Try importing the matplotlib and numpy for stats.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
21 try:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
22 from matplotlib import pyplot as plt
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
23 import numpy
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
24 can_stats = True
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
25 except ImportError:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
26 can_stats = False
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
27
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
28 import vcf
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
29
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
30 from phe.variant_filters import IUPAC_CODES
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
31
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
32
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
33 def plot_stats(pos_stats, total_samples, plots_dir="plots", discarded={}):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
34 if not os.path.exists(plots_dir):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
35 os.makedirs(plots_dir)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
36
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
37 for contig in pos_stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
38 plt.style.use('ggplot')
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
39
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
40 x = numpy.array([pos for pos in pos_stats[contig] if pos not in discarded.get(contig, [])])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
41 y = numpy.array([ float(pos_stats[contig][pos]["mut"]) / total_samples for pos in pos_stats[contig] if pos not in discarded.get(contig, []) ])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
42
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
43 f, (ax1, ax2, ax3, ax4) = plt.subplots(4, sharex=True, sharey=True)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
44 f.set_size_inches(12, 15)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
45 ax1.plot(x, y, 'ro')
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
46 ax1.set_title("Fraction of samples with SNPs")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
47 plt.ylim(0, 1.1)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
48
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
49 y = numpy.array([ float(pos_stats[contig][pos]["N"]) / total_samples for pos in pos_stats[contig] if pos not in discarded.get(contig, [])])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
50 ax2.plot(x, y, 'bo')
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
51 ax2.set_title("Fraction of samples with Ns")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
52
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
53 y = numpy.array([ float(pos_stats[contig][pos]["mix"]) / total_samples for pos in pos_stats[contig] if pos not in discarded.get(contig, [])])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
54 ax3.plot(x, y, 'go')
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
55 ax3.set_title("Fraction of samples with mixed bases")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
56
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
57 y = numpy.array([ float(pos_stats[contig][pos]["gap"]) / total_samples for pos in pos_stats[contig] if pos not in discarded.get(contig, [])])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
58 ax4.plot(x, y, 'yo')
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
59 ax4.set_title("Fraction of samples with uncallable genotype (gap)")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
60
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
61 contig = contig.replace("/", "-")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
62 plt.savefig(os.path.join(plots_dir, "%s.png" % contig), dpi=100)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
63
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
64 def get_mixture(record, threshold):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
65 mixtures = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
66 try:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
67 if len(record.samples[0].data.AD) > 1:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
68
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
69 total_depth = sum(record.samples[0].data.AD)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
70 # Go over all combinations of touples.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
71 for comb in itertools.combinations(range(0, len(record.samples[0].data.AD)), 2):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
72 i = comb[0]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
73 j = comb[1]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
74
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
75 alleles = list()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
76
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
77 if 0 in comb:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
78 alleles.append(str(record.REF))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
79
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
80 if i != 0:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
81 alleles.append(str(record.ALT[i - 1]))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
82 mixture = record.samples[0].data.AD[i]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
83 if j != 0:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
84 alleles.append(str(record.ALT[j - 1]))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
85 mixture = record.samples[0].data.AD[j]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
86
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
87 ratio = float(mixture) / total_depth
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
88 if ratio == 1.0:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
89 logging.debug("This is only designed for mixtures! %s %s %s %s", record, ratio, record.samples[0].data.AD, record.FILTER)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
90
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
91 if ratio not in mixtures:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
92 mixtures[ratio] = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
93 mixtures[ratio].append(alleles.pop())
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
94
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
95 elif ratio >= threshold:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
96 try:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
97 code = IUPAC_CODES[frozenset(alleles)]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
98 if ratio not in mixtures:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
99 mixtures[ratio] = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
100 mixtures[ratio].append(code)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
101 except KeyError:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
102 logging.warn("Could not retrieve IUPAC code for %s from %s", alleles, record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
103 except AttributeError:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
104 mixtures = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
105
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
106 return mixtures
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
107
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
108 def print_stats(stats, pos_stats, total_vars):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
109 for contig in stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
110 for sample, info in stats[contig].items():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
111 print "%s,%i,%i" % (sample, len(info.get("n_pos", [])), total_vars)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
112
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
113 for contig in stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
114 for pos, info in pos_stats[contig].iteritems():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
115 print "%s,%i,%i,%i,%i" % (contig, pos, info.get("N", "NA"), info.get("-", "NA"), info.get("mut", "NA"))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
116
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
117
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
118 def get_args():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
119 args = argparse.ArgumentParser(description="Combine multiple VCFs into a single FASTA file.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
120
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
121 group = args.add_mutually_exclusive_group(required=True)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
122 group.add_argument("--directory", "-d", help="Path to the directory with .vcf files.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
123 group.add_argument("--input", "-i", type=str, nargs='+', help="List of VCF files to process.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
124
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
125 args.add_argument("--out", "-o", required=True, help="Path to the output FASTA file.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
126
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
127 args.add_argument("--with-mixtures", type=float, help="Specify this option with a threshold to output mixtures above this threshold.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
128
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
129 args.add_argument("--column-Ns", type=float, help="Keeps columns with fraction of Ns above specified threshold.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
130
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
131 args.add_argument("--sample-Ns", type=float, help="Keeps samples with fraction of Ns above specified threshold.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
132
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
133 args.add_argument("--reference", type=str, help="If path to reference specified (FASTA), then whole genome will be written.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
134
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
135 group = args.add_mutually_exclusive_group()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
136
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
137 group.add_argument("--include")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
138 group.add_argument("--exclude")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
139
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
140 args.add_argument("--with-stats", help="If a path is specified, then position of the outputed SNPs is stored in this file. Requires mumpy and matplotlib.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
141 args.add_argument("--plots-dir", default="plots", help="Where to write summary plots on SNPs extracted. Requires mumpy and matplotlib.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
142
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
143 args.add_argument("--debug", action="store_true", help="More verbose logging (default: turned off).")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
144 args.add_argument("--local", action="store_true", help="Re-read the VCF instead of storing it in memory.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
145
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
146 return args.parse_args()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
147
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
148 def main():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
149 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
150 Process VCF files and merge them into a single fasta file.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
151 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
152 args = get_args()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
153
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
154 logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
155
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
156 contigs = list()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
157
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
158 sample_stats = dict()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
159
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
160 # All positions available for analysis.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
161 avail_pos = dict()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
162 # Stats about each position in each chromosome.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
163 pos_stats = dict()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
164 indel_summary = defaultdict(int)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
165 # Cached version of the data.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
166 vcf_data = dict()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
167 mixtures = dict()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
168
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
169 empty_tree = FastRBTree()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
170
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
171 exclude = False
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
172 include = False
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
173
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
174 if args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
175 ref_seq = OrderedDict()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
176 with open(args.reference) as fp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
177 for record in SeqIO.parse(fp, "fasta"):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
178 ref_seq[record.id] = str(record.seq)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
179
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
180 args.reference = ref_seq
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
181
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
182 if args.exclude or args.include:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
183 pos = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
184 chr_pos = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
185 bed_file = args.include if args.include is not None else args.exclude
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
186
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
187 with open(bed_file) as fp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
188 for line in fp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
189 data = line.strip().split("\t")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
190
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
191 chr_pos += [ (i, False,) for i in xrange(int(data[1]), int(data[2]) + 1)]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
192
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
193 if data[0] not in pos:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
194 pos[data[0]] = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
195
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
196 pos[data[0]] += chr_pos
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
197
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
198
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
199 pos = {chrom: FastRBTree(l) for chrom, l in pos.items()}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
200
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
201 if args.include:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
202 include = pos
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
203 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
204 exclude = pos
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
205
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
206
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
207 if args.directory is not None and args.input is None:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
208 args.input = glob.glob(os.path.join(args.directory, "*.filtered.vcf"))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
209
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
210 # First pass to get the references and the positions to be analysed.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
211 for vcf_in in args.input:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
212 sample_name, _ = os.path.splitext(os.path.basename(vcf_in))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
213 vcf_data[vcf_in] = list()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
214 reader = vcf.Reader(filename=vcf_in)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
215
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
216 for record in reader:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
217 if include and include.get(record.CHROM, empty_tree).get(record.POS, True) or exclude and not exclude.get(record.CHROM, empty_tree).get(record.POS, True):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
218 continue
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
219
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
220 if not args.local:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
221 vcf_data[vcf_in].append(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
222
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
223 if record.CHROM not in contigs:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
224 contigs.append(record.CHROM)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
225 avail_pos[record.CHROM] = FastRBTree()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
226 mixtures[record.CHROM] = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
227 sample_stats[record.CHROM] = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
228
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
229 if sample_name not in mixtures[record.CHROM]:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
230 mixtures[record.CHROM][sample_name] = FastRBTree()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
231
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
232 if sample_name not in sample_stats[record.CHROM]:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
233 sample_stats[record.CHROM][sample_name] = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
234
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
235 if not record.FILTER:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
236 if record.is_snp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
237 if record.POS in avail_pos[record.CHROM] and avail_pos[record.CHROM][record.POS] != record.REF:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
238 logging.critical("SOMETHING IS REALLY WRONG because reference for the same position is DIFFERENT! %s in %s", record.POS, vcf_in)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
239 return 2
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
240
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
241 if record.CHROM not in pos_stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
242 pos_stats[record.CHROM] = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
243
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
244 avail_pos[record.CHROM].insert(record.POS, str(record.REF))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
245 pos_stats[record.CHROM][record.POS] = {"N":0, "-": 0, "mut": 0, "mix": 0, "gap": 0}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
246
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
247 elif args.with_mixtures and record.is_snp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
248 mix = get_mixture(record, args.with_mixtures)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
249
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
250 for ratio, code in mix.items():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
251 for c in code:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
252 avail_pos[record.CHROM].insert(record.POS, str(record.REF))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
253 if record.CHROM not in pos_stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
254 pos_stats[record.CHROM] = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
255 pos_stats[record.CHROM][record.POS] = {"N": 0, "-": 0, "mut": 0, "mix": 0, "gap": 0}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
256
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
257 if sample_name not in mixtures[record.CHROM]:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
258 mixtures[record.CHROM][sample_name] = FastRBTree()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
259
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
260 mixtures[record.CHROM][sample_name].insert(record.POS, c)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
261 elif not record.is_deletion and not record.is_indel:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
262 if record.CHROM not in pos_stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
263 pos_stats[record.CHROM] = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
264 pos_stats[record.CHROM][record.POS] = {"N": 0, "-": 0, "mut": 0, "mix": 0, "gap": 0}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
265 avail_pos[record.CHROM].insert(record.POS, str(record.REF))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
266 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
267 logging.debug("Discarding %s from %s as DEL and/or INDEL", record.POS, vcf_in)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
268 indel_summary[vcf_in] += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
269 try:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
270 vcf_data[vcf_in].remove(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
271 except ValueError:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
272 pass
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
273
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
274
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
275 all_data = { contig: {} for contig in contigs}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
276 samples = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
277
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
278 for vcf_in in args.input:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
279
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
280 sample_seq = ""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
281 sample_name, _ = os.path.splitext(os.path.basename(vcf_in))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
282 samples.append(sample_name)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
283
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
284 # Initialise the data for this sample to be REF positions.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
285 for contig in contigs:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
286 all_data[contig][sample_name] = { pos: avail_pos[contig][pos] for pos in avail_pos[contig] }
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
287
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
288 # Re-read data from VCF if local is specified, otherwise get it from memory.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
289 iterator = vcf.Reader(filename=vcf_in) if args.local else vcf_data[vcf_in]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
290 for record in iterator:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
291 # Array of filters that have been applied.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
292 filters = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
293
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
294 # If position is our available position.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
295 if avail_pos.get(record.CHROM, empty_tree).get(record.POS, False):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
296 if not record.FILTER:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
297 if record.is_snp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
298 if len(record.ALT) > 1:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
299 logging.info("POS %s passed filters but has multiple alleles. Inserting N")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
300 all_data[record.CHROM][sample_name][record.POS] = "N"
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
301 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
302 all_data[record.CHROM][sample_name][record.POS] = record.ALT[0].sequence
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
303 pos_stats[record.CHROM][record.POS]["mut"] += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
304 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
305
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
306 # Currently we are only using first filter to call consensus.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
307 extended_code = mixtures[record.CHROM][sample_name].get(record.POS, "N")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
308
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
309 # extended_code = PHEFilterBase.call_concensus(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
310
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
311 # Calculate the stats
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
312 if extended_code == "N":
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
313 pos_stats[record.CHROM][record.POS]["N"] += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
314
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
315 if "n_pos" not in sample_stats[record.CHROM][sample_name]:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
316 sample_stats[record.CHROM][sample_name]["n_pos"] = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
317 sample_stats[record.CHROM][sample_name]["n_pos"].append(record.POS)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
318
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
319 elif extended_code == "-":
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
320 pos_stats[record.CHROM][record.POS]["-"] += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
321 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
322 pos_stats[record.CHROM][record.POS]["mix"] += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
323 # print "Good mixture %s: %i (%s)" % (sample_name, record.POS, extended_code)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
324 # Record if there was uncallable genoty/gap in the data.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
325 if record.samples[0].data.GT == "./.":
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
326 pos_stats[record.CHROM][record.POS]["gap"] += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
327
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
328 # Save the extended code of the SNP.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
329 all_data[record.CHROM][sample_name][record.POS] = extended_code
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
330 del vcf_data[vcf_in]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
331
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
332 # Output the data to the fasta file.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
333 # The data is already aligned so simply output it.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
334 discarded = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
335
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
336 if args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
337 # These should be in the same order as the order in reference.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
338 contigs = args.reference.keys()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
339
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
340 if args.sample_Ns:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
341 delete_samples = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
342 for contig in contigs:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
343 for sample in samples:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
344
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
345 # Skip if the contig not in sample_stats
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
346 if contig not in sample_stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
347 continue
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
348
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
349 sample_n_ratio = float(len(sample_stats[contig][sample]["n_pos"])) / len(avail_pos[contig])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
350 if sample_n_ratio > args.sample_Ns:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
351 for pos in sample_stats[contig][sample]["n_pos"]:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
352 pos_stats[contig][pos]["N"] -= 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
353
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
354 logging.info("Removing %s due to high Ns in sample: %s", sample , sample_n_ratio)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
355
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
356 delete_samples.append(sample)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
357
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
358 samples = [sample for sample in samples if sample not in delete_samples]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
359 snp_positions = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
360 with open(args.out, "w") as fp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
361
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
362 for sample in samples:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
363 sample_seq = ""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
364 for contig in contigs:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
365 if contig in avail_pos:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
366 if args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
367 positions = xrange(1, len(args.reference[contig]) + 1)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
368 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
369 positions = avail_pos[contig].keys()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
370 for pos in positions:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
371 if pos in avail_pos[contig]:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
372 if not args.column_Ns or float(pos_stats[contig][pos]["N"]) / len(samples) < args.column_Ns and \
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
373 float(pos_stats[contig][pos]["-"]) / len(samples) < args.column_Ns:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
374 sample_seq += all_data[contig][sample][pos]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
375 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
376 if contig not in discarded:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
377 discarded[contig] = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
378 discarded[contig].append(pos)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
379 elif args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
380 sample_seq += args.reference[contig][pos - 1]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
381 elif args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
382 sample_seq += args.reference[contig]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
383
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
384 fp.write(">%s\n%s\n" % (sample, sample_seq))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
385 # Do the same for reference data.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
386 ref_snps = ""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
387
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
388 for contig in contigs:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
389 if contig in avail_pos:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
390 if args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
391 positions = xrange(1, len(args.reference[contig]) + 1)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
392 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
393 positions = avail_pos[contig].keys()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
394 for pos in positions:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
395 if pos in avail_pos[contig]:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
396 if not args.column_Ns or float(pos_stats[contig][pos]["N"]) / len(samples) < args.column_Ns and \
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
397 float(pos_stats[contig][pos]["-"]) / len(samples) < args.column_Ns:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
398
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
399 ref_snps += str(avail_pos[contig][pos])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
400 snp_positions.append((contig, pos,))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
401 elif args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
402 ref_snps += args.reference[contig][pos - 1]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
403 elif args.reference:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
404 ref_snps += args.reference[contig]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
405
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
406 fp.write(">reference\n%s\n" % ref_snps)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
407
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
408 if can_stats and args.with_stats:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
409 with open(args.with_stats, "wb") as fp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
410 fp.write("contig\tposition\tmutations\tn_frac\n")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
411 for values in snp_positions:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
412 fp.write("%s\t%s\t%s\t%s\n" % (values[0],
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
413 values[1],
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
414 float(pos_stats[values[0]][values[1]]["mut"]) / len(args.input),
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
415 float(pos_stats[values[0]][values[1]]["N"]) / len(args.input)))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
416 plot_stats(pos_stats, len(samples), discarded=discarded, plots_dir=os.path.abspath(args.plots_dir))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
417 # print_stats(sample_stats, pos_stats, total_vars=len(avail_pos[contig]))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
418
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
419 total_discarded = 0
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
420 for _, i in discarded.items():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
421 total_discarded += len(i)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
422 logging.info("Discarded total of %i poor quality columns", float(total_discarded) / len(args.input))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
423 logging.info("Samples with indels:")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
424 for sample, count in indel_summary.iteritems():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
425 logging.info("%s\t%s", sample, count)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
426 return 0
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
427
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
428 if __name__ == '__main__':
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
429 import time
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
430
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
431 # with PyCallGraph(output=graphviz):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
432 # T0 = time.time()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
433 r = main()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
434 # T1 = time.time()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
435
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
436 # print "Time taken: %i" % (T1 - T0)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
437 exit(r)