This file is indexed.

/usr/lib/python2.7/dist-packages/cogent/parse/ncbi_taxonomy.py is in python-cogent 1.9-9.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/env python
"""Extracts data from NCBI nodes.dmp and names.dmp files.
"""
from cogent.core.tree import TreeNode
from string import strip

__author__ = "Jason Carnes"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Jason Carnes", "Rob Knight"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Jason Carnes"
__email__ = "jason.carnes@sbri.org"
__status__ = "Development"

class MissingParentError(Exception):
    pass

#Note: numbers not guaranteed to be consistent if new taxa are invented...
RanksToNumbers = {
    'forma':1,
    'varietas':2,
    'subspecies':3,
    'species':4,
    'species subgroup':5,
    'species group':6,
    'subgenus':7,
    'genus':8,
    'subtribe':9,
    'tribe':10,
    'subfamily':11,
    'family':12,
    'superfamily':13,
    'parvorder':14,
    'infraorder':15,
    'suborder':16,
    'order':17,
    'superorder':18,
    'infraclass':19,
    'subclass':20,
    'class':21,
    'superclass':22,
    'subphylum':23,
    'phylum':24,
    'superphylum':25,
    'kingdom':26,
    'superkingdom':27,
    'no rank':28,
}

class NcbiTaxon(object):
    """Extracts taxon information: init from one line of NCBI's nodes.dmp.

    Properties:
        TaxonId     ID of this node
        ParentId    ID of this node's parent
        Rank        Rank of this node: genus, species, etc.
        EmblCode    Locus name prefix; not unique
        DivisionId  From division.dmp           
        DivisionInherited  1 or 0; 1 if node inherits division from parent
        TranslTable  ID of this node's genetic code from gencode.dmp
        GCInherit   1 or 0; 1 if node inherits genetic code from parent
        TranslTableMt    ID of this node's mitochondrial code from gencode.dmp
        TranslTableMtInherited 1 or 0; 1 if node inherits mt code from parent
        Hidden      1 or 0; 1 if hidden by default in GenBank's listing
        HiddenSubtreeRoot   1 or 0; 1 if no sequences from this subtree exist
        Comments    free-text comments
        
        RankId      Arbitrary number corresponding to rank. See RanksToNumbers.
        Name        Name of this node: must get from external source. Thanks
                    so much, NCBI...
                    Expect a string: '' by default.
    """
    Fields = ['TaxonId', 'ParentId', 'Rank', 'EmblCode', 
              'DivisionId', 'DivisionInherited', 'TranslTable', 
              'TranslTableInherited',
              'TranslTableMt', 'TranslTableMtInherited', 'Hidden', 
              'HiddenSubtreeRoot', 'Comments'] 
    def __init__(self, line):
        """Returns new NcbiTaxon from line containing taxonomy data."""
        line_pieces = map(strip, line.split('|'))
        for i in [0, 1, 5, 6, 7, 8, 9, 10, 11]:
            line_pieces[i] = int(line_pieces[i])
        #fix trailing delimiter
        last = line_pieces[-1]
        if last.endswith('|'):
            line_pieces[-1] = last[:-1]
        self.__dict__ = dict(zip(self.Fields, line_pieces))
        self.Name = '' #will get name field from names.dmp; fillNames
        self.RankId = RanksToNumbers.get(self.Rank, None)
        
    def __str__(self):
        """Writes data out in format we got it."""
        pieces = [str(getattr(self,f)) for f in self.Fields]
        #remember to set the parent of the root to itself
        if pieces[1] == 'None':
            pieces[1] = pieces[0]
        return '\t|\t'.join(pieces) + '\t|\n'
    
    def __cmp__(self, other):
        """Compare by taxon rank."""
        try:
            return cmp(self.RankId, other.RankId)
        except AttributeError:
            return 1    #always sort ranked nodes above unranked
    

def NcbiTaxonParser(infile):
    """Returns a sequence of NcbiTaxon objects from sequence of lines."""
    for line in infile:
        if line.strip():
            yield NcbiTaxon(line)

def NcbiTaxonLookup(taxa):
    """Returns dict of TaxonId -> NcbiTaxon object."""
    result = {}
    for t in taxa:
        result[t.TaxonId] = t
    return result

class NcbiName(object):
    """Extracts name information: init from one line of NCBI's names.dmp.
    
    Properties:
        TaxonId     TaxonId of this node
        Name        Text representation of the name, e.g. Homo sapiens
        UniqueName  The unique variant of this name if Name not unique 
        NameClass   Kind of name, e.g. scientific name, synonym, etc.
    """
    Fields = ['TaxonId', 'Name', 'UniqueName', 'NameClass']
    def __init__(self, line):
        """Returns new NcbiName from line containing name data."""
        line_pieces = map(strip, line.split('|'))
        line_pieces[0] = int(line_pieces[0])    #convert taxon_id
        self.__dict__ = dict(zip(self.Fields, line_pieces))
        
    def __str__(self):
        """Writes data out in similar format as the one we got it from."""
        return '\t|\t'.join([str(getattr(self, f)) for f in self.Fields]) \
            + '|\n'

def NcbiNameParser(infile):
    """Returns sequence of NcbiName objects from sequence of lines."""
    for line in infile:
        if line.strip():
            yield NcbiName(line)

def NcbiNameLookup(names):
    """Returns dict mapping taxon id -> NCBI scientific name."""
    result = {}
    for name in names:
        if name.NameClass == 'scientific name':
            result[name.TaxonId] = name
    return result

class NcbiTaxonomy(object):
    """Holds root node of a taxonomy tree, plus lookup by id or name."""
    def __init__(self, taxa, names, strict=False):
        """Creates new taxonomy, using data in Taxa and Names.

        taxa should be the product of NcbiTaxonLookup.
        
        names should be the product of NcbiNameLookup.
        
        strict, if True, raises an error on finding taxa whose parents don't
        exist. Otherwise, will put them in self.Deadbeats keyed by parent ID.
        
        Note: because taxa is a dict, nodes will be added in arbitrary order.
        """
        names_to_nodes = {}
        ids_to_nodes = {}
        for t_id, t in taxa.iteritems():
            name_rec = names.get(t_id, None)
            if name_rec:
                name = name_rec.Name
            else:
                name = 'Unknown'
            t.Name = name
            
            node = NcbiTaxonNode(t)
            names_to_nodes[name] = node
            ids_to_nodes[t_id] = node
        self.ByName = names_to_nodes
        self.ById = ids_to_nodes

        deadbeats = {}
        #build the tree by connecting each node to its parent
        for t_id, t in ids_to_nodes.iteritems():
            if t.ParentId == t.TaxonId:
                t.Parent = None
            else:
                try:
                    ids_to_nodes[t.ParentId].append(t)
                except KeyError:    #found a child whose parent doesn't exist
                    if strict:
                        raise MissingParentError, \
                            "Node %s has parent %s, which isn't in taxa." % \
                            (t_id, t.ParentId)
                    else:
                        deadbeats[t.ParentId] = t
        self.Deadbeats = deadbeats
        self.Root = t.root()
            
    def __getitem__(self, item):
        """If item is int, returns taxon by id: otherwise, searches by name.
        
        Returns the relevant NcbiTaxonNode.
        Will raise KeyError if not present.
        """
        try:
            return self.ById[int(item)]
        except ValueError:
            return self.ByName[item]

class NcbiTaxonNode(TreeNode):
    """Provides some additional methods specific to Ncbi taxa."""

    def __init__(self, Data):
        """Returns a new NcbiTaxonNode object; requires NcbiTaxon to initialize."""
        self.Data = Data
        self._parent = None
        self.Children = []
            
    def getRankedDescendants(self, rank):
        """Returns all descendants of self with specified rank as flat list."""
        curr = self.Rank
        if curr == rank:
            result = [self]
        else:
            result = []
        for i in self:
            result.extend(i.getRankedDescendants(rank))
        return result

    def _get_parent_id(self): 
        return self.Data.ParentId
    ParentId = property(_get_parent_id)

    def _get_taxon_id(self): 
        return self.Data.TaxonId
    TaxonId = property(_get_taxon_id)

    def _get_rank(self): 
        return self.Data.Rank
    Rank = property(_get_rank)

    def _get_name(self):
        return self.Data.Name
    Name = property(_get_name)
   
def NcbiTaxonomyFromFiles(nodes_file, names_file, strict=False):
    """Returns new NcbiTaxonomy fron nodes and names files."""
    taxa = NcbiTaxonLookup(NcbiTaxonParser(nodes_file))
    names = NcbiNameLookup(NcbiNameParser(names_file))
    return NcbiTaxonomy(taxa, names, strict)