annotate vsnp_statistics.py @ 0:c21d338dbdc4 draft

Uploaded
author greg
date Tue, 21 Apr 2020 10:19:53 -0400
parents
children 14e29f7d59ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
1 #!/usr/bin/env python
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
2
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
3 import argparse
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
4 import gzip
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
5 import multiprocessing
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
6 import numpy
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
7 import os
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
8 import pandas
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
9 import queue
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
10
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
11 INPUT_IDXSTATS_DIR = 'input_idxstats'
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
12 INPUT_METRICS_DIR = 'input_metrics'
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
13 INPUT_READS_DIR = 'input_reads'
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
14 OUTPUT_DIR = 'output'
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
15 QUALITYKEY = {'!':'0', '"':'1', '#':'2', '$':'3', '%':'4', '&':'5', "'":'6', '(':'7', ')':'8', '*':'9', '+':'10', ',':'11', '-':'12', '.':'13', '/':'14', '0':'15', '1':'16', '2':'17', '3':'18', '4':'19', '5':'20', '6':'21', '7':'22', '8':'23', '9':'24', ':':'25', ';':'26', '<':'27', '=':'28', '>':'29', '?':'30', '@':'31', 'A':'32', 'B':'33', 'C':'34', 'D':'35', 'E':'36', 'F':'37', 'G':'38', 'H':'39', 'I':'40', 'J':'41', 'K':'42', 'L':'43', 'M':'44', 'N':'45', 'O':'46', 'P':'47', 'Q':'48', 'R':'49', 'S':'50', 'T':'51', 'U':'52', 'V':'53', 'W':'54', 'X':'55', 'Y':'56', 'Z':'57', '_':'1', ']':'1', '[':'1', '\\':'1', '\n':'1', '`':'1', 'a':'1', 'b':'1', 'c':'1', 'd':'1', 'e':'1', 'f':'1', 'g':'1', 'h':'1', 'i':'1', 'j':'1', 'k':'1', 'l':'1', 'm':'1', 'n':'1', 'o':'1', 'p':'1', 'q':'1', 'r':'1', 's':'1', 't':'1', 'u':'1', 'v':'1', 'w':'1', 'x':'1', 'y':'1', 'z':'1', ' ':'1'}
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
16 READCOLUMNS = ['Sample', 'Reference', 'Fastq File', 'Size', 'Total Reads', 'Mean Read Length', 'Mean Read Quality', 'Reads Passing Q30']
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
17 SEP = "\t"
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
18
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
19
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
20 def get_base_file_name(file_path):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
21 base_file_name = os.path.basename(file_path)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
22 if base_file_name.find(".") > 0:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
23 # Eliminate the extension.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
24 return os.path.splitext(base_file_name)[0]
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
25 elif base_file_name.find("_") > 0:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
26 # The dot extension was likely changed to
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
27 # the " character.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
28 items = base_file_name.split("_")
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
29 return "_".join(items[0:-1])
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
30 else:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
31 return base_file_name
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
32
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
33
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
34 def nice_size(size):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
35 # Returns a readably formatted string with the size
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
36 words = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
37 prefix = ''
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
38 try:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
39 size = float(size)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
40 if size < 0:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
41 size = abs(size)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
42 prefix = '-'
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
43 except Exception:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
44 return '??? bytes'
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
45 for ind, word in enumerate(words):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
46 step = 1024 ** (ind + 1)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
47 if step > size:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
48 size = size / float(1024 ** ind)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
49 if word == 'bytes': # No decimals for bytes
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
50 return "%s%d bytes" % (prefix, size)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
51 return "%s%.1f %s" % (prefix, size, word)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
52 return '??? bytes'
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
53
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
54
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
55 def output_read_stats(gzipped, fastq_file, ofh, sampling_number=10000, output_sample=False, dbkey=None, collection=False):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
56 file_name_base = os.path.basename(fastq_file)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
57 # Output a 2-column file where column 1 is
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
58 # the labels and column 2 is the values.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
59 if output_sample:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
60 # The Sample and Reference columns should be
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
61 # output only once, so this block handles
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
62 # paired reads, where the columns are not
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
63 # output for Read2.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
64 try:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
65 # Illumina read file names are something like:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
66 # 13-1941-6_S4_L001_R1_600000_fastq_gz
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
67 sample = file_name_base.split("_")[0]
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
68 except Exception:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
69 sample = ""
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
70 # Sample
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
71 ofh.write("Sample%s%s\n" % (SEP, sample))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
72 ofh.write("Reference%s%s\n" % (SEP, dbkey))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
73 if collection:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
74 read = "Read"
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
75 else:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
76 read = "Read1"
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
77 else:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
78 read = "Read2"
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
79 # Read
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
80 ofh.write("%s File%s%s\n" % (read, SEP, file_name_base))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
81 # File Size
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
82 ofh.write("%s File Size%s%s\n" % (read, SEP, nice_size(os.path.getsize(fastq_file))))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
83 if gzipped.lower() == "true":
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
84 df = pandas.read_csv(gzip.open(fastq_file, "r"), header=None, sep="^")
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
85 else:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
86 df = pandas.read_csv(open(fastq_file, "r"), header=None, sep="^")
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
87 total_read_count = int(len(df.index) / 4)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
88 # Readx Total Reads
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
89 ofh.write("%s Total Reads%s%s\n" % (read, SEP, total_read_count))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
90 # Mean Read Length
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
91 sampling_size = int(sampling_number)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
92 if sampling_size > total_read_count:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
93 sampling_size = total_read_count
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
94 df = df.iloc[3::4].sample(sampling_size)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
95 dict_mean = {}
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
96 list_length = []
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
97 for index, row in df.iterrows():
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
98 base_qualities = []
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
99 for base in list(row.array[0]):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
100 base_qualities.append(int(QUALITYKEY[base]))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
101 dict_mean[index] = numpy.mean(base_qualities)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
102 list_length.append(len(row.array[0]))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
103 ofh.write("%s Mean Read Length%s%s\n" % (read, SEP, "%.1f" % numpy.mean(list_length)))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
104 # Mean Read Quality
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
105 df_mean = pandas.DataFrame.from_dict(dict_mean, orient='index', columns=['ave'])
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
106 ofh.write("%s Mean Read Quality%s%s\n" % (read, SEP, "%.1f" % df_mean['ave'].mean()))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
107 # Reads Passing Q30
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
108 reads_gt_q30 = len(df_mean[df_mean['ave'] >= 30])
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
109 reads_passing_q30 = "{:10.2f}".format(reads_gt_q30 / sampling_size)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
110 ofh.write("%s reads passing Q30%s%s\n" % (read, SEP, reads_passing_q30))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
111 return total_read_count
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
112
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
113
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
114 def output_statistics(task_queue, read2, collection, gzipped, dbkey, timeout):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
115 while True:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
116 try:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
117 tup = task_queue.get(block=True, timeout=timeout)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
118 except queue.Empty:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
119 break
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
120 read_file, idxstats_file, metrics_file, output_file = tup
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
121 total_reads = 0
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
122 with open(output_file, "w") as ofh:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
123 total_reads += output_read_stats(gzipped, read_file, ofh, output_sample=True, dbkey=dbkey, collection=collection)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
124 if read2 is not None:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
125 total_reads += output_read_stats(gzipped, read2, ofh)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
126 ofh.write("Total Reads%s%d\n" % (SEP, total_reads))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
127 with open(idxstats_file, "r") as ifh:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
128 unmapped_reads = 0
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
129 for i, line in enumerate(ifh):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
130 items = line.split("\t")
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
131 if i == 0:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
132 # NC_002945.4 4349904 213570 4047
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
133 ofh.write("All Mapped Reads%s%s\n" % (SEP, items[2]))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
134 elif i == 1:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
135 # * 0 0 82774
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
136 unmapped_reads = int(items[3])
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
137 ofh.write("Unmapped Reads%s%d\n" % (SEP, unmapped_reads))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
138 percent_str = "Unmapped Reads Percentage of Total"
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
139 if unmapped_reads > 0:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
140 unmapped_reads_percentage = "{:10.2f}".format(unmapped_reads / total_reads)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
141 ofh.write("%s%s%s\n" % (percent_str, SEP, unmapped_reads_percentage))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
142 else:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
143 ofh.write("%s%s0\n" % (percent_str, SEP))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
144 with open(metrics_file, "r") as ifh:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
145 for i, line in enumerate(ifh):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
146 if i == 0:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
147 # Skip comments.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
148 continue
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
149 items = line.split("\t")
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
150 if i == 1:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
151 # MarkDuplicates 10.338671 98.74%
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
152 ofh.write("Average Depth of Coverage%s%s\n" % (SEP, items[2]))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
153 ofh.write("Reference with Coverage%s%s\n" % (SEP, items[3]))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
154 elif i == 2:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
155 # VCFfilter 611
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
156 ofh.write("Good SNP Count%s%s\n" % (SEP, items[1]))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
157 task_queue.task_done()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
158
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
159
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
160 def set_num_cpus(num_files, processes):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
161 num_cpus = int(multiprocessing.cpu_count())
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
162 if num_files < num_cpus and num_files < processes:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
163 return num_files
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
164 if num_cpus < processes:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
165 half_cpus = int(num_cpus / 2)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
166 if num_files < half_cpus:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
167 return num_files
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
168 return half_cpus
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
169 return processes
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
170
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
171
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
172 if __name__ == '__main__':
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
173 parser = argparse.ArgumentParser()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
174
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
175 parser.add_argument('--read1', action='store', dest='read1', required=False, default=None, help='Required: single read')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
176 parser.add_argument('--read2', action='store', dest='read2', required=False, default=None, help='Optional: paired read')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
177 parser.add_argument('--dbkey', action='store', dest='dbkey', help='Reference dbkey')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
178 parser.add_argument('--gzipped', action='store', dest='gzipped', help='Input files are gzipped')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
179 parser.add_argument('--samtools_idxstats', action='store', dest='samtools_idxstats', required=False, default=None, help='Output of samtools_idxstats')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
180 parser.add_argument('--output', action='store', dest='output', required=False, default=None, help='Output statisticsfile')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
181 parser.add_argument('--vsnp_azc', action='store', dest='vsnp_azc', required=False, default=None, help='Output of vsnp_add_zero_coverage')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
182 parser.add_argument('--processes', action='store', dest='processes', type=int, help='User-selected number of processes to use for job splitting')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
183
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
184 args = parser.parse_args()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
185
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
186 reads_files = []
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
187 idxstats_files = []
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
188 metrics_files = []
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
189 output_files = []
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
190 if args.output is not None:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
191 # The inputs were not dataset collections, so
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
192 # read1, read2 (possibly) and vsnp_azc will also
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
193 # not be None.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
194 collection = False
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
195 reads_files.append(args.read1)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
196 idxstats_files.append(args.samtools_idxstats)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
197 metrics_files.append(args.vsnp_azc)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
198 output_files.append(args.output)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
199 else:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
200 collection = True
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
201 for file_name in sorted(os.listdir(INPUT_READS_DIR)):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
202 file_path = os.path.abspath(os.path.join(INPUT_READS_DIR, file_name))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
203 reads_files.append(file_path)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
204 base_file_name = get_base_file_name(file_path)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
205 output_files.append(os.path.abspath(os.path.join(OUTPUT_DIR, "%s.tabular" % base_file_name)))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
206 for file_name in sorted(os.listdir(INPUT_IDXSTATS_DIR)):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
207 file_path = os.path.abspath(os.path.join(INPUT_IDXSTATS_DIR, file_name))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
208 idxstats_files.append(file_path)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
209 for file_name in sorted(os.listdir(INPUT_METRICS_DIR)):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
210 file_path = os.path.abspath(os.path.join(INPUT_METRICS_DIR, file_name))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
211 metrics_files.append(file_path)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
212
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
213 multiprocessing.set_start_method('spawn')
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
214 queue1 = multiprocessing.JoinableQueue()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
215 num_files = len(output_files)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
216 cpus = set_num_cpus(num_files, args.processes)
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
217 # Set a timeout for get()s in the queue.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
218 timeout = 0.05
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
219
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
220 for i, output_file in enumerate(output_files):
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
221 read_file = reads_files[i]
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
222 idxstats_file = idxstats_files[i]
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
223 metrics_file = metrics_files[i]
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
224 queue1.put((read_file, idxstats_file, metrics_file, output_file))
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
225
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
226 # Complete the output_statistics task.
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
227 processes = [multiprocessing.Process(target=output_statistics, args=(queue1, args.read2, collection, args.gzipped, args.dbkey, timeout, )) for _ in range(cpus)]
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
228 for p in processes:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
229 p.start()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
230 for p in processes:
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
231 p.join()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
232 queue1.join()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
233
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
234 if queue1.empty():
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
235 queue1.close()
c21d338dbdc4 Uploaded
greg
parents:
diff changeset
236 queue1.join_thread()