-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreference_trees.py
1195 lines (894 loc) · 41.2 KB
/
reference_trees.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from builtins import zip
from builtins import str
from builtins import range
from past.builtins import basestring
from builtins import object
import re
from collections import Counter
import xml.etree.ElementTree as ET
from ete3 import NCBITaxa, Tree, TreeStyle, NodeStyle, TextFace, faces, AttrFace, PhyloTree
from Bio import Entrez, AlignIO, Phylo
from rate_limit import RateLimit
from ncbi_taxa import ncbiTaxa
#from fuzzy_text_matching import sw
import pandas as pd
# configuration
# nmicrobiol201648_s4 = "./data/nmicrobiol201648-s4.txt.fixed"
# nmicrobiol201648_s5 = "./data/nmicrobiol201648-s5.txt" # =Alignment
# nmicrobiol201648_s6 = "./data/nmicrobiol201648-s6.txt" # =Tree
# nmicrobiol201648_s6_PATHd8 = "./data/nmicrobiol201648-s6.txt.nw.PATHd8.out.d8.nw" # =Tree
# nmicrobiol201648_s8 = "./data/nmicrobiol201648-s8.txt"
# itol_newick = "./data/itol_newick.txt"
# Entrez.email = "[email protected]"
# unhandledXMLsFile = "reference_tree.unhandled_xml.txt"
# nodeIdentifiersMappingTable_csv = "reference_trees.hug.identifiers.csv"
# nodeIdentifiersMappingTable_xls = "reference_trees.hug.identifiers.xlsx"
# nodeIdentifiersMappingTable_with_testing_csv = "reference_trees.hug.identifiers.with_testing.csv"
# nodeIdentifiersMappingTable_with_inclusion_csv = "reference_trees.hug.included_for_itol.csv"
# alignmentIdentifiersToTreeIdentifiersMappingTable_csv = "reference_trees.hug.alignment_ids.csv"
##taxonItemsToIgnore = frozenset(("Unclassified", "Incertae", "Candidate", "candidate", "Candidatus", "incertae", "sedis"))
#taxonItemsToIgnore = frozenset(("Unclassified", "Incertae", "incertae", "sedis"))
reNodeSupport = re.compile('[[][^]]*[]]')
def stripTreeInternalSupport(tree):
return re.sub(reNodeSupport, '', tree)
rl = RateLimit(15)
def getContainingSpeciesLevelTaxon(taxId):
lineage = ncbiTaxa.get_lineage(taxId)
ranks = ncbiTaxa.get_rank(lineage)
for taxon in reversed(lineage):
rank = ranks[taxon]
if rank == "no rank":
continue
elif rank == "subspecies":
continue
elif rank == "forma":
continue
elif rank == "varietas":
continue
elif rank == "species":
return taxon
else:
#print("--"*50)
#print(taxId)
#print("Lineage: %s" % lineage)
#print("Ranks: %s" % ranks)
#print(ncbiTaxa.get_taxid_translator(lineage))
return None
# class FuzzyTaxonMatcher(object):
# def __init__(self, requiredQueryIdentity=0.6):
# self._requiredQueryIdentity = requiredQueryIdentity
# self._db = ncbiTaxa.db # use NCBITaxa's sqlite connection
# def match(self, query, scope, verbose=False):
# out = []
# for taxId, ncbiMatch in self._db.execute('select taxid, spname from species where spname like "%s%%";' % scope).fetchall(): # TODO where rank in (111,222,333)
# match = sw.align( ncbiMatch, query)
# out.append( (taxId, ncbiMatch, match, match.score) )
# out.sort(key = lambda x:-(x[3]) )
# if verbose:
# for x in out[:3]:
# print("--"*20)
# print("%d %s" % (x[0], x[1]))
# print( "score: %d matches: %d" % (x[2].score, x[2].matches))
# x[2].dump()
# else:
# #out[0][2].dump()
# pass
# if not out:
# return None
# if out[0][2].matches < len(query)*self._requiredQueryIdentity:
# return None
# if len(out)>1 and not ( ( out[0][3] > out[1][3]) and (out[0][2].matches > out[1][2].matches ) ):
# return None
# return (out[0][0], out[0][1], float(out[0][2].matches)/len(query))
def pruneTree(tree, keepNodes=None, keepTaxIds=None, saveTreeAs=None):
# Collect all taxids to keep (based on keepNodes and keepTaxIds):
_allTaxIdsToKeep = set()
# first, if we got nodes, extract the taxid from each one
if( not keepNodes is None ):
for x in keepNodes:
if not x.taxId is None:
_allTaxIdsToKeep.add(x.taxId)
# second, if we got taxids, also add them to the list of kept taxids
if( not keepTaxIds is None ):
_allTaxIdsToKeep = _allTaxIdsToKeep.union( frozenset(keepTaxIds) )
print("Pruning, will keep %d nodes" % len(_allTaxIdsToKeep))
for node in tree.traverse(strategy='postorder'):
childTaxIds = set()
for x in node.get_leaves():
try:
if not x.taxId is None:
childTaxIds.add(x.taxId)
except AttributeError as e:
pass
#print(len(childTaxIds))
if not _allTaxIdsToKeep.intersection(childTaxIds):
node.detach()
#node.delete()
#print(".")
nn = []
for node in tree.traverse():
if node.is_leaf():
nn.append(node)
print("nn = %d" % len(nn))
print(len(tree))
tree.prune(nn, preserve_branch_length=True)
print(len(tree))
print("Expected: %d Actual: %d" % (len(_allTaxIdsToKeep), len(tree)))
if not saveTreeAs is None:
tree.write(format=1, outfile=saveTreeAs)
#assert(len(tree) <= len(taxIds))
return tree
def pruneTree2(tree, keepNodes):
tree.prune(keepNodes)
def pruneTreeByTaxonomy(tree, parentTaxonIdToKeep):
allKeepTaxIds = set()
# Collect all child taxons whose lineage includes the specified parent
for node in tree.traverse():
if node.is_leaf():
leafTaxId = getattr(node, "taxId", None)
if (not leafTaxId is None):
lineage = frozenset(ncbiTaxa.get_lineage(node.taxId))
if( parentTaxonIdToKeep in lineage ):
allKeepTaxIds.add( node.taxId )
return pruneTree(tree, keepTaxIds=allKeepTaxIds )
def getTaxidsFromTree(tree):
out = []
for node in tree.traverse():
if node.is_leaf():
if (not node.taxId is None):
out.append(node.taxId)
return out
def extendTreeWithSpecies( tree, additionalSpecies, limitTaxonomy=None ):
# Collect the tax-ids of all species included in the tree
realTreeSpecies = frozenset(getTaxidsFromTree(tree))
for taxId in additionalSpecies:
if taxId in realTreeSpecies: continue # this species is included in the tree, no need to append it
if not limitTaxonomy is None:
lineage = frozenset(ncbiTaxa.get_lineage(taxId))
if( not limitTaxonomy in lineage ): # this species does not belong to the specified taxon
continue
# Create a compatible node for this species, and add it to the tree as an outgroup
newNode = tree.add_child( name=str(taxId), dist=0.1 )
newNode.add_features( taxId = taxId, label = taxId, dummyTopology = True )
return tree
def readTranslationMap():
treeNodeIdentifiersDf = pd.read_csv(nodeIdentifiersMappingTable_with_testing_csv, dtype={'NodeLabel': 'string', 'DBIdentifier': 'string', 'DBIdentifierType': 'category', 'TaxId': 'int32'} )
out = {}
for row in treeNodeIdentifiersDf.itertuples():
label = row.NodeLabel
taxId = row.TaxId
if not taxId is None and taxId != 0:
out[label] = taxId
return out
def inferTaxIdForLabel(label):
# Try to match the species name with the NCBI taxonomy
# The node names contain concatenated taxon names and we don't know where the species name starts.
# We will collect candidate strings (based on the criteria below) and choose the candidate that has a match in NCBI.
#
# See also: FuzzyMatch in R 'evobiR' package (https://cran.r-project.org/web/packages/evobiR/index.html)
#
items = label.split("_") # "items" (words) are separated by '_'
candidates = []
for i in range(len(items)-1, 0, -1): # process suffixes (strings containing last N items), from shortest to longest
candidate = " ".join(items[i:])
# Filter candidates that are too short or too long to be a binomial species name (with optional strain description)
if len(candidate) < 8:
continue # skip this suffix
if len(candidate) > 100:
break # skip this and longer suffixes
if items[i] in taxonItemsToIgnore:
continue
if candidate.find("ncertae sedis") > -1:
continue
candidates.append( (i,candidate,-1) )
# For each suffix, also consider its prefixes as candidates. This is bacause the species names sometimes include additional strain identifiers that are not included in NCBI
if i<=len(items)-2 and items[i] and items[i+1] and items[i][0].isupper() and items[i+1][0].islower(): # require that the first item start with a capital, and the second item start with a lower-case letter (e.g., "Eschericia coli")
for j in range(i+1,len(items)-1):
candidate = " ".join(items[i:j+1])
candidates.append( (i,candidate,j+1) )
if " ".join(items).find("Curtiss") > -1:
print("="*50)
print(items)
print(candidates)
print("="*50)
# Sort candidates by priority - from longest to shortest
candidates = sorted(candidates, key=lambda x:-len(x[1]))
# Finished collecting candidates; match them in NCBI
# Note: matching all names in a single call is much faster
ncbiMatches = ncbiTaxa.get_name_translator([x[1] for x in candidates])
speciesStartItem = None
speciesLevelTaxon = None
matchingName = None
matchingTaxId = None
# Find the first (i.e., longest) match
for candidateStartItem, candidateName, _ in candidates:
if candidateName in ncbiMatches:
candidateTaxId = ncbiMatches[candidateName][0]
speciesLevelTaxon = getContainingSpeciesLevelTaxon( candidateTaxId )
if speciesLevelTaxon is None: # Make sure this taxon is not above species rank
continue
# select this candidate
matchingName = candidateName
matchingTaxId = candidateTaxId
speciesStartItem = candidateStartItem # remember at what item the selected species starts
break
return (matchingTaxId, matchingName)
def pruneReferenceTree_Nmicrobiol201648(taxa):
treefmt = None
with open(nmicrobiol201648_s6_PATHd8, "r") as f:
#with open(itol_newick, "r") as f:
treefmt = f.read()
# Strip internal support, and parse the resulting string
tree = PhyloTree(stripTreeInternalSupport(treefmt))
#ts.show_leaf_name = True
#ts.layout_fn = nodeLayoutWithTaxonomicNames
unmatched = []
matched = []
#xxx = set([1287680, 115713, 203267, 188937, 4781, 187420, 243230, 130081, 227882, 228908, 227377, 224308, 5693, 345663, 208964, 224325, 1116230, 243273, 213585, 64091, 45670, 1069680, 1397361, 280699, 1047168, 284811, 284812, 46234, 418459, 214684, 262768, 243365, 273063, 511145, 176299, 272557, 272558, 402612, 283166, 223926, 163003, 559292, 1041607, 1183438, 2769, 122586, 273116, 593117, 192222, 1574623, 243159, 160490, 212717, 272623, 272631, 272632, 63737, 272634, 1341181, 1125630, 99287, 27923, 400667, 269084, 257314, 96563, 300852, 4927, 381764, 242507, 65357, 104782, 336722, 190304, 882, 347515, 353152, 83332, 93061, 194439, 1223560, 267671, 196164, 1245935, 449447, 420778, 195522, 556484, 5061, 391623, 70601, 85962, 272844, 259536, 272633, 220668, 169963, 295405, 237561, 407035, 997884, 1432061, 1010810, 562, 1010800])
labelToTaxId = readTranslationMap()
numProcessed = 0
numMatched = 0
# Annotate tree leaves
for node in tree.traverse():
if node.is_leaf():
#items = node.name.split("_") # "items" (words) are separated by '_'
#for i,x in enumerate(items):
# if x.startswith("Submit") or x.startswith("submit"):
# print("Removing submission note on node: %s" % items)
# items = items[:i]
# break
matchingName = None
matchingTaxId = None
#print("---------------------------")
#print(n)
# Check if the label has a mapping in the id-conversion table
matchingTaxId = labelToTaxId.get(node.name)
#if not matchingTaxId is None:
# matchingName = ncbiTaxa.get_taxid_translator((matchingTaxId,))[matchingTaxId]
# Did we find a match for this leaf?
#if not matchingName is None: # match found
if not matchingTaxId is None:
node.label = node.name
node.name = str(matchingTaxId)
#node.matchingName
#print("<%s>" % node.name)
#lineageItems = [x for x in items[:speciesStartItem] if not x in taxonItemsToIgnore]
lineageItems = ncbiTaxa.get_lineage(matchingTaxId)
# TODO - Fix lineageItems ?
node.add_features(taxId = matchingTaxId, lineageItems = lineageItems)
#node.add_features(taxId = matchingTaxId)
#if matchingTaxId != speciesLevelTaxon:
# node.add_features(speciesLevelTaxon=speciesLevelTaxon)
matched.append("%s [%d]" % (node.label, matchingTaxId))
#print("--"*20)
#print(matchingName)
#print(items[:speciesStartItem])
numMatched += 1
else: # no match found
unmatched.append(node.name)
node.name = "n/a"
numProcessed += 1
if (rl()):
print("(processed %d matched %d)" % (numProcessed, numMatched))
#if(numProcessed>1000):
# break
# Save unmatched names to file (for examination)
with open("unmatched_names.txt", "w") as f:
f.writelines(["%s\n" % x for x in sorted(unmatched)])
# Save unmatched names to file (for examination)
with open("matched_names.txt", "w") as f:
f.writelines(["%s\n" % x for x in sorted(matched)])
print("//"*30)
print("//"*30)
print("//"*30)
print("//"*30)
outer = {}
# Try to annotate non-leaf nodes with common taxonomic group
for node in tree.traverse(strategy='postorder'): # children first
if node.is_leaf():
continue
a = []
for c in node.children:
try:
a.append( c.lineageItems )
except AttributeError:
pass
print(">>" * 20)
print(len(a))
print(a)
if a:
out = None
if len(a)==2:
out = []
#if a[0][-1]=="Anabaena" or a[1][-1]=="Anabaena":
# print(">"*50)
# print(a)
# print(">"*50)
for u,v in zip(a[0], a[1]):
if u==v:
out.append(u)
else:
break
elif len(a)==1:
#if a[0][-1]=="Anabaena":
# print(">"*50)
# print(a)
# print(">"*50)
out = a[0]
else:
assert(False)
if out:
#print("out = %s" % out)
print("//"*20)
print(out[-1])
node.add_features(lineageItems = out, testK = out[-1])
outer[out[-1]] = id(node)
print(">>> %s" % out[-1])
if out[-1]==2:
print("- - "*5)
print(a)
print("- - "*5)
# #if out[-1] == "Nostocaceae":
# #print(node.name)
# #print(a)
# #pass
#else:
# print("*-"*20)
# print(node.name)
# print(a)
for node in tree.traverse(strategy='postorder'): # children first
if node.is_leaf():
continue
try:
l = node.testK
if not l is None:
if outer[l]==id(node):
node.add_features(testL = l)
except AttributeError as e:
pass
print("//"*30)
print("//"*30)
print("//"*30)
print("//"*30)
# Now we have our annotated reference phylogenetic tree
# Get our target list of species to appear on the final tree
#taxa = getSpeciesToInclude()
allNames = ncbiTaxa.get_taxid_translator(taxa)
#taxa.append(1906157)
#taxa.append(251221)
#print(ncbiTaxa.get_rank(taxa))
for x in (45157,4896,44056):
print(x)
print(tree.search_nodes(taxId=x))
f = set()
fnodes = []
notf = set()
for x in taxa:
print("=="*20)
print("Searching for %d" % x)
found = tree.search_nodes(taxId=x)
if found:
f.add(x)
fnodes.append(found[0])
print("Exact match found")
else:
containingSpeciesLevelTaxon = getContainingSpeciesLevelTaxon(x)
if x != containingSpeciesLevelTaxon:
found = tree.search_nodes(taxId=containingSpeciesLevelTaxon)
if not found:
found = tree.search_nodes(speciesLevelTaxon=containingSpeciesLevelTaxon)
# TODO - CONTINUE HERE
#if not found:
# found = tree.search_nodes(
if found:
f.add(x)
fnodes.append(found[0])
print("Found")
else:
print("Not found at all...")
#elif ncbiTaxa.get_rank([x])[x] == 'no rank':
# parent = ncbiTaxa.get_lineage(x)[-2]
# found = tree.search_nodes(taxId=parent)
# if found:
# f.add(x)
# fnodes.append(found[0])
if x not in f:
#print("--"*50)
#print("TaxId not found: %d" % x)
#print("Name: %s" % allNames[x])
containingSpeciesLevelTaxon = getContainingSpeciesLevelTaxon(x)
#print("Species TaxId: %d %s" % (containingSpeciesLevelTaxon, "" if x==containingSpeciesLevelTaxon else "***"))
lineage = ncbiTaxa.get_lineage(x)
#print("Lineage: %s" % lineage)
names = ncbiTaxa.get_taxid_translator(lineage)
#print(names)
for y in reversed(lineage):
name = names[y]
res = bool(tree.search_nodes(testK = name))
#print("%s: %s" % (name, res))
notf.add(x)
print("Found (%d): %s" % (len(f), f))
print(ncbiTaxa.get_rank(list(f)))
print("Couldn't find (%d): %s" % (len(notf), notf))
print(list(ncbiTaxa.get_taxid_translator(list(notf)).values()))
print(len(fnodes))
tree2 = tree.copy()
print("Before pruning: %d" % len(tree2))
if fnodes:
#tree2.prune(fnodes, preserve_branch_length=True)
pruneTree(tree2, fnodes)
print("After pruning: %d" % len(tree2))
return (tree, tree2)
"""
Parse Supplamentary Table 1 from Hug et. al., containing external DB (NCBI or JGI) identifiers for samples used in tree nodes.
"""
def readIdentifiersTable():
from csv import reader
count = 0
identifiers = {}
with open( nmicrobiol201648_s4, 'rU') as csvfile: # This is a mac file with '\r' line endings
for row in reader( csvfile, delimiter='\t' ):
if len(row) < 5:
continue
(stringIdentifier, _, ncbiIdentifier, JGIIdentifier, _) = row
if( not stringIdentifier ):
continue
if not (ncbiIdentifier or JGIIdentifier):
print("Warning: skipping sample missing any identifiers: %s" % stringIdentifier)
continue
count += 1
if (ncbiIdentifier and JGIIdentifier):
print("Warning: sample has multiple identifiers: %s" % stringIdentifier)
if ncbiIdentifier:
identifiers[stringIdentifier] = (ncbiIdentifier, 'N')
elif JGIIdentifier:
identifiers[stringIdentifier] = (JGIIdentifier, 'J')
else:
assert(False)
print("Read %d rows" % count)
return identifiers
treeNodeIdentifiersDf = pd.DataFrame({'NodeLabel': pd.Series([], dtype='str'),
'DBIdentifier': pd.Series([], dtype='str'),
'DBIdentifierType': pd.Categorical([]),
'TaxId': pd.Series([], dtype='int')})
badTaxIdsForBiosamples = frozenset((646099, 9606)) # Ignore biosamples coming from human or human metagenomes (since we are looking for individual species to assign to tree nodes). Why are these even included?
#
# TODO - add filter to ignore samples assigned to other metagenomes (under "unclassified [12908] -> metagenome [408169]" in NCBITaxon hierarchy)
#
alreadyFound = set()
failedBiosampleIdentifiers = []
"""
Return taxid for species-specific NCBI Biosample IDs; Try to reject multiple-species or metagenomic samples.
'sampleIdentifier' may be a string or sequence of strings
"""
def translateNCBIBioSampleIdentifier(sampleIdentifier, identifierClass):
# DEBUG ONLY #### DEBUG ONLY #### DEBUG ONLY #### DEBUG ONLY #### DEBUG ONLY #
#if identifierClass=="SAMN":
# return None
# DEBUG ONLY #### DEBUG ONLY #### DEBUG ONLY #### DEBUG ONLY #### DEBUG ONLY #
#print(sampleIdentifier)
handle = Entrez.efetch(db="biosample", id=sampleIdentifier, retmode="xml") # id can be a string or sequence of strings
tree = ET.parse(handle)
handle.close()
out = []
for sample in tree.findall("./BioSample"):
accession = sample.get("accession")
taxId = None
organism = sample.findall("./Description/Organism")
if len(organism) == 1: # reject taxid if there are multiple annotations
taxId = int(organism[0].get("taxonomy_id"))
if taxId in badTaxIdsForBiosamples:
taxId = None
# Detect multiply-occurring taxId and issue warning
if not taxId is None:
if taxId in alreadyFound:
print("Warning: multiple occurence of taxId %d detected in record %s" % (taxId, sampleIdentifier))
else:
alreadyFound.add(taxId)
if taxId is None:
failedBiosampleIdentifiers.append(sampleIdentifier)
out.append(taxId)
# Check the returned value, according to the number of items requested
if isinstance(sampleIdentifier, basestring):
assert(len(out)==1)
elif isinstance(sampleIdentifier, Iterable):
assert(len(out)==len(sampleIdentifier))
else:
assert(False)
return out
failedSequenceIdentifiers = []
# <eLinkResult>
# <LinkSet>
# <DbFrom>nuccore</DbFrom>
# <IdList>
# <Id>851302729</Id>
# </IdList>
# <LinkSetDb>
# <DbTo>taxonomy</DbTo>
# <LinkName>nuccore_taxonomy</LinkName>
# <Link>
# <Id>1343739</Id>
# </Link>
# </LinkSetDb>
# </LinkSet>
# </eLinkResult>
def translateNCBISequenceIdentifier(seqIdentifier, identifierClass):
#record = Entrez.read(Entrez.elink(dbfrom="pubmed", id=pmid))
handle = Entrez.elink(db="taxonomy", dbfrom="nucleotide", id=seqIdentifier, retmode="xml")
tree = ET.parse(handle)
handle.close()
taxId = None
taxIdNode = tree.findall("./LinkSet/LinkSetDb/Link/Id")
if not taxIdNode is None and len(taxIdNode)==1:
taxId = int(taxIdNode[0].text)
else:
print("XX[seq] %s" % seqIdentifier)
with open(unhandledXMLsFile, "w+") as f:
f.write("\n%s\n" % "--"*20)
f.write("ID[seq]: %s\n" % seqIdentifier)
f.write(ET.tostring(tree.getroot()))
f.write("\n")
f.flush()
failedSequenceIdentifiers.append(seqIdentifier)
return [taxId]
# <RecordSet><DocumentSummary uid="47111">
# <Project>
# <ProjectID>
# <ArchiveID accession="PRJNA47111" archive="NCBI" id="47111" />
# </ProjectID>
# <ProjectDescr>
# <Name>Bigelowiella natans CCMP2755</Name>
# ...
# </ProjectDescr>
# <ProjectType>
# <ProjectTypeSubmission>
# <Target capture="eWhole" material="eGenome" sample_scope="eMonoisolate">
# <Organism species="227086" taxID="753081">
# <OrganismName>Bigelowiella natans CCMP2755</OrganismName>
# <Strain>CCMP2755</Strain>
# <Supergroup>eEukaryotes</Supergroup>
# <GenomeSize units="Kb">94700.000000</GenomeSize>
# </Organism>
failedProjectIdentifiers = []
def translateNCBIBioProjectIdentifier(prjIdentifier, identifierClass):
handle = Entrez.efetch(db="bioproject", id=prjIdentifier, retmode="xml")
tree = ET.parse(handle)
handle.close()
taxId = None
organismNode = tree.findall("./DocumentSummary/Project/ProjectType/ProjectTypeSubmission/Target/Organism")
if not organismNode is None and len(organismNode)==1:
taxId = int(organismNode[0].get("taxID"))
speciesTaxId = int(organismNode[0].get("species")) # ignored...
else:
print("XX[prj] %s" % prjIdentifier)
with open(unhandledXMLsFile, "w+") as f:
f.write("\n%s\n" % "--"*20)
f.write("ID[prj]: %s\n" % prjIdentifier)
f.write(ET.tostring(tree.getroot()))
f.write("\n")
f.flush()
failedProjectIdentifiers.append(prjIdentifier)
return [taxId]
reNCBIIdentifier = re.compile("([A-Z]+)_?\w+")
x = Counter()
def translateNCBIIdentifier(dbIdentifier):
#from random import randint # DEBUG ONLY
# Determine the accession class
# https://www.ncbi.nlm.nih.gov/books/NBK21091/table/ch18.T.refseq_accession_numbers_and_mole/?report=objectonly
#
# https://www.ncbi.nlm.nih.gov/Sequin/acc.html
#
# Accession classes in Hug et. al. supp. table 1:
# Counter({'SAMN': 1475, 'NZ': 634, 'NC': 596, 'PRJNA': 171, 'CP': 21, 'SAMEA': 6, 'FP': 5, 'PRJEA': 5, 'SAMD': 5, 'PRJDA': 3, 'EKD': 3, 'FN': 2, 'AE': 2, 'AP': 2, 'PRJEB': 2, 'AUYT': 1, 'BA': 1, 'AWSN': 1, 'AYLK': 1, 'EKE': 1, 'AYLL': 1, 'APKF': 1, 'CAFG': 1, 'JYIM': 1, 'ASAI': 1, 'ATBP': 1, 'AKKQ': 1, 'ASPK': 1, 'AGCY': 1, 'APGO': 1, 'ASZN': 1, 'AQTX': 1, 'JWKR': 1, 'AYLI': 1, 'ADVD': 1, 'CA': 1, 'L': 1, 'JRFF': 1, 'AGNT': 1, 'JWKP': 1, 'AXWL': 1, 'JWKN': 1, 'JWKQ': 1, 'JWKT': 1, 'JWKU': 1, 'JWKV': 1, 'JWKW': 1, 'LSSB': 1, 'ARQD': 1, 'JWKS': 1, 'JWKX': 1, 'JWKY': 1, 'JWKZ': 1, 'JQJA': 1, 'DP': 1, 'KT': 1, 'AXCJ': 1, 'ARWQ': 1, 'JWKO': 1})
#
match = reNCBIIdentifier.match(dbIdentifier)
ncbiIdentifierClass = None
if match is None:
x.update(("-",))
return None
else:
ncbiIdentifierClass = match.group(1)
x.update((ncbiIdentifierClass,))
if ncbiIdentifierClass.startswith("SAM"):
#if randint(0, 9)>0:
# return None
#else:
return translateNCBIBioSampleIdentifier( dbIdentifier, ncbiIdentifierClass )[0]
elif ncbiIdentifierClass.startswith("PRJ"):
return translateNCBIBioProjectIdentifier( dbIdentifier, ncbiIdentifierClass )[0]
else:
#if randint(0, 9)>0:
# return None
#else:
return translateNCBISequenceIdentifier( dbIdentifier, ncbiIdentifierClass )[0]
allJGIIds = []
def translateJGIIdentifier(dbIdentifier):
# TODO - IMPL THIS (but only 56/~3000 species, and almost all of these are not at the species level)
allJGIIds.append(dbIdentifier)
return None
def storeCorrelatedIdentifier( treeNodeIdentifier, dbIdentifierValue, dbIdentifierSource, taxId):
#fout.write("%s\t%s\t%s\t%s\n" % (treeNodeIdentifier, dbIdentifierValue, dbIdentifierSource, "" if taxId is None else str(taxId)))
global treeNodeIdentifiersDf
treeNodeIdentifiersDf = treeNodeIdentifiersDf.append(
pd.DataFrame({'NodeLabel': pd.Series([treeNodeIdentifier], dtype='str'),
'DBIdentifier': pd.Series([dbIdentifierValue], dtype='str'),
'DBIdentifierType': pd.Categorical([dbIdentifierSource]),
'TaxId': pd.Series([0 if taxId is None else taxId], dtype='int')}) )
topLevelTaxons = Counter()
def isTaxonParticularCellularSpecies(taxId):
assert(not taxId is None)
lineage = ncbiTaxa.get_lineage(taxId)
ranks = ncbiTaxa.get_rank(lineage)
assert(lineage[0]==1) # 'Root' node of taxonomy
# Reject samples that belong to viruses, or to the "Unspecified" subtree of the taxonomy (e.g., metagenomic samples)
if lineage[1] != 131567: # Cellular organism
return False
topLevelTaxons.update([lineage[1]]) # Keep track of encountered top-level taxons
# Reject samples that are not specified at the species level
if "species" in list(ranks.values()):
return True
else:
return False
def prepareTranslationMap():
identifiers = readIdentifiersTable()
numProcessed = 0
numFound = 0
for treeNodeIdentifier, dbIdentifier in list(identifiers.items()):
(dbIdentifierValue, dbIdentifierSource) = dbIdentifier
taxId = None
if dbIdentifierSource=='N': # NCBI
taxId = translateNCBIIdentifier( dbIdentifierValue )
#if not taxId is None:
# print(taxId)
elif dbIdentifierSource=='J': # JGI
taxId = translateJGIIdentifier( dbIdentifierValue )
else:
assert(False)
if (not taxId is None) and (not isTaxonParticularCellularSpecies(taxId)):
taxId = None
numProcessed += 1
if not taxId is None:
numFound += 1
# TODO: store taxId
storeCorrelatedIdentifier( treeNodeIdentifier, dbIdentifierValue, dbIdentifierSource, taxId )
if( rl() ):
print("%d processed, %d found" % (numProcessed, numFound))
# Done
# Save results to file
treeNodeIdentifiersDf.to_csv( nodeIdentifiersMappingTable_csv )
treeNodeIdentifiersDf.to_excel(nodeIdentifiersMappingTable_xls, sheet_name='NodeTaxonMapping')
print("Identifier classes counts:")
print(x)
print("Failed Sequence identifiers:")
print(failedSequenceIdentifiers)
print("Failed Biosample identifiers:")
print(failedBiosampleIdentifiers)
print("Failed Project identifiers:")
print(failedProjectIdentifiers)
print("JGI Ids:")
print(allJGIIds)
print("Top-level taxons:")
print(topLevelTaxons)
def taxonomicTreeDistance(taxId1, taxId2):
if taxId1==taxId2:
return 0
assert(taxId1>0)
assert(taxId2>0)
lineage1 = ncbiTaxa.get_lineage(taxId1)
lineage2 = ncbiTaxa.get_lineage(taxId2)
lastMatch = 0
for u,v in zip(lineage1, lineage2):
if u != v:
break
else:
lastMatch += 1
return max(len(lineage1),len(lineage2)) - lastMatch
def testTranslationMap():
treeNodeIdentifiersDf = pd.read_csv(nodeIdentifiersMappingTable_csv, dtype= {'NodeLabel': 'string', 'DBIdentifier': 'string', 'DBIdentifierType': 'category', 'TaxId': 'int32'} )
#treeNodeIdentifiersDf['InferredTaxId'] = 0
inferredTaxIds = pd.Series([], dtype='int32')
inferredTaxIdDists = pd.Series([], dtype='int32')
i = 0
for row in treeNodeIdentifiersDf.itertuples():
inferredTaxId = inferTaxIdForLabel(row.NodeLabel)[0]
print("%s %s" % (row.TaxId, inferredTaxId))
inferredTaxIds[i] = 0 if inferredTaxId is None else inferredTaxId
dist = None
if row.TaxId==0 or inferredTaxId is None:
dist = 99
else:
dist = taxonomicTreeDistance(row.TaxId, inferredTaxId)
inferredTaxIdDists[i] = dist
i += 1
treeNodeIdentifiersDf['InferredTaxId'] = inferredTaxIds
treeNodeIdentifiersDf['InferredTaxIdDist'] = inferredTaxIdDists
#treeNodeIdentifiersDf.assign( Match = (TaxId==InferredTaxId) )
treeNodeIdentifiersDf.to_csv( nodeIdentifiersMappingTable_with_testing_csv )
return 0
"""
Write table showing all tree nodes, with value indicating for each node whether it has been imported into our DB.
This report is can be used as an "external dataset" in iTOL viewer, *if the appropriate header is prepended*.
"""
def outputNodeExistenceInRnafoldDB():
from data_helpers import getSpeciesName
treeNodeIdentifiersDf = pd.read_csv(nodeIdentifiersMappingTable_csv, dtype= {'NodeLabel': 'string', 'DBIdentifier': 'string', 'DBIdentifierType': 'category', 'TaxId': 'int32'} )
existenceStatuses = pd.Series([], dtype='int32')
values = Counter()
i=0
for row in treeNodeIdentifiersDf.itertuples():
isIncluded = not (getSpeciesName(row.TaxId) is None)
existenceStatuses[i] = 1 if isIncluded else 0
values.update((isIncluded,))
i += 1
treeNodeIdentifiersDf['Included'] = existenceStatuses
del treeNodeIdentifiersDf['DBIdentifier']
del treeNodeIdentifiersDf['DBIdentifierType']
del treeNodeIdentifiersDf['TaxId']
del treeNodeIdentifiersDf['Unnamed: 0']
treeNodeIdentifiersDf.to_csv( nodeIdentifiersMappingTable_with_inclusion_csv, index=False )
print("Output values summary: %s" % values)
return 0
def removeUninformativeTerms(name):
#n2 = name[:]
name = name.replace("_CPR_", "_")
name = name.replace("_CP_", "_")
name = name.replace("_Unclassified_", "_")
name = name.replace("_unclassified_", "_")
name = name.replace("_Unclassfied_", "_")
name = name.replace("_Doykabacteria", "_Dojkabacteria")
#if n2 != name:
# print("%s -> %s" % (n2, name))
return name
# def findBestMatches(n1, n2):
# bestMatches = {}