Changeset 718:ec997c138f9e in orange-bioinformatics


Ignore:
Timestamp:
03/23/09 10:44:10 (4 years ago)
Author:
ales_erjavec <ales.erjavec@…>
Branch:
default
Convert:
6f8f5db40ff0d4754bbcf38aa8ed655ffe881d7a
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • obiGene.py

    r717 r718  
    1010import obiTaxonomy 
    1111import orngServerFiles 
    12 from obiTaxonomy import TextDB 
    13 from weakref import WeakValueDictionary 
    1412 
    1513default_database_path = orngServerFiles.localpath("NCBI_geneinfo") 
    1614 
    1715class GeneInfo(object): 
    18     __slots__ = ("tax_id", "gene_id", "symbol", "locus_tag", "synonyms", 
    19                  "dbXrefs", "chromosome", "map_location", "description", "type", 
    20                  "symbol_from_nomenclature_authority", "full_name_from_nomenclature_authority", 
    21                  "nomenclature_status", "other_designations", "modification_date") 
     16    """ An object representing the NCBI information for a gene. 
     17    """ 
     18    __slots__ = ncbi_geneinfo_tags 
    2219    def __init__(self, line): 
     20        """ Construct the GeneInfo object from a line in the NCBI gene_info file 
     21        """ 
    2322        line = line.split("\t") 
    24 ##        line = line[:1] + line[2:] 
    2523        for attr, value in zip(self.__slots__, line): 
    2624            if value == "-": 
     
    4442 
    4543class NCBIGeneInfo(dict): 
    46 ##    _object_cache = WeakValueDictionary() 
    4744    _object_cache = {} 
    4845    def __init__(self, *args, **kwargs): 
    49         """ An object for accessing NCBI gene info 
     46        """ An dictionary like object for accessing NCBI gene info 
     47        Arguments:: 
     48                - *organsim*    Organism id 
     49 
     50        Example:: 
     51            >>> info = NCBIGeneInfo("Homo sapiens") 
    5052        """ 
    5153        if args and type(args[0]) in [str, unicode]: 
     
    5860            elif len(taxids) > 1: 
    5961                raise obiTaxonomy.MultipleSpeciesException, ", ".join(["%s: %s" % (id, obiTaxonomy.name(id)) for id in taxids]) 
    60 ##            self.taxid = args[0] 
     62             
    6163            self.taxid = taxids.pop() 
    6264            if not os.path.exists(orngServerFiles.localpath("NCBI_geneinfo", "gene_info.%s.db" % self.taxid)): 
     
    6466            file = open(orngServerFiles.localpath("NCBI_geneinfo", "gene_info.%s.db" % self.taxid), "rb") 
    6567            self.update(dict((line.split("\t", 3)[1], line) for line in file.read().split("\n") if line.strip() and not line.startswith("#"))) 
    66 ##            if self.taxid  not in self._object_cache: 
    67 ##                if not os.path.exists(orngServerFiles.localpath("NCBI_geneinfo", "gene_info.%s.db" % self.taxid)): 
    68 ##                    orngServerFiles.download("NCBI_geneinfo", "gene_info.%s.db" % self.taxid) 
    69 ##                self._object_cache[self.taxid] = self.load(orngServerFiles.localpath("NCBI_geneinfo", "gene_info.%s.db" % self.taxid)) 
    70 ##                 
    71 ##            self.__dict__ = self._object_cache[self.taxid].__dict__ 
     68 
    7269        else: 
    7370            dict.__init__(self, *args, **kwargs) 
     
    7673    @classmethod     
    7774    def load(cls, file): 
     75        """ A class method that loads gene info from file 
     76        """ 
    7877        if type(file) in [str, unicode]: 
    7978            file = open(file, "rb") 
    80 ##        self._data = dict([(line.split("\t", 3)[1], line) for line in file.read().split("\n") if line]) 
    81 ##        self.update(dict([(line.split("\t", 3)[1], line) for line in file.read().split("\n") if line])) 
    8279        return cls((line.split("\t", 3)[1], line) for line in file.read().split("\n") if line.strip() and not line.startswith("#")) 
    8380         
    84     def get_info(self, id): 
    85 ##        return GeneInfo(self._data[id]) 
    86         return self[id] 
     81    def get_info(self, gene_id): 
     82        """ Search and return the GeneInfo object for gene_id 
     83        """ 
     84        return self[gene_id] 
    8785         
    8886    def __call__(self, name): 
     87        """ Search and return the GeneInfo object for gene_id 
     88        """ 
    8989        translate = lambda a:a 
    9090        id = translate(name) 
     
    125125    @staticmethod 
    126126    def get_geneinfo_from_ncbi(progressCallback=None): 
    127 ##        if type(file) in [unicode, str]: 
    128 ##            file = open(file, "wb") 
    129127        import urllib2, gzip 
    130128        from cStringIO import StringIO 
     
    133131        return info 
    134132         
    135 def prepare_gene_info(file): 
    136     info = NCBIGeneInfo.load(file) 
    137     taxids = ["9606"]#obiTaxonomy.common_organisms() 
    138     genes = dict([(taxid, []) for taxid in taxids]) 
    139     for gi in info.itervalues(): 
    140         if gi.tax_id in taxids: 
    141             genes[gi.tax_id].append(gi) 
    142  
    143     for taxid, genes in genes.items(): 
    144         f = open("gene_info.%s.db" % taxid, "wb") 
    145         f.write("\n".join([str(gi) for gi in sorted(genes, key=lambda gi:int(gi.gene_id))])) 
    146          
    147133 
    148134if __name__ == "__main__": 
    149 ##    info = NCBIGeneInfo("9606") 
    150 ##    gi = info("1") 
    151 ##    print type(gi), type(info.get("1")) 
    152 ##    print type(info.values()[0]) 
    153     prepare_gene_info("D:/Download/gene_info/Homo_sapiens.gene_info") 
    154 ##    print gi.tax_id, gi.synonyms, gi.dbXrefs, gi.symbol_from_nomenclature_authority, gi.full_name_from_nomenclature_authority 
     135    info = NCBIGeneInfo("9606") 
     136    gi = info(list(info)[0]) 
     137    print gi.tax_id, gi.synonyms, gi.dbXrefs, gi.symbol_from_nomenclature_authority, gi.full_name_from_nomenclature_authority 
    155138     
Note: See TracChangeset for help on using the changeset viewer.