-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccuracy.py
734 lines (628 loc) · 28.5 KB
/
accuracy.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
from Bio import SeqIO
from sklearn.metrics import accuracy_score, confusion_matrix
import os
from os import listdir
from os.path import isfile, join
import numpy as np
import joblib
import random
# Set seed to ensure deterministic output
seed = 0
random.seed(seed)
np.random.seed(seed)
'''
Inputs:
lst: list, list of lengths whose optimal threshold has been calculated
K: single number, length of sequence whose optimal threshold is to be determined
Outputs:
single number, the element in lst that is closest to K
closest determines the element in the input list "lst" that is closest to the integer
'''
def closest(lst, K):
return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))]
'''
seqid2taxid is a dictionary whose values are sequence ids and the keys are the taxonomy id
corresponding to the sequence id
'''
seqid2taxid = {}
with open('models_and_references/seqid2taxid_abfpvHost.txt', 'r') as f:
for line in f:
seqid2taxid[line.split('\t')[1][:-1]] = line.split('\t')[0]
'''
hosts contains all the species to be considered host
'''
hosts = ['Homo sapiens', 'Mus musculus', 'Sus scrofa']
'''
dict_ref is a dictionary whose keys are sequence ids and whose values are the species corresponding to that sequence id
'''
dict_ref = joblib.load('models_and_references/dict_ref')
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile: the output file of Kraken2-H containing Kraken2-H's predicted label for all sequence ids
in the text file
Outputs:
accuracy: Kraken2-H's accuracy at separating host from microbial sequences
sensitivity: Kraken2-H's sensitivity at separating host from microbial sequences
specificity: Kraken2-H's specificity at separating host from microbial sequences
kraken_rhd takes in the true labels of a set of DNA sequences and their predicted labels from Kraken2-H (Kraken2 with only host DNA in its index) and outputs the accuracy, sensitivity, and specificity of Kraken2-H at separating host from microbial sequences
'''
def kraken_rhd(truefile, predfile):
true_dict = {}
with open(truefile, 'r') as f:
for line in f:
if line.split(', ')[1][:-1] in hosts:
true_dict[line.split(', ')[0]] = 1
else:
true_dict[line.split(', ')[0]] = 0
pred_dict = {}
with open(predfile, 'r') as f:
for line in f:
taxid = line.split('\t')[2]
if taxid in seqid2taxid.keys():
seqid = seqid2taxid[taxid]
label = dict_ref[seqid]
if label in hosts:
pred_dict[line.split('\t')[1]] = 1
else:
pred_dict[line.split('\t')[1]] = 0
else:
pred_dict[line.split('\t')[1]] = 0
true = []
preds = []
for i in list(true_dict.keys()):
true.append(true_dict[i])
preds.append(pred_dict[i])
accuracy = accuracy_score(true, preds)
tn, fp, fn, tp = confusion_matrix(true, preds).ravel()
sens = tp/(tp + fn)
spec = tn/(tn + fp)
return accuracy, sens, spec
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile: the output file of Centrifuge-H containing Centrifuge-H's predicted label for all sequence ids
in the text file
Outputs:
accuracy: Centrifuge-H's accuracy at separating host from microbial sequences
sensitivity: Centrifuge-H's sensitivity at separating host from microbial sequences
specificity: Centrifuge-H's specificity at separating host from microbial sequences
centrifuge_rhd takes in the true labels of a set of DNA sequences and their predicted labels from Centrifuge-H (Centrifuge with only host DNA in its index) and outputs the accuracy, sensitivity, and specificity of Centrifuge-H at separating host from microbial sequences
'''
def centrifuge_rhd(truefile, predfile):
true_dict = {}
with open(truefile, 'r') as f:
for line in f:
if line.split(', ')[1][:-1] in hosts:
true_dict[line.split(', ')[0]] = 1
else:
true_dict[line.split(', ')[0]] = 0
pred_dict = {}
count = 0
with open(predfile, 'r') as f:
for line in f:
if count > 0:
taxid = line.split('\t')[2]
if taxid in seqid2taxid.keys():
seqid = seqid2taxid[taxid]
label = dict_ref[seqid]
if label in hosts:
pred_dict[line.split('\t')[0]] = 1
else:
pred_dict[line.split('\t')[0]] = 0
else:
pred_dict[line.split('\t')[0]] = 0
count += 1
true = []
preds = []
print(len(true_dict.keys()), len(pred_dict.keys()))
for i in list(true_dict.keys()):
if i in true_dict.keys():
true.append(true_dict[i])
elif i + '/1' in true_dict.keys():
true.append(true_dict[i+'/1'])
preds.append(pred_dict[i])
accuracy = accuracy_score(true, preds)
tn, fp, fn, tp = confusion_matrix(true, preds).ravel()
sens = tp/(tp + fn)
spec = tn/(tn + fp)
return accuracy, sens, spec
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile: the output file of Bowtie2-H containing Bowtie2-H's predicted label for all sequence ids
in the text file
Outputs:
accuracy: Bowtie2-H's accuracy at separating host from microbial sequences
sensitivity: Bowtie2-H's sensitivity at separating host from microbial sequences
specificity: Bowtie2-H's specificity at separating host from microbial sequences
bowtie_rhd takes in the true labels of a set of DNA sequences and their predicted labels from Bowtie2-H (Bowtie2 with only host DNA in its index) and outputs the accuracy, sensitivity, and specificity of Bowtie2-H at separating host from microbial sequences
'''
def bowtie_rhd(truefile, predfile):
true_dict = {}
with open(truefile, 'r') as f:
for line in f:
if line.split(', ')[1][:-1] in hosts:
true_dict[line.split(', ')[0]] = 1
else:
true_dict[line.split(', ')[0]] = 0
pred_dict = {}
with open(predfile, 'r') as f:
for line in f:
if line[0] != '@':
input_seq = line.split()[0]
rec_id = line.split()[2]
pred_dict[input_seq] = 1
true = []
preds = []
for i in list(true_dict.keys()):
true.append(true_dict[i])
if i not in pred_dict.keys():
preds.append(0)
else:
preds.append(pred_dict[i])
accuracy = accuracy_score(true, preds)
tn, fp, fn, tp = confusion_matrix(true, preds).ravel()
sens = tp/(tp + fn)
spec = tn/(tn + fp)
return accuracy, sens, spec
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile: the output file of Minimap2-H containing Minimap2-H's predicted label for all sequence ids
in the text file
Outputs:
accuracy: Minimap2-H's accuracy at separating host from microbial sequences
sensitivity: Minimap2-H's sensitivity at separating host from microbial sequences
specificity: Minimap2-H's specificity at separating host from microbial sequences
minimap_rhd takes in the true labels of a set of DNA sequences and their predicted labels from Minimap2-H (Minimap2 with only host DNA in its index) and outputs the accuracy, sensitivity, and specificity of Minimap2-H at separating host from microbial sequences
'''
def minimap_rhd(truefile, predfile):
true_dict = {}
with open(truefile, 'r') as f:
for line in f:
if line.split(', ')[1][:-1] in hosts:
true_dict[line.split(', ')[0]] = 1
else:
true_dict[line.split(', ')[0]] = 0
pred_dict = {}
with open(predfile, 'r') as f:
for line in f:
if line[0] != '@':
input_seq = line.split()[0]
rec_id = line.split()[2]
if rec_id != '*' and rec_id in dict_ref.keys():
pred_dict[input_seq] = 1
else:
pred_dict[input_seq] = 0
true = []
preds = []
for i in list(true_dict.keys()):
true.append(true_dict[i])
preds.append(pred_dict[i])
accuracy = accuracy_score(true, preds)
tn, fp, fn, tp = confusion_matrix(true, preds).ravel()
sens = tp/(tp + fn)
spec = tn/(tn + fp)
return accuracy, sens, spec
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile: the output file of AMAISE containing the probabilities of each sequence being from a host
Outputs:
accuracy: AMAISE's accuracy at separating host from microbial sequences
sensitivity: AMAISE's sensitivity at separating host from microbial sequences
specificity: AMAISE's specificity at separating host from microbial sequences
ml_rhd takes in the true labels of a set of DNA sequences and the probabilities that they are from a host from AMAISE and outputs the accuracy, sensitivity, and specificity of AMAISE at separating host from microbial sequences
'''
def ml_rhd(truefile, predfile, threshs):
true_dict = {}
with open(truefile, 'r') as f:
for line in f:
if line.split(', ')[1][:-1] in hosts:
true_dict[line.split(', ')[0]] = 1
else:
true_dict[line.split(', ')[0]] = 0
pred_dict = {}
count = 0
with open(predfile, 'r') as f:
for line in f:
if count > 0:
seqid = line.split(', ')[0]
pred = float(line.split(', ')[1])
final_len = closest(list(threshs.keys()), int(line.split(', ')[2]))
if pred > threshs[final_len]:
pred_dict[seqid] = 1
else:
pred_dict[seqid] = 0
count += 1
true = []
preds = []
for i in list(true_dict.keys()):
true.append(true_dict[i])
preds.append(pred_dict[i])
accuracy = accuracy_score(true, preds)
tn, fp, fn, tp = confusion_matrix(true, preds).ravel()
sens = tp/(tp + fn)
spec = tn/(tn + fp)
return accuracy, sens, spec
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile: the output file of Kraken2-HM containing Kraken2-HM's predicted label for all sequence ids
ML_outputfile: the output file of AMAISE containing the probabilities of each sequence being from a host
threshs: the thresholds used to convert AMAISE's output probabilities into classification labels
Outputs:
overall_acc: the accuracy of Kraken2-HM at classifying the test sequences
filter_acc: the accuracy of Kraken2-HM at classifying the sequences AMAISE classified as microbial
unfilter_acc: the accuracy of Kraken2-HM at classifying the sequences AMAISE classified as microbial
host_acc: the accuracy of Kraken2-HM at classifying the sequences whose true label is host
microbe_acc: the accuracy of Kraken2-HM at classifying the sequences whose true label is microbial
kraken_acc takes in the true labels of a set of DNA sequences and their predicted labels from Kraken2-HM and outputs Kraken2-HM's overall accuracy, Kraken2-HM's accuracy on the sequences AMAISE classified as microbial, Kraken2-HM's accuracy on the sequences AMAISE classified as host, Kraken2-HM's accuracy on the sequences whose true label is microbial, and Kraken2-HM's accuracy on the sequences whose true label is host
host_acc and microbe_acc are reported in the paper
'''
def kraken_acc(truefile, predfile, ML_outputfile, threshs):
filter_ids = []
unfilter_ids = []
count = 0
with open(ML_outputfile, 'r') as f:
for line in f:
if count > 0:
seqid = line.split(', ')[0]
pred = float(line.split(', ')[1])
final_len = closest(list(threshs.keys()), int(line.split(', ')[2]))
if pred <= threshs[final_len]:
filter_ids.append(seqid)
else:
unfilter_ids.append(seqid)
count += 1
true_dict = {}
unique_labels = []
with open(truefile, 'r') as f:
for line in f:
true_dict[line.split(', ')[0]] = line.split(', ')[1][:-1].split(',')[0]
unique_labels.append(line.split(', ')[1][:-1].split(',')[0])
unique_labels = list(set(unique_labels))
unique_labels.append('None')
unique_labels_dict = {}
for i in range(len(unique_labels)):
unique_labels_dict[unique_labels[i]] = i
pred_dict = {}
with open(predfile, 'r') as f:
for line in f:
taxid = line.split('\t')[2]
if taxid in seqid2taxid.keys():
seqid = seqid2taxid[taxid]
label = dict_ref[seqid]
pred_dict[line.split('\t')[1]] = label
if label not in unique_labels:
pred_dict[line.split('\t')[1]] = 'None'
else:
pred_dict[line.split('\t')[1]] = 'None'
overall_count = len(filter_ids) + len(unfilter_ids)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
overall_acc = accuracy_score(true, preds)
true = []
preds = []
for i in filter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
filter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in unfilter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
unfilter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
host_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] not in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
microbe_acc = accuracy_score(true, preds)
return overall_acc, filter_acc, unfilter_acc, host_acc, microbe_acc
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile: the output file of Centrifuge-HM containing Centrifuge-HM's predicted label for all sequence ids
ML_outputfile: the output file of AMAISE containing the probabilities of each sequence being from a host
threshs: the thresholds used to convert AMAISE's output probabilities into classification labels
Outputs:
overall_acc: the accuracy of Centrifuge-HM at classifying the test sequences
filter_acc: the accuracy of Centrifuge-HM at classifying the sequences AMAISE classified as microbial
unfilter_acc: the accuracy of Centrifuge-HM at classifying the sequences AMAISE classified as microbial
host_acc: the accuracy of Centrifuge-HM at classifying the sequences whose true label is host
microbe_acc: the accuracy of Centrifuge-HM at classifying the sequences whose true label is microbial
centrifuge_acc takes in the true labels of a set of DNA sequences and their predicted labels from Centrifuge-HM and outputs Centrifuge-HM's overall accuracy, Centrifuge-HM's accuracy on the sequences AMAISE classified as microbial, Centrifuge-HM's accuracy on the sequences AMAISE classified as host, Centrifuge-HM's accuracy on the sequences whose true label is microbial, and Centrifuge-HM's accuracy on the sequences whose true label is host
host_acc and microbe_acc are reported in the paper
'''
def centrifuge_acc(truefile, predfile, ML_outputfile, threshs):
count = 0
filter_ids = []
unfilter_ids = []
with open(ML_outputfile, 'r') as f:
for line in f:
if count > 0:
seqid = line.split(', ')[0]
pred = float(line.split(', ')[1])
final_len = closest(list(threshs.keys()), int(line.split(', ')[2]))
if pred <= threshs[final_len]:
filter_ids.append(seqid)
else:
unfilter_ids.append(seqid)
count += 1
true_dict = {}
unique_labels = []
with open(truefile, 'r') as f:
for line in f:
true_dict[line.split(', ')[0]] = line.split(', ')[1][:-1].split(',')[0]
unique_labels.append(line.split(', ')[1][:-1].split(',')[0])
unique_labels = list(set(unique_labels))
unique_labels.append('None')
unique_labels_dict = {}
for i in range(len(unique_labels)):
unique_labels_dict[unique_labels[i]] = i
pred_dict = {}
count = 0
with open(predfile, 'r') as f:
for line in f:
if count > 0:
taxid = line.split('\t')[2]
if taxid in seqid2taxid.keys():
seqid = seqid2taxid[taxid]
label = dict_ref[seqid]
pred_dict[line.split('\t')[0]] = label
if label not in unique_labels:
pred_dict[line.split('\t')[0]] = 'None'
else:
pred_dict[line.split('\t')[0]] = 'None'
count += 1
overall_count = len(filter_ids) + len(unfilter_ids)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
overall_acc = accuracy_score(true, preds)
true = []
preds = []
for i in filter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
filter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in unfilter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
unfilter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
host_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] not in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
microbe_acc = accuracy_score(true, preds)
return overall_acc, filter_acc, unfilter_acc, host_acc, microbe_acc
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile1: the output file of AMAISE containing the probabilities of each sequence being from a host
predfile2: the output file of Kraken2-M containing the Kraken2-M's predicted label for the sequences AMAISE labeled as microbial
threshs: the thresholds used to convert AMAISE's output probabilities into classification labels
Outputs:
overall_acc: the accuracy of AMAISE + Kraken2-M at classifying the test sequences
filter_acc: the accuracy of AMAISE + Kraken2-M at classifying the sequences AMAISE classified as microbial
unfilter_acc: the accuracy of AMAISE + Kraken2-M at classifying the sequences AMAISE classified as microbial
host_acc: the accuracy of AMAISE + Kraken2-M at classifying the sequences whose true label is host
microbe_acc: the accuracy of AMAISE + Kraken2-M at classifying the sequences whose true label is microbial
krakenML_acc_mults takes in the true labels of a set of DNA sequences, AMAISE's output, and Kraken2-M's output given the sequences that AMAISE classified as microbial and outputs AMAISE + Kraken2-M's overall accuracy, AMAISE + Kraken2-M's accuracy on the sequences AMAISE classified as microbial, AMAISE + Kraken2-M's accuracy on the sequences AMAISE classified as host, AMAISE + Kraken2-M's accuracy on the sequences whose true label is microbial, and AMAISE + Kraken2-M's accuracy on the sequences whose true label is host
host_acc and microbe_acc are reported in the paper
'''
def krakenML_acc_mults(truefile, predfile1, predfile2, threshs):
true_dict = {}
unique_labels = []
with open(truefile, 'r') as f:
for line in f:
true_dict[line.split(', ')[0]] = line.split(', ')[1][:-1].split(',')[0]
unique_labels.append(line.split(', ')[1][:-1].split(',')[0])
unique_labels = list(set(unique_labels))
unique_labels.append('None')
unique_labels_dict = {}
for i in range(len(unique_labels)):
unique_labels_dict[unique_labels[i]] = i
count = 0
pred_dict = {}
filter_ids = []
unfilter_ids = []
with open(predfile1, 'r') as f:
for line in f:
if count > 0:
seqid = line.split(', ')[0]
pred = float(line.split(', ')[1])
final_len = closest(list(threshs.keys()), int(line.split(', ')[2]))
if pred > threshs[final_len]:
pred_dict[seqid] = 'Homo sapiens'
unfilter_ids.append(seqid)
else:
filter_ids.append(seqid)
count += 1
with open(predfile2, 'r') as f:
for line in f:
taxid = line.split('\t')[2]
if taxid in seqid2taxid.keys():
seqid = seqid2taxid[taxid]
label = dict_ref[seqid]
pred_dict[line.split('\t')[1]] = label
if label not in unique_labels:
pred_dict[line.split('\t')[1]] = 'None'
else:
pred_dict[line.split('\t')[1]] = 'None'
overall_count = len(filter_ids) + len(unfilter_ids)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
overall_acc = accuracy_score(true, preds)
true = []
preds = []
for i in filter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
filter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in unfilter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
unfilter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
host_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] not in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
microbe_acc = accuracy_score(true, preds)
return overall_acc, filter_acc, unfilter_acc, host_acc, microbe_acc
'''
Inputs:
truefile: a file containing the sequence ids of the test file and their true labels
predfile1: the output file of AMAISE containing the probabilities of each sequence being from a host
predfile2: the output file of Centrifuge-M containing the Centrifuge-M's predicted label for the sequences AMAISE labeled as microbial
threshs: the thresholds used to convert AMAISE's output probabilities into classification labels
Outputs:
overall_acc: the accuracy of AMAISE + Centrifuge-M at classifying the test sequences
filter_acc: the accuracy of AMAISE + Centrifuge-M at classifying the sequences AMAISE classified as microbial
unfilter_acc: the accuracy of AMAISE + Centrifuge-M at classifying the sequences AMAISE classified as microbial
host_acc: the accuracy of AMAISE + Centrifuge-M at classifying the sequences whose true label is host
microbe_acc: the accuracy of AMAISE + Centrifuge-M at classifying the sequences whose true label is microbial
centrifugeML_acc_mults takes in the true labels of a set of DNA sequences, AMAISE's output, and Centrifuge-M's output given the sequences that AMAISE classified as microbial and outputs AMAISE + Centrifuge-M's overall accuracy, AMAISE + Centrifuge-M's accuracy on the sequences AMAISE classified as microbial, AMAISE + Centrifuge-M's accuracy on the sequences AMAISE classified as host, AMAISE + Centrifuge-M's accuracy on the sequences whose true label is microbial, and AMAISE + Centrifuge-M's accuracy on the sequences whose true label is host
host_acc and microbe_acc are reported in the paper
'''
def centrifugeML_acc_mults(truefile, predfile1, predfile2, threshs):
true_dict = {}
unique_labels = []
with open(truefile, 'r') as f:
for line in f:
true_dict[line.split(', ')[0]] = line.split(', ')[1][:-1].split(',')[0]
unique_labels.append(line.split(', ')[1][:-1].split(',')[0])
unique_labels = list(set(unique_labels))
unique_labels.append('None')
unique_labels_dict = {}
for i in range(len(unique_labels)):
unique_labels_dict[unique_labels[i]] = i
count = 0
pred_dict = {}
filter_ids = []
unfilter_ids = []
with open(predfile1, 'r') as f:
for line in f:
if count > 0:
seqid = line.split(', ')[0]
pred = float(line.split(', ')[1])
final_len = closest(list(threshs.keys()), int(line.split(', ')[2]))
if pred > threshs[final_len]:
pred_dict[seqid] = 'Homo sapiens'
unfilter_ids.append(seqid)
else:
filter_ids.append(seqid)
count += 1
count = 0
with open(predfile2, 'r') as f:
for line in f:
if count > 0:
taxid = line.split('\t')[2]
if taxid in seqid2taxid.keys():
seqid = seqid2taxid[taxid]
label = dict_ref[seqid]
pred_dict[line.split('\t')[0]] = label
if label not in unique_labels:
pred_dict[line.split('\t')[0]] = 'None'
else:
pred_dict[line.split('\t')[0]] = 'None'
count += 1
overall_count = len(filter_ids) + len(unfilter_ids)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
overall_acc = accuracy_score(true, preds)
true = []
preds = []
for i in filter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
filter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in unfilter_ids:
if true_dict[i] in hosts and pred_dict[i] in hosts:
pred_dict[i] = true_dict[i]
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
unfilter_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
host_acc = accuracy_score(true, preds)
true = []
preds = []
for i in list(true_dict.keys()):
if true_dict[i] not in hosts:
true.append(unique_labels_dict[true_dict[i]])
preds.append(unique_labels_dict[pred_dict[i]])
microbe_acc = accuracy_score(true, preds)
return overall_acc, filter_acc, unfilter_acc, host_acc, microbe_acc