X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=erange.git;a=blobdiff_plain;f=cistematic%2Fgenomes%2Fhsapiens.py;fp=cistematic%2Fgenomes%2Fhsapiens.py;h=e9c677cace13c0e556d0c2234029d3ce42e8c98d;hp=0000000000000000000000000000000000000000;hb=bc30aca13e5ec397c92e67002fbf7a103130b828;hpb=0d3e3112fd04c2e6b44a25cacef1d591658ad181 diff --git a/cistematic/genomes/hsapiens.py b/cistematic/genomes/hsapiens.py new file mode 100644 index 0000000..e9c677c --- /dev/null +++ b/cistematic/genomes/hsapiens.py @@ -0,0 +1,264 @@ +########################################################################### +# # +# C O P Y R I G H T N O T I C E # +# Copyright (c) 2003-10 by: # +# * California Institute of Technology # +# # +# All Rights Reserved. # +# # +# Permission is hereby granted, free of charge, to any person # +# obtaining a copy of this software and associated documentation files # +# (the "Software"), to deal in the Software without restriction, # +# including without limitation the rights to use, copy, modify, merge, # +# publish, distribute, sublicense, and/or sell copies of the Software, # +# and to permit persons to whom the Software is furnished to do so, # +# subject to the following conditions: # +# # +# The above copyright notice and this permission notice shall be # +# included in all copies or substantial portions of the Software. # +# # +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS # +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN # +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # +# SOFTWARE. # +########################################################################### +# +# data for Homo sapiens +import string +from cistematic.genomes import Genome +from cistematic.core.geneinfo import geneinfoDB +from os import environ + +if environ.get("CISTEMATIC_ROOT"): + cisRoot = environ.get("CISTEMATIC_ROOT") +else: + cisRoot = "/proj/genome" + +geneDB = "%s/H_sapiens/hsapiens.genedb" % cisRoot + +def loadChromosome(db, chromID, chromPath, chromOut): + seqArray = [] + hsGenome = Genome("hsapiens", dbFile=db) + inFile = open(chromPath, "r") + line = inFile.readline() + for line in inFile: + seqArray.append(line.strip()) + + seq = string.join(seqArray, "") + seqLen = len(seq) + if seqLen < 1: + print "Problems reading sequence from file" + print "writing to file %s" % chromOut + outFile = open("%s%s" % (cisRoot, chromOut), "w") + outFile.write(seq) + outFile.close() + hsGenome.addChromosomeEntry(chromID, chromOut, "file") + + +def loadGeneEntries(db, gFile, cDict): + """ FIXME - NEED TO DEAL WITH ALTERNATIVE SPLICING ENTRIES + """ + geneEntries = [] + hsGenome = Genome("hsapiens", dbFile=db) + geneFile = open(gFile, "r") + for line in geneFile: + cols = line.split("\t") + if cols[11] != "GENE": + continue + + if cols[12] == "Celera": + continue + + chrom = cols[1].strip() + if chrom not in cDict: + continue + + name = cols[10].split(":") + gid = name[1] + start = int(cols[2]) + stop = int(cols[3]) + sense = cols[4] + if sense == "+": + sense = "F" + else: + sense = "R" + + geneID = ("hsapiens", gid) + gidVersion = 1 + geneEntries.append((geneID, chrom, start, stop, sense, "gene", gidVersion)) + + print "Adding %d gene entries" % len(geneEntries) + hsGenome.addGeneEntryBatch(geneEntries) + + +def loadGeneFeatures(db, gFile, cDict): + """ Load gene features such as CDS, UTR, and PSEUDO from the gene file. + """ + featureEntries = [] + hsGenome = Genome("hsapiens", dbFile=db) + featureFile = open(gFile, "r") + for line in featureFile: + cols = line.split("\t") + if cols[11] not in ["CDS", "UTR", "PSEUDO"]: + continue + if cols[12] == "Celera": + continue + chrom = cols[1].strip() + if chrom not in cDict: + continue + + fType = cols[11] + name = cols[10].split(":") + gid = name[1] + start = int(cols[2]) + stop = int(cols[3]) + sense = cols[4] + if sense == "+": + sense = "F" + else: + sense = "R" + + geneID = ("hsapiens", gid) + gidVersion = 1 + featureEntries.append((geneID, gidVersion, chrom, start, stop, sense, fType)) + + print "Adding %d feature entries" % len(featureEntries) + hsGenome.addFeatureEntryBatch(featureEntries) + + +def loadGeneAnnotations(db): + geneAnnotations = [] + idb = geneinfoDB() + hsGenome = Genome("hsapiens", dbFile=db) + gidList = hsGenome.allGIDs() + for locID in gidList: + gID = ("hsapiens", locID) + geneDescArray = idb.getDescription(gID) + geneDesc = "" + for entry in geneDescArray: + geneDesc += "," + geneDesc += entry.strip() + + if len(geneDescArray) > 0: + geneAnnotations.append((gID, string.replace(geneDesc[1:], "'", "p"))) + + print "Adding %d annotations" % len(geneAnnotations) + hsGenome.addAnnotationBatch(geneAnnotations) + + +def loadGeneOntology(db, goPath, goDefPath): + hsGenome = Genome("hsapiens", dbFile=db) + goDefFile = open(goDefPath, "r") + goFile = open(goPath, "r") + idb = geneinfoDB() + goDefs = {} + goArray = [] + for goDefEntry in goDefFile: + if goDefEntry[0] != "!": + cols = goDefEntry.split("\t") + goDefs[cols[0]] = (cols[1], cols[2].strip()) + + prevGID = "" + index = 0 + for entry in goFile: + try: + fields = entry.split("\t") + if fields[0] != "9606": + continue + + index += 1 + if index % 1000 == 0: + print "adding 1000 go entries" + hsGenome.addGoInfoBatch(goArray) + goArray = [] + + locID = fields[1].strip() + gID = ("hsapiens", locID) + if prevGID != gID: + prevGID = gID + gene_name = "" + synonyms = idb.geneIDSynonyms(gID) + if len(synonyms) >0: + for entry in synonyms: + gene_name += "," + gene_name += entry + else: + gene_name = " " + + goArray.append((gID, fields[2], "", gene_name[1:], "", string.replace(goDefs[fields[2]][0], "'", "p"), goDefs[fields[2]][1], "")) + except: + print "locus ID %s could not be added" % locID + pass + + print "adding %d go entries" % len(goArray) + hsGenome.addGoInfoBatch(goArray) + + +def createDBFile(db): + hsGenome = Genome("hsapiens", dbFile=db) + hsGenome.createGeneDB(db) + + +def createDBindices(db): + hsGenome = Genome("hsapiens", dbFile=db) + hsGenome.createIndices() + + +def buildHsapiensDB(db=geneDB, downloadDir="%s/download" % cisRoot): + genePath = "%s/seq_gene.md" % downloadDir # ftp://ftp.ncbi.nih.gov/genomes/H_sapiens/mapview/seq_gene.md.gz + goDefPath = "%s/GO.terms_and_ids" % downloadDir # ftp://ftp.geneontology.org/go/doc/GO.terms_and_ids + goPath = "%s/gene2go" % downloadDir # ftp://ftp.ncbi.nih.gov/gene/gene2go.gz + # chromosomes are from UCSC - will ignore all the alternative haplotypes, chrUn, and random chromosomes + chromDict = {"1": "%s/chr1.fa" % downloadDir, + "2": "%s/chr2.fa" % downloadDir, + "3": "%s/chr3.fa" % downloadDir, + "4": "%s/chr4.fa" % downloadDir, + "5": "%s/chr5.fa" % downloadDir, + "6": "%s/chr6.fa" % downloadDir, + "7": "%s/chr7.fa" % downloadDir, + "8": "%s/chr8.fa" % downloadDir, + "9": "%s/chr9.fa" % downloadDir, + "10": "%s/chr10.fa" % downloadDir, + "11": "%s/chr11.fa" % downloadDir, + "12": "%s/chr12.fa" % downloadDir, + "13": "%s/chr13.fa" % downloadDir, + "14": "%s/chr14.fa" % downloadDir, + "15": "%s/chr15.fa" % downloadDir, + "16": "%s/chr16.fa" % downloadDir, + "17": "%s/chr17.fa" % downloadDir, + "18": "%s/chr18.fa" % downloadDir, + "19": "%s/chr19.fa" % downloadDir, + "20": "%s/chr20.fa" % downloadDir, + "21": "%s/chr21.fa" % downloadDir, + "22": "%s/chr22.fa" % downloadDir, + "X": "%s/chrX.fa" % downloadDir, + "Y": "%s/chrY.fa" % downloadDir + } + + print "Creating database %s" % db + createDBFile(db) + + print "Adding gene entries" + loadGeneEntries(db, genePath, chromDict) + + print "Adding gene features" + loadGeneFeatures(db, genePath, chromDict) + + print "Adding gene annotations" + loadGeneAnnotations(db) + + print "Adding gene ontology" + loadGeneOntology(db, goPath, goDefPath) + + for chromID in chromDict.keys(): + print "Loading chromosome %s" % chromID + loadChromosome(db, chromID, chromDict[chromID], "/H_sapiens/chromo%s.bin" % chromID) + + print "Creating Indices" + createDBindices(db) + + print "Finished creating database %s" % db