erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / genomes / ggallus.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 Gallus gallus
31 import string
32 from cistematic.genomes import Genome
33 from cistematic.core.geneinfo import geneinfoDB
34 from os import environ
35
36 if environ.get("CISTEMATIC_ROOT"):
37     cisRoot = environ.get("CISTEMATIC_ROOT") 
38 else:
39     cisRoot = "/proj/genome"
40
41 geneDB = "%s/G_gallus/ggallus.genedb" % cisRoot
42
43
44 def loadChromosome(db, chromID, chromPath, chromOut):
45     seqArray = []
46     ggGenome = Genome("ggallus", dbFile=db)
47     inFile = open(chromPath, "r")
48     line = inFile.readline()
49     for line in inFile:
50         seqArray.append(line.strip())
51
52     seq = string.join(seqArray, "")
53     seqLen = len(seq)
54     if seqLen < 1:
55         print "Problems reading sequence from file"
56
57     print "writing to file %s" % chromOut
58     outFile = open("%s%s" % (cisRoot, chromOut), "w")
59     outFile.write(seq)
60     outFile.close()
61     ggGenome.addChromosomeEntry(chromID, chromOut, "file")
62
63
64 def loadGeneEntries(db, gFile):
65     """ FIXME - NEED TO DEAL WITH ALTERNATIVE SPLICING ENTRIES
66     """
67     geneEntries = []
68     alreadySeen = []
69     ggGenome = Genome("ggallus", dbFile=db)
70     geneFile = open(gFile, "r")
71     geneFile.readline()
72     for line in geneFile:
73         if "|" in line:
74             continue
75
76         cols = line.split("\t")
77         if cols[11].strip() != "GENE":
78             continue
79
80         name = cols[10].split(":")
81         gid = name[1]
82         if gid == "" or gid in alreadySeen:
83             continue
84
85         alreadySeen.append(gid)
86         start = int(cols[2]) - 1
87         stop = int(cols[3]) - 1
88         sense = cols[4]
89         chrom = cols[1].strip()
90         if sense == "+":
91             sense = "F"
92         else:
93             sense = "R"
94
95         geneID = ("ggallus", gid)
96         gidVersion = 1
97         geneEntries.append((geneID, chrom, start, stop, sense, "gene", gidVersion))
98
99     print "Adding %d gene entries" % len(geneEntries)
100     ggGenome.addGeneEntryBatch(geneEntries)
101
102
103 def loadGeneFeatures(db, gFile):
104     """ Load gene features such as CDS, UTR, and PSEUDO from the gene file.
105     """
106     featureEntries = []
107     ggGenome = Genome("ggallus", dbFile=db)
108     featureFile = open(gFile, "r")
109     featureFile.readline()
110     for line in featureFile:
111         if "|" in line:
112             continue
113
114         cols = line.split("\t")
115         if cols[11].strip() not in ["CDS", "UTR", "PSEUDO"]:
116             continue
117
118         fType = cols[11]
119         name = cols[10].split(":")
120         gid = name[1]
121         if gid == "":
122             continue
123
124         start = int(cols[2]) - 1
125         stop = int(cols[3]) - 1
126         sense = cols[4]
127         chrom = cols[1].strip()
128         if sense == "+":
129             sense = "F"
130         else:
131             sense = "R"
132
133         geneID = ("ggallus", gid)
134         gidVersion = 1
135         featureEntries.append((geneID, gidVersion, chrom, start, stop, sense, fType))
136
137     print "Adding %d feature entries" % len(featureEntries)
138     ggGenome.addFeatureEntryBatch(featureEntries)
139
140
141 def loadGeneOntology(db, goPath, goDefPath):
142     ggGenome = Genome("ggallus", dbFile=db)
143     goDefFile = open(goDefPath, "r")
144     goFile = open(goPath, "r")
145     idb = geneinfoDB()
146     goDefs = {}
147     goArray = []
148     for goDefEntry in goDefFile:
149         if goDefEntry[0] != "!":
150             cols = goDefEntry.split("\t")
151             goDefs[cols[0]] = (cols[1], cols[2].strip())
152
153     goEntries = goFile.readlines()
154     prevGID = ""
155     for entry in goEntries:
156         try:
157             fields = entry.split("\t")
158             if fields[0] != "9031":
159                 continue
160
161             locID = fields[1].strip()
162             gID = ("ggallus", locID)
163             if prevGID != gID:
164                 prevGID = gID
165                 gene_name = ""
166                 synonyms = idb.geneIDSynonyms(gID)
167                 if len(synonyms) >0:
168                     gene_name = string.join(synonyms, ",")
169
170             goArray.append((gID, fields[2], "", gene_name, "", string.replace(goDefs[fields[2]][0], "'", "p"), goDefs[fields[2]][1], ""))
171         except:
172             print "locus ID %s could not be added" % locID
173             pass
174
175     print "adding %d go entries" % len(goArray)
176     ggGenome.addGoInfoBatch(goArray)
177
178
179 def createDBFile(db):
180     ggGenome = Genome("ggallus",  dbFile=db)
181     ggGenome.createGeneDB(db)
182
183
184 def createDBindices(db):
185     ggGenome = Genome("ggallus", dbFile=db)
186     ggGenome.createIndices()
187
188
189 def buildChickenDB(db=geneDB):
190     genePath = "%s/download/seq_gene.md" % cisRoot
191     goDefPath = "%s/download/GO.terms_and_ids" % cisRoot # ftp://ftp.geneontology.org/pub/go/doc/GO.terms_and_ids
192     goPath = "%s/download/gene2go" % cisRoot # ftp://ftp.ncbi.nih.gov/gene/DATA/gene2go.gz
193     chromos = {"1": "%s/download/chr1.fa" % cisRoot,
194                "2": "%s/download/chr2.fa" % cisRoot,
195                "3": "%s/download/chr3.fa" % cisRoot,
196                "4": "%s/download/chr4.fa" % cisRoot,
197                "5": "%s/download/chr5.fa" % cisRoot,
198                "6": "%s/download/chr6.fa" % cisRoot,
199                "7": "%s/download/chr7.fa" % cisRoot,
200                "8": "%s/download/chr8.fa" % cisRoot,
201                "9": "%s/download/chr9.fa" % cisRoot,
202                "10": "%s/download/chr10.fa" % cisRoot,
203                "11": "%s/download/chr11.fa" % cisRoot,
204                "12": "%s/download/chr12.fa" % cisRoot,
205                "13": "%s/download/chr13.fa" % cisRoot,
206                "14": "%s/download/chr14.fa" % cisRoot,
207                "15": "%s/download/chr15.fa" % cisRoot,
208                "16": "%s/download/chr16.fa" % cisRoot,
209                "17": "%s/download/chr17.fa" % cisRoot,
210                "18": "%s/download/chr18.fa" % cisRoot,
211                "19": "%s/download/chr19.fa" % cisRoot,
212                "20": "%s/download/chr20.fa" % cisRoot,
213                "21": "%s/download/chr21.fa" % cisRoot,
214                "22": "%s/download/chr22.fa" % cisRoot,
215                "23": "%s/download/chr23.fa" % cisRoot,
216                "24": "%s/download/chr24.fa" % cisRoot,
217                "25": "%s/download/chr25.fa" % cisRoot,
218                "26": "%s/download/chr26.fa" % cisRoot,
219                "27": "%s/download/chr27.fa" % cisRoot,
220                "28": "%s/download/chr28.fa" % cisRoot,
221                "32": "%s/download/chr32.fa" % cisRoot,
222                "W": "%s/download/chrW.fa" % cisRoot,
223                "Z": "%s/download/chrZ.fa" % cisRoot,
224                "M": "%s/download/chrM.fa" % cisRoot,
225                "E22C19W28_E50C23": "%s/download/chrE22C19W28_E50C23.fa" % cisRoot,
226                "E64": "%s/download/chrE64.fa" % cisRoot,
227                "1_random": "%s/download/chr1_random.fa" % cisRoot,
228                "2_random": "%s/download/chr2_random.fa" % cisRoot,
229                "4_random": "%s/download/chr4_random.fa" % cisRoot,
230                "5_random": "%s/download/chr5_random.fa" % cisRoot,
231                "6_random": "%s/download/chr6_random.fa" % cisRoot,
232                "7_random": "%s/download/chr7_random.fa" % cisRoot,
233                "8_random": "%s/download/chr8_random.fa" % cisRoot,
234                "10_random": "%s/download/chr10_random.fa" % cisRoot,
235                "11_random": "%s/download/chr11_random.fa" % cisRoot,
236                "12_random": "%s/download/chr12_random.fa" % cisRoot,
237                "13_random": "%s/download/chr13_random.fa" % cisRoot,
238                "16_random": "%s/download/chr16_random.fa" % cisRoot,
239                "17_random": "%s/download/chr17_random.fa" % cisRoot,
240                "18_random": "%s/download/chr18_random.fa" % cisRoot,
241                "20_random": "%s/download/chr20_random.fa" % cisRoot,
242                "22_random": "%s/download/chr22_random.fa" % cisRoot,
243                "25_random": "%s/download/chr25_random.fa" % cisRoot,
244                "28_random": "%s/download/chr28_random.fa" % cisRoot,
245                "Un_random": "%s/download/chrUn_random.fa" % cisRoot,
246                "W_random": "%s/download/chrW_random.fa" % cisRoot,
247                "E64_random": "%s/download/chrE64_random.fa" % cisRoot,
248                "Z_random": "%s/download/chrZ_random.fa" % cisRoot,
249                "E22C19W28_E50C23_random": "%s/download/chrE22C19W28_E50C23_random.fa" % cisRoot
250     }
251
252     print "Creating database %s" % db
253     createDBFile(db)
254
255     print "Adding gene entries"
256     loadGeneEntries(db, genePath)
257     
258     #print "Adding gene annotations"
259     #loadGeneAnnotations(db, annotPath)
260
261     print "Adding gene features"
262     loadGeneFeatures(db, genePath)
263
264     for chromID in chromos.keys():
265         print "Loading chromosome %s" % chromID
266         loadChromosome(db, chromID, chromos[chromID], "/G_gallus/chromo%s.bin" % chromID)
267
268     print "Adding gene ontology"
269     loadGeneOntology(db, goPath, goDefPath)
270
271     print "Creating Indices"
272     createDBindices(db)
273
274     print "Finished creating database %s" % db