/usr/lib/python2.7/dist-packages/cogent/core/info.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 | #!/usr/bin/env python
"""Provides Info, DbRef, DbRefs
Info is a dictionary and is the annotation object of a Sequence object.
"""
from cogent.parse.record import MappedRecord
from cogent.util.misc import Delegator, FunctionWrapper, ConstrainedDict
__author__ = "Rob Knight"
__copyright__ = "Copyright 2007-2016, The Cogent Project"
__credits__ = ["Rob Knight"]
__license__ = "GPL"
__version__ = "1.9"
__maintainer__ = "Rob Knight"
__email__ = "rob@spot.colorado.edu"
__status__ = "Prototype"
class DbRef(object):
"""Holds a database accession, and optionally other data.
Accession: id in the database: str or int
Db: database name: str
Name: short name of the record: str
Description: description of the record, possibly lengthy: str
Data: any data associated with the record: arbitrary object
str(DbRef) always returns the accession.
"""
def __init__(self, Accession, Db='', Name='', Description='', \
Data=None):
"""Returns new DbRef.
str(DbRef) always returns the accession as a string.
"""
self.Accession = Accession
self.Db = Db
self.Name = Name
self.Description = Description
self.Data = Data
def __str__(self):
"""Returns accession."""
return str(self.Accession)
def __int__(self):
"""Tries to coerce accession to int."""
return int(self.Accession)
def __cmp__(self, other):
"""Compares by accession: tries numeric first, then alphabetic"""
try:
return cmp(int(self), int(other))
except:
return cmp(str(self), str(other))
def _make_list(obj):
"""Returns list corresponding to or containing obj, depending on type."""
if isinstance(obj, list):
return obj
elif isinstance(obj, tuple):
return list(obj)
else:
return [obj]
class DbRefs(MappedRecord, ConstrainedDict):
"""Holds Database -> [Accessions] mapping.
The accessions for a particular database are always stored as a list.
DbRefs will ultimately contain methods for actually getting the records
from known databases.
"""
ValueMask = FunctionWrapper(_make_list)
DefaultValue = []
KnownDatabases = dict.fromkeys(['RefSeq', 'GenBank', 'GenNucl', 'GenPept',
'GI', 'SwissProt', 'PIR', 'EMBL', 'DDBJ',
'NDB', 'PDB', 'Taxon', 'LocusLink', 'UniGene', 'OMIM', 'PubMed', 'COGS',
'CDD', 'Pfam', 'Rfam', 'GO', 'dbEST', 'IPI', 'rRNA', 'EC', 'HomoloGene',
'KEGG', 'BRENDA', 'EcoCyc', 'HumanCyc', 'BLOCKS'])
class Info(MappedRecord, Delegator):
"""Dictionary that stores attributes for Sequence objects.
Delegates to DbRefs for database IDs.
"""
Required = {'Refs':None}
def __init__(self, *args, **kwargs):
"""Returns new Info object. Creates DbRefs if necessary."""
temp = dict(*args, **kwargs)
if 'Refs' in temp:
refs = temp['Refs']
if not isinstance(refs, DbRefs):
refs = DbRefs(refs)
else:
refs = DbRefs()
#move keys into refs if they belong there: allows init from flat dict
for key, val in temp.items():
if key in KnownDatabases:
refs[key] = val
del temp[key]
Delegator.__init__(self, refs)
self['Refs'] = refs
MappedRecord.__init__(self, temp)
def __getattr__(self, attr):
"""Checks for attr in Refs first."""
if attr in KnownDatabases:
return getattr(self.Refs, attr)
else:
return super(Info, self).__getattr__(attr)
def __setattr__(self, attr, val):
"""Try to set in Refs first."""
if attr in KnownDatabases:
return setattr(self.Refs, attr, val)
else:
return super(Info, self).__setattr__(attr, val)
def __getitem__(self, item):
"""Checks for item in Refs first."""
if item in KnownDatabases:
return getattr(self.Refs, item)
else:
return super(Info, self).__getitem__(item)
def __setitem__(self, item, val):
"""Try to set in Refs first."""
if item in KnownDatabases:
return setattr(self.Refs, item, val)
else:
return super(Info, self).__setitem__(item, val)
def __contains__(self, item):
"""Checks for item in Refs first."""
if item in KnownDatabases:
return item in self.Refs
else:
return super(Info, self).__contains__(item)
|