erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / genomes / cbrenneri.py
diff --git a/cistematic/genomes/cbrenneri.py b/cistematic/genomes/cbrenneri.py
new file mode 100644 (file)
index 0000000..a227faa
--- /dev/null
@@ -0,0 +1,197 @@
+###########################################################################
+#                                                                         #
+# 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 Caenorhaditis remanei
+import string
+from cistematic.genomes import Genome
+from os import environ
+
+if environ.get("CISTEMATIC_ROOT"):
+    cisRoot = environ.get("CISTEMATIC_ROOT") 
+else:
+    cisRoot = "/proj/genome"
+
+geneDB = "%s/C_brenneri/cbrenneri.genedb" % cisRoot
+
+
+def loadChromosome(db, chromPath, chromOutPath):
+    seqArray = []
+    seqLen = 0
+    cbGenome = Genome("cbrenneri", dbFile=db)
+    inFile = open(chromPath, "r")
+    header = inFile.readline()
+    while header != "":
+        seqArray = []
+        seqLen = 0
+        chromHeader = header.strip()[1:].split()
+        chromID = chromHeader[0]
+        currentLine = inFile.readline()
+
+        while currentLine != "" and currentLine[0] != ">":
+            lineSeq = currentLine.strip()
+            seqLen += len(lineSeq)
+            seqArray.append(lineSeq)
+            currentLine = inFile.readline()
+
+        seq = string.join(seqArray, "")
+        if seqLen < 100000:
+            print "Added contig %s to database" % chromID
+            cbGenome.addSequence(("cbrenneri", chromID), seq, "chromosome", str(seqLen))
+            cbGenome.addChromosomeEntry(chromID, chromID, "db")
+        else:
+            outFileName = "%s%s.bin" % (chromOutPath, chromID)
+            outFile = open( "%s%s" % (cisRoot, outFileName), "w")
+            outFile.write(seq)
+            outFile.close()
+            print "Added contig file %s to database" % outFileName
+            cbGenome.addChromosomeEntry(chromID, outFileName, "file")
+
+        header = currentLine
+
+    inFile.close()
+
+
+def loadGeneEntries(db, gffFile):
+    cbGenome = Genome("cbrenneri", dbFile=db)
+    geneFile = open(gffFile, "r")
+    geneStart = {}
+    geneStop = {}
+    geneChrom = {}
+    geneSense = {}
+    geneEntries = []
+    for line in geneFile:
+        if line[0] == "#":
+            continue
+
+        if line[0] == "\n":
+            continue
+
+        field = line[:-1].split("\t")
+        if field[2] != "stop_codon" and field[2] != "start_codon":
+            continue
+
+        idfield = field[8].split('"')
+        gid = idfield[1]
+        geneID = ("cbrenneri", gid)
+        sense = field[6]
+        geneChrom[geneID] = field[0].strip()
+        if sense == "+":
+            geneSense[geneID] = "F"
+        else:
+            geneSense[geneID] = "R"
+
+        if field[2] == "start_codon":
+            if sense == "+":
+                geneStart[geneID] = int(field[3])
+            else:
+                geneStart[geneID] = int(field[4])
+        else:
+            if sense == "+":
+                geneStop[geneID] = int(field[3])
+            else:
+                geneStop[geneID] = int(field[4])
+
+    for geneID in geneStart:
+        if geneID not in geneStop:
+            print "geneID %s not in geneStop - skipping" % str(geneID)
+            continue
+
+        geneEntries.append((geneID, geneChrom[geneID], geneStart[geneID], geneStop[geneID], geneSense[geneID], "CDS", 1))
+
+    print "Adding %d gene entries" % len(geneEntries)
+    cbGenome.addGeneEntryBatch(geneEntries)
+
+
+def loadFeatureEntries(db, gffFile):
+    cbGenome = Genome("cbrenneri", dbFile=db)
+    featureFile = open(gffFile, "r")
+    featureEntries = []
+    for line in featureFile:
+        if line[0] == "#":
+            continue
+
+        if line[0] == "\n":
+            continue
+
+        field = line.split("\t")
+        if field[2].strip() != "CDS":
+            continue
+
+        gidrev = field[8].split('"')
+        gid = gidrev[1]
+        geneID = ("cbrenneri", gid)
+        gidVersion = 1
+
+        start = int(field[3])
+        stop = int(field[4])
+
+        sense = field[6]
+        chrom = field[0].strip()
+        if sense == "+":
+            sense = "F"
+        else:
+            sense = "R"
+
+        featureEntries.append((geneID, gidVersion, chrom, start, stop, sense, "CDS"))
+
+    print "Adding %d feature entries" % len(featureEntries)
+    cbGenome.addFeatureEntryBatch(featureEntries)
+
+
+def createDBFile(db):
+    cbGenome = Genome("cbrenneri", version="PB2801_001",  dbFile=db)
+    cbGenome.createGeneDB(db)
+
+
+def createDBindices(db):
+    cbGenome = Genome("cbrenneri", version="PB2801_001", dbFile=db)
+    cbGenome.createIndices()
+
+
+def buildCbrenneriDB(db=geneDB):
+    gffPath = "%s/download/PB2801_2007feb09.gff" % cisRoot # using EMS special version
+    chromoPath = "%s/download/PB2801_supercontigs.fa" % cisRoot
+    chromoOutPath = "/C_brenneri/"
+
+    print "Creating database %s" % db
+    createDBFile(db)
+
+    print "Adding gene entries"
+    loadGeneEntries(db, gffPath)
+
+    print "Adding feature entries"
+    loadFeatureEntries(db, gffPath)
+
+    print "Loading genomic sequence" 
+    loadChromosome(db, chromoPath, chromoOutPath)
+
+    print "Creating Indices"
+    createDBindices(db)
+
+    print "Finished creating database %s" % db
\ No newline at end of file