erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / genomes / ecaballus.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 Equus Caballus
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/E_caballus/ecaballus.genedb" % cisRoot
41
42
43 def loadChromosome(db, chromID, chromPath, chromOut):
44     seqArray = []
45     ecGenome = Genome("ecaballus", dbFile=db)
46     inFile = open(chromPath, "r")
47     line = inFile.readline()
48     for line in inFile:
49         seqArray.append(line.strip())
50
51     seq = string.join(seqArray, "")
52     seqLen = len(seq)
53     if seqLen < 1:
54         print "Problems reading sequence from file"
55
56     print "writing to file %s" % chromOut
57     outFile = open("%s%s" % (cisRoot, chromOut), "w")
58     outFile.write(seq)
59     outFile.close()
60     ecGenome.addChromosomeEntry(chromID, chromOut, "file")
61
62
63 def loadGeneEntries(db, gFile):
64     """ FIXME - NEED TO DEAL WITH ALTERNATIVE SPLICING ENTRIES
65     """
66     geneEntries = []
67     alreadySeen = []
68     ecGenome = Genome("ecaballus", dbFile=db)
69     geneFile = open(gFile, "r")
70     geneFile.readline()
71     for line in geneFile:
72         cols = line.split("\t")
73         if cols[11].strip() != "GENE":
74             continue
75
76         name = cols[10].split(":")
77         gid = name[1]
78         if gid == "" or gid in alreadySeen:
79             continue
80
81         alreadySeen.append(gid)
82         start = int(cols[2]) - 1
83         stop = int(cols[3]) - 1
84         sense = cols[4]
85         chrom = cols[1].strip()
86         if sense == "+":
87             sense = "F"
88         else:
89             sense = "R"
90
91         geneID = ("ecaballus", gid)
92         gidVersion = 1
93         geneEntries.append((geneID, chrom, start, stop, sense, "gene", gidVersion))
94
95     print "Adding %d gene entries" % len(geneEntries)
96     ecGenome.addGeneEntryBatch(geneEntries)
97
98
99 def loadGeneFeatures(db, gFile):
100     """ Load gene features such as CDS, UTR, and PSEUDO from the gene file.
101     """
102     featureEntries = []
103     ecGenome = Genome("ecaballus", dbFile=db)
104     featureFile = open(gFile, "r")
105     featureFile.readline()
106     for line in featureFile:
107         cols = line.split("\t")
108         if cols[11].strip() not in ["CDS", "UTR", "PSEUDO"]:
109             continue
110
111         fType = cols[11]
112         name = cols[10].split(":")
113         gid = name[1]
114         if gid == "":
115             continue
116
117         start = int(cols[2]) - 1
118         stop = int(cols[3]) - 1
119         sense = cols[4]
120         chrom = cols[1].strip()
121         if sense == "+":
122             sense = "F"
123         else:
124             sense = "R"
125
126         geneID = ("ecaballus", gid)
127         gidVersion = 1
128         featureEntries.append((geneID, gidVersion, chrom, start, stop, sense, fType))
129
130     print "Adding %d feature entries" % len(featureEntries)
131     ecGenome.addFeatureEntryBatch(featureEntries)
132
133
134 def createDBFile(db):
135     ecGenome = Genome("ecaballus",  dbFile=db)
136     ecGenome.createGeneDB(db)
137
138
139 def createDBindices(db):
140     ecGenome = Genome("ecaballus", dbFile=db)
141     ecGenome.createIndices()
142
143
144 def buildHorseDB(db=geneDB):
145     genePath = "%s/download/seq_gene.md" % cisRoot
146     chromos = {"1": "%s/download/chr1.fa" % cisRoot,
147                "2": "%s/download/chr2.fa" % cisRoot,
148                "3": "%s/download/chr3.fa" % cisRoot,
149                "4": "%s/download/chr4.fa" % cisRoot,
150                "5": "%s/download/chr5.fa" % cisRoot,
151                "6": "%s/download/chr6.fa" % cisRoot,
152                "7": "%s/download/chr7.fa" % cisRoot,
153                "8": "%s/download/chr8.fa" % cisRoot,
154                "9": "%s/download/chr9.fa" % cisRoot,
155                "10": "%s/download/chr10.fa" % cisRoot,
156                "11": "%s/download/chr11.fa" % cisRoot,
157                "12": "%s/download/chr12.fa" % cisRoot,
158                "13": "%s/download/chr13.fa" % cisRoot,
159                "14": "%s/download/chr14.fa" % cisRoot,
160                "15": "%s/download/chr15.fa" % cisRoot,
161                "16": "%s/download/chr16.fa" % cisRoot,
162                "17": "%s/download/chr17.fa" % cisRoot,
163                "18": "%s/download/chr18.fa" % cisRoot,
164                "19": "%s/download/chr19.fa" % cisRoot,
165                "20": "%s/download/chr20.fa" % cisRoot,
166                "21": "%s/download/chr21.fa" % cisRoot,
167                "22": "%s/download/chr22.fa" % cisRoot,
168                "23": "%s/download/chr23.fa" % cisRoot,
169                "24": "%s/download/chr24.fa" % cisRoot,
170                "25": "%s/download/chr25.fa" % cisRoot,
171                "26": "%s/download/chr26.fa" % cisRoot,
172                "27": "%s/download/chr27.fa" % cisRoot,
173                "28": "%s/download/chr28.fa" % cisRoot,
174                "29": "%s/download/chr29.fa" % cisRoot,
175                "30": "%s/download/chr30.fa" % cisRoot,
176                "31": "%s/download/chr31.fa" % cisRoot,
177                "M": "%s/download/chrM.fa" % cisRoot,
178                "X": "%s/download/chrX.fa" % cisRoot,
179                "Un": "%s/download/chrUn.fa" % cisRoot
180     }
181
182     print "Creating database %s" % db
183     createDBFile(db)
184
185     print "Adding gene entries"
186     loadGeneEntries(db, genePath)
187
188     print "Adding gene features"
189     loadGeneFeatures(db, genePath)
190
191     for chromID in chromos.keys():
192         print "Loading chromosome %s" % chromID
193         loadChromosome(db, chromID, chromos[chromID], "/E_caballus/chromo%s.bin" % chromID)
194
195     print "Creating Indices"
196     createDBindices(db)
197
198     print "Finished creating database %s" % db