X-Git-Url: http://woldlab.caltech.edu/gitweb/?p=erange.git;a=blobdiff_plain;f=cistematic%2Fgenomes%2Frnorvegicus.py;fp=cistematic%2Fgenomes%2Frnorvegicus.py;h=b0fae70e721bc11e5046d9a15b4e34120144c16e;hp=0000000000000000000000000000000000000000;hb=bc30aca13e5ec397c92e67002fbf7a103130b828;hpb=0d3e3112fd04c2e6b44a25cacef1d591658ad181 diff --git a/cistematic/genomes/rnorvegicus.py b/cistematic/genomes/rnorvegicus.py new file mode 100644 index 0000000..b0fae70 --- /dev/null +++ b/cistematic/genomes/rnorvegicus.py @@ -0,0 +1,255 @@ +########################################################################### +# # +# 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 Ratus Norvegicus +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/R_norvegicus/rnorvegicus.genedb" % cisRoot + + +def loadChromosome(db, chromID, chromPath, chromOut): + seqArray = [] + rnGenome = Genome("rnorvegicus", 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() + rnGenome.addChromosomeEntry(chromID, chromOut, "file") + + +def loadGeneEntries(db, gFile): + """ FIXME - NEED TO DEAL WITH ALTERNATIVE SPLICING ENTRIES + """ + geneEntries = [] + rnGenome = Genome("rnorvegicus", dbFile=db) + geneFile = open(gFile, "r") + for line in geneFile: + cols = line.split("\t") + if cols[11] != "GENE": + continue + + if cols[12] == "Celera": + continue + + name = cols[10].split(":") + gid = name[1] + start = int(cols[2]) - 1 + stop = int(cols[3]) - 1 + sense = cols[4] + chrom = cols[1].strip() + if sense == "+": + sense = "F" + else: + sense = "R" + + geneID = ("rnorvegicus", gid) + gidVersion = 1 + geneEntries.append((geneID, chrom, start, stop, sense, "gene", gidVersion)) + + print "Adding %d gene entries" % len(geneEntries) + rnGenome.addGeneEntryBatch(geneEntries) + + +def loadGeneFeatures(db, gFile): + """ Load gene features such as CDS, UTR, and PSEUDO from the gene file. + """ + featureEntries = [] + rnGenome = Genome("rnorvegicus", 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 + + fType = cols[11] + name = cols[10].split(":") + gid = name[1] + start = int(cols[2]) - 1 + stop = int(cols[3]) - 1 + sense = cols[4] + chrom = cols[1].strip() + if sense == "+": + sense = "F" + else: + sense = "R" + + geneID = ("rnorvegicus", gid) + gidVersion = 1 + featureEntries.append((geneID, gidVersion, chrom, start, stop, sense, fType)) + + print "Adding %d feature entries" % len(featureEntries) + rnGenome.addFeatureEntryBatch(featureEntries) + + +def loadGeneAnnotations(db): + geneAnnotations = [] + idb = geneinfoDB() + rnGenome = Genome("rnorvegicus", dbFile=db) + gidList = rnGenome.allGIDs() + for locID in gidList: + gID = ("rnorvegicus", 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) + rnGenome.addAnnotationBatch(geneAnnotations) + + +def loadGeneOntology(db, goPath, goDefPath): + rnGenome = Genome("rnorvegicus", 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()) + + goEntries = goFile.readlines() + prevGID = '' + for entry in goEntries: + try: + fields = entry.split("\t") + if fields[0] != "10090": + continue + + locID = fields[1].strip() + gID = ("rnorvegicus", 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) + rnGenome.addGoInfoBatch(goArray) + + +def createDBFile(db): + rnGenome = Genome("rnorvegicus", dbFile=db) + rnGenome.createGeneDB(db) + + +def createDBindices(db): + rnGenome = Genome("rnorvegicus", dbFile=db) + rnGenome.createIndices() + + +def buildRatDB(db=geneDB, downloadDir="%s/download" % cisRoot): + genePath = "%s/seq_gene.md" % downloadDir + goDefPath = "%s/GO.terms_and_ids" % downloadDir + goPath = "%s/gene2go" % downloadDir + # ignoring all random chromosomes + chromos = {"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, + "Un": "%s/chrUn.fa" % downloadDir, + "X": "%s/chrX.fa" % downloadDir, + "20": "%s/chr20.fa" % downloadDir, + "M": "%s/chrM.fa" % downloadDir + } + + print "Creating database %s" % db + createDBFile(db) + + print "Adding gene entries" + loadGeneEntries(db, genePath) + + print "Adding gene features" + loadGeneFeatures(db, genePath) + + print "Adding gene annotations" + loadGeneAnnotations(db) + + print "Adding gene ontology" + loadGeneOntology(db, goPath, goDefPath) + + for chromID in chromos.keys(): + print "Loading chromosome %s" % chromID + loadChromosome(db, chromID, chromos[chromID], "/R_norvegicus/chromo%s.bin" % chromID) + + print "Creating Indices" + createDBindices(db) + + print "Finished creating database %s" % db \ No newline at end of file