+###########################################################################
+# #
+# 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, os
+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_remanei/cremanei.genedb" % cisRoot
+
+
+def loadChromosomes(db, inPath, chromOutPath):
+ crGenome = Genome("cremanei", dbFile=db)
+ scontigList = os.listdir(inPath)
+ for scontig in scontigList:
+ seq = ''
+ seqArray = []
+ seqLen = 0
+ inFile = open("%s/%s" % (inPath, scontig), "r")
+ index = 0
+ header = inFile.readline()
+ chromID = header.strip()[1:]
+ while header != "":
+ seqArray = []
+ seqLen = 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
+ crGenome.addSequence(("cremanei", chromID), seq, "chromosome", str(seqLen))
+ crGenome.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
+ crGenome.addChromosomeEntry(chromID, outFileName, "file")
+
+ index += 1
+ header = currentLine
+
+ inFile.close()
+
+
+def loadGeneEntries(db, gffFile):
+ crGenome = Genome("cremanei", 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] != "CDS":
+ continue
+
+ idfield = field[8].split('"')
+ gid = idfield[1]
+ geneID = ("cremanei", gid)
+ geneStart[geneID] = int(field[3]) - 1
+ geneStop[geneID] = int(field[4]) - 1
+ sense = field[6]
+ geneChrom[geneID] = field[0].strip()
+ if sense == "+":
+ geneSense[geneID] = "F"
+ else:
+ geneSense[geneID] = "R"
+
+ 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)
+ crGenome.addGeneEntryBatch(geneEntries)
+
+
+def loadFeatureEntries(db, gffFile):
+ crGenome = Genome("cremanei", 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() != "coding_exon":
+ continue
+
+ gidrev = field[8].split('"')
+ gid = gidrev[1]
+ geneID = ("cremanei", gid)
+ gidVersion = 1
+ start = int(field[3]) - 1
+ stop = int(field[4]) - 1
+ 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)
+ crGenome.addFeatureEntryBatch(featureEntries)
+
+
+def createDBFile(db):
+ crGenome = Genome("cremanei", version="CR20050824", dbFile=db)
+ crGenome.createGeneDB(db)
+
+
+def createDBindices(db):
+ crGenome = Genome("cremanei", version="CR20050824", dbFile=db)
+ crGenome.createIndices()
+
+
+def buildCremaneiDB(db=geneDB):
+ gffPath = "%s/download/cr01_wu_merged_gff" % cisRoot # using 20050824 version
+ chromoPath = "%s/download/sctg_masked_seqs/seqs" % cisRoot
+ chromoOutPath = "/C_remanei/"
+
+ 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"
+ loadChromosomes(db, chromoPath, chromoOutPath)
+
+ print "Creating Indices"
+ createDBindices(db)
+
+ print "Finished creating database %s" % db
\ No newline at end of file