erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / genomes / spurpuratus.py
1 ###########################################################################
2 #                                                                         #
3 # C O P Y R I G H T   N O T I C E                                         #
4 #  Copyright (c) 2003-10 by:                                              #
5 #    * California Institute of Technology                                 #
6 #                                                                         #
7 #    All Rights Reserved.                                                 #
8 #                                                                         #
9 # Permission is hereby granted, free of charge, to any person             #
10 # obtaining a copy of this software and associated documentation files    #
11 # (the "Software"), to deal in the Software without restriction,          #
12 # including without limitation the rights to use, copy, modify, merge,    #
13 # publish, distribute, sublicense, and/or sell copies of the Software,    #
14 # and to permit persons to whom the Software is furnished to do so,       #
15 # subject to the following conditions:                                    #
16 #                                                                         #
17 # The above copyright notice and this permission notice shall be          #
18 # included in all copies or substantial portions of the Software.         #
19 #                                                                         #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,         #
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF      #
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND                   #
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS     #
24 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN      #
25 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN       #
26 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE        #
27 # SOFTWARE.                                                               #
28 ###########################################################################
29 #
30 # data for Strongylocentrotus purpuratus
31 import string
32 from cistematic.genomes import Genome
33 from os import environ
34
35 if environ.get("CISTEMATIC_ROOT"):
36     cisRoot = environ.get("CISTEMATIC_ROOT") 
37 else:
38     cisRoot = "/proj/genome"
39
40 geneDB = "%s/S_purpuratus/spurpuratus.genedb" % cisRoot
41
42
43 def loadChromosome(db, chromPath, chromOutPath):
44     seqArray = []
45     seqLen = 0
46     spGenome = Genome("spurpuratus", dbFile=db)
47     inFile = open(chromPath, "r")
48     header = inFile.readline()
49     while header != "":
50         seqArray = []
51         seqLen = 0
52         fields = header.split()
53         if "purpuratus" in header:
54             idpart = fields[3]
55         else:
56             idpart = fields[-1].strip()
57
58         parts = idpart.split("_")
59         chromID = parts[-1]
60         currentLine = inFile.readline()
61         while currentLine != "" and currentLine[0] != ">":
62             lineSeq = currentLine.strip()
63             seqLen += len(lineSeq)
64             seqArray.append(lineSeq)
65             currentLine = inFile.readline()
66
67         seq = string.join(seqArray, "")
68         if seqLen < 50000:
69             print "Added contig %s to database" % chromID
70             spGenome.addSequence(("spurpuratus", chromID), seq, "chromosome", str(seqLen))
71             spGenome.addChromosomeEntry(chromID, chromID, "db")
72         else:
73             outFileName = "%s%s.bin" % (chromOutPath, chromID)
74             outFile = open("%s%s" % (cisRoot, outFileName), "w")
75             outFile.write(seq)
76             outFile.close()
77             print "Added contig file %s to database" % outFileName
78             spGenome.addChromosomeEntry(chromID, outFileName, "file")
79
80         header = currentLine
81
82     inFile.close()
83
84
85 def createDBFile(db):
86     spGenome = Genome("spurpuratus", version="2.1",  dbFile=db)
87     spGenome.createGeneDB(db)
88
89
90 def createDBindices(db):
91     spGenome = Genome("spurpuratus", version="2.1", dbFile=db)
92     spGenome.createIndices()
93
94
95 def buildSpurpuratusDB(db=geneDB):
96     chromoPath = "%s/download/Spur2.1_Nmasked.txt" % cisRoot
97     chromoOutPath = "/S_purpuratus/"
98
99     print "Creating database %s" % db
100     createDBFile(db)
101
102     print "Loading genomic sequence" 
103     loadChromosome(db, chromoPath, chromoOutPath)
104
105     print "Creating Indices"
106     createDBindices(db)
107
108     print "Finished creating database %s" % db