-
Notifications
You must be signed in to change notification settings - Fork 0
/
centrifuge_evaluate_mason.py
364 lines (322 loc) · 13.3 KB
/
centrifuge_evaluate_mason.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
import sys, os, subprocess, inspect
import platform, multiprocessing
import string, re
from datetime import datetime, date, time
import copy
from argparse import ArgumentParser, FileType
"""
"""
def read_taxonomy_tree(tax_file):
taxonomy_tree = {}
for line in tax_file:
fields = line.strip().split('\t')
assert len(fields) == 5
tax_id, parent_tax_id, rank = fields[0], fields[2], fields[4]
assert tax_id not in taxonomy_tree
taxonomy_tree[tax_id] = [parent_tax_id, rank]
return taxonomy_tree
"""
"""
def compare_scm(centrifuge_out, true_out, taxonomy_tree, rank):
higher_ranked = {}
ancestors = set()
for tax_id in taxonomy_tree.keys():
if tax_id in ancestors:
continue
while True:
parent_tax_id, cur_rank = taxonomy_tree[tax_id]
if parent_tax_id in ancestors:
break
if tax_id == parent_tax_id:
break
tax_id = parent_tax_id
ancestors.add(tax_id)
db_dic = {}
first = True
for line in open(centrifuge_out):
if first:
first = False
continue
read_name, seq_id, tax_id, score, _, _, _, _ = line.strip().split('\t')
# Traverse up taxonomy tree to match the given rank parameter
rank_tax_id = tax_id
if rank != "strain":
while True:
if tax_id not in taxonomy_tree:
rank_tax_id = ""
break
parent_tax_id, cur_rank = taxonomy_tree[tax_id]
if cur_rank == rank:
rank_tax_id = tax_id
break
if tax_id == parent_tax_id:
rank_tax_id = ""
break
tax_id = parent_tax_id
else:
assert rank == "strain"
if tax_id in ancestors:
continue
if rank_tax_id == "":
# higher_ranked[read_name] = True
continue
if read_name not in db_dic:
db_dic[read_name] = set()
db_dic[read_name].add(rank_tax_id)
classified, unclassified, unique_classified = 0, 0, 0
for line in open(true_out):
if line.startswith('@'):
continue
fields = line.strip().split('\t')
if len(fields) != 3:
print >> sys.stderr, "Warning: %s missing" % (line.strip())
continue
read_name, tax_id = fields[1:3]
# Traverse up taxonomy tree to match the given rank parameter
rank_tax_id = tax_id
if rank != "strain":
while True:
if tax_id not in taxonomy_tree:
rank_tax_id = ""
break
parent_tax_id, cur_rank = taxonomy_tree[tax_id]
if cur_rank == rank:
rank_tax_id = tax_id
break
if tax_id == parent_tax_id:
rank_tax_id = ""
break
tax_id = parent_tax_id
if rank_tax_id == "":
continue
if read_name not in db_dic:
unclassified += 1
continue
maps = db_dic[read_name]
if rank_tax_id in maps:
classified += 1
if len(maps) == 1 and read_name not in higher_ranked:
unique_classified += 1
else:
unclassified += 1
# daehwan - for debugging purposes
# print read_name
raw_unique_classified = 0
for read_name, maps in db_dic.items():
if len(maps) == 1 and read_name not in higher_ranked:
raw_unique_classified += 1
return classified, unique_classified, unclassified, len(db_dic), raw_unique_classified
"""
"""
def evaluate(index_base,
ranks,
verbose,
debug):
# Current script directory
curr_script = os.path.realpath(inspect.getsourcefile(evaluate))
path_base = os.path.dirname(curr_script) + "/.."
def check_files(fnames):
for fname in fnames:
if not os.path.exists(fname):
return False
return True
# Check if indexes exists, otherwise create indexes
index_path = "%s/indexes/Centrifuge" % path_base
# index_path = "."
if not os.path.exists(path_base + "/indexes"):
os.mkdir(path_base + "/indexes")
if not os.path.exists(index_path):
os.mkdir(index_path)
index_fnames = ["%s/%s.%d.cf" % (index_path, index_base, i+1) for i in range(3)]
assert check_files(index_fnames)
# Read taxonomic IDs
centrifuge_inspect = os.path.join(path_base, "../centrifuge-inspect")
tax_ids = set()
tax_cmd = [centrifuge_inspect,
"--conversion-table",
"%s/%s" % (index_path, "b+h+v")]
tax_proc = subprocess.Popen(tax_cmd, stdout=subprocess.PIPE)
for line in tax_proc.stdout:
_, tax_id = line.strip().split()
tax_ids.add(tax_id)
tax_ids = list(tax_ids)
# Read taxonomic tree
tax_tree_cmd = [centrifuge_inspect,
"--taxonomy-tree",
"%s/%s" % (index_path, "b+h+v")]
tax_tree_proc = subprocess.Popen(tax_tree_cmd, stdout=subprocess.PIPE, stderr=open("/dev/null", 'w'))
taxonomy_tree = read_taxonomy_tree(tax_tree_proc.stdout)
compressed = (index_base.find("compressed") != -1) or (index_base == "centrifuge_Dec_Bonly")
read_fname = "centrifuge_data/bacteria_sim10K/bacteria_sim10K.fa"
scm_fname = "centrifuge_data/bacteria_sim10K/bacteria_sim10K.truth_species"
read_fnames = [read_fname, scm_fname]
program_bin_base = "%s/.." % path_base
centrifuge_cmd = ["%s/centrifuge" % program_bin_base,
# "-k", "20",
# "--min-hitlen", "15",
"-f",
"-p", "1",
"%s/%s" % (index_path, index_base),
read_fname]
if verbose:
print >> sys.stderr, ' '.join(centrifuge_cmd)
out_fname = "centrifuge.output"
proc = subprocess.Popen(centrifuge_cmd, stdout=open(out_fname, "w"), stderr=subprocess.PIPE)
proc.communicate()
results = {"strain" : [0, 0, 0],
"species" : [0, 0, 0],
"genus" : [0, 0, 0],
"family" : [0, 0, 0],
"order" : [0, 0, 0],
"class" : [0, 0, 0],
"phylum" : [0, 0, 0]}
for rank in ranks:
if compressed and rank == "strain":
continue
classified, unique_classified, unclassified, raw_classified, raw_unique_classified = \
compare_scm(out_fname, scm_fname, taxonomy_tree, rank)
results[rank] = [classified, unique_classified, unclassified]
num_cases = classified + unclassified
# if rank == "strain":
# assert num_cases == num_fragment
print >> sys.stderr, "\t\t%s" % rank
print >> sys.stderr, "\t\t\tsensitivity: {:,} / {:,} ({:.2%})".format(classified, num_cases, float(classified) / num_cases)
print >> sys.stderr, "\t\t\tprecision : {:,} / {:,} ({:.2%})".format(classified, raw_classified, float(classified) / raw_classified)
print >> sys.stderr, "\n\t\t\tfor uniquely classified "
print >> sys.stderr, "\t\t\t\t\tsensitivity: {:,} / {:,} ({:.2%})".format(unique_classified, num_cases, float(unique_classified) / num_cases)
print >> sys.stderr, "\t\t\t\t\tprecision : {:,} / {:,} ({:.2%})".format(unique_classified, raw_unique_classified, float(unique_classified) / raw_unique_classified)
# Calculate sum of squared residuals in abundance
"""
if rank == "strain":
abundance_SSR = compare_abundance("centrifuge_report.tsv", truth_fname, taxonomy_tree, debug)
print >> sys.stderr, "\t\t\tsum of squared residuals in abundance: {}".format(abundance_SSR)
"""
# calculate true abundance
true_abundance = {}
total_sum = 0.0
num_genomes, num_species = 0, 0
for line in open("abundance.txt"):
seqID, taxID, genomeSize, reads, reads10K, genomeName = line.strip().split(',')[:6]
genomeSize, reads, reads10K = int(genomeSize), int(reads), int(reads10K)
if reads <= 0:
continue
num_genomes += 1
while True:
if taxID not in taxonomy_tree:
rank_taxID = ""
break
parent_taxID, rank = taxonomy_tree[taxID]
if rank == "species":
rank_taxID = taxID
break
if taxID == parent_taxID:
rank_taxID = ""
break
taxID = parent_taxID
if rank_taxID == "":
continue
assert rank == "species"
num_species += 1
total_sum += (reads / float(genomeSize))
if rank_taxID not in true_abundance:
true_abundance[rank_taxID] = 0.0
true_abundance[rank_taxID] += (reads / float(genomeSize))
for taxID, reads in true_abundance.items():
true_abundance[taxID] /= total_sum
print >> sys.stderr, "number of genomes:", num_genomes
print >> sys.stderr, "number of species:", num_species
print >> sys.stderr, "number of uniq species:", len(true_abundance)
read_fname = "centrifuge_data/bacteria_sim10M/bacteria_sim10M.fa"
summary_fname = "centrifuge.summary"
centrifuge_cmd = ["%s/centrifuge" % program_bin_base,
# "-k", "20",
# "--min-hitlen", "15",
"--report-file", summary_fname,
"-f",
"-p", "3",
"%s/%s" % (index_path, index_base),
read_fname]
if verbose:
print >> sys.stderr, ' '.join(centrifuge_cmd)
out_fname = "centrifuge.output"
proc = subprocess.Popen(centrifuge_cmd, stdout=open(out_fname, "w"), stderr=subprocess.PIPE)
proc.communicate()
calc_abundance = {}
for taxID in true_abundance.keys():
calc_abundance[taxID] = 0.0
first = True
for line in open(summary_fname):
if first:
first = False
continue
name, taxID, taxRank, genomeSize, numReads, numUniqueReads, abundance = line.strip().split("\t")
genomeSize, numReads, numUniqueReads, abundance = int(genomeSize), int(numReads), int(numUniqueReads), float(abundance)
calc_abundance[taxID] = abundance
# DK - for debugging purposes
"""
if taxID in true_abundance:
print >> sys.stderr, "%s: %.6f vs. %.6f" % (taxID, abundance, true_abundance[taxID])
"""
abundance_file = open("abundance.cmp", 'w')
print >> abundance_file, "taxID\ttrue\tcalc\trank"
for rank in ranks:
if rank == "strain":
continue
true_abundance_rank, calc_abundance_rank = {}, {}
for taxID in true_abundance.keys():
assert taxID in calc_abundance
rank_taxID = taxID
while True:
if rank_taxID not in taxonomy_tree:
rank_taxID = ""
break
parent_taxID, cur_rank = taxonomy_tree[rank_taxID]
if cur_rank == rank:
break
if rank_taxID == parent_taxID:
rank_taxID = ""
break
rank_taxID = parent_taxID
if rank_taxID not in true_abundance_rank:
true_abundance_rank[rank_taxID] = 0.0
calc_abundance_rank[rank_taxID] = 0.0
true_abundance_rank[rank_taxID] += true_abundance[taxID]
calc_abundance_rank[rank_taxID] += calc_abundance[taxID]
ssr = 0.0 # Sum of Squared Residuals
for taxID in true_abundance_rank.keys():
assert taxID in calc_abundance_rank
ssr += (true_abundance_rank[taxID] - calc_abundance_rank[taxID]) ** 2
print >> abundance_file, "%s\t%.6f\t%.6f\t%s" % (taxID, true_abundance_rank[taxID], calc_abundance_rank[taxID], rank)
print >> sys.stderr, "%s) Sum of squared residuals: %.6f" % (rank, ssr)
abundance_file.close()
if __name__ == "__main__":
parser = ArgumentParser(
description='Centrifuge evaluation on Mason simulated reads')
parser.add_argument("--index-base",
type=str,
default="b_compressed",
help='Centrifuge index such as b_compressed, b+h+v, and centrifuge_Dec_Bonly (default: b_compressed)')
rank_list_default = "strain,species,genus,family,order,class,phylum"
parser.add_argument("--rank-list",
dest="ranks",
type=str,
default=rank_list_default,
help="A comma-separated list of ranks (default: %s)" % rank_list_default)
parser.add_argument('-v', '--verbose',
dest='verbose',
action='store_true',
help='also print some statistics to stderr')
parser.add_argument('--debug',
dest='debug',
action='store_true',
help='Debug')
args = parser.parse_args()
if not args.index_base:
parser.print_help()
exit(1)
ranks = args.ranks.split(',')
evaluate(args.index_base,
ranks,
args.verbose,
args.debug)