erange 4.0a dev release with integrated cistematic
[erange.git] / cistematic / genomes / cfamiliaris.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 Canis familiaris
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/C_familiaris/cfamiliaris.genedb" % cisRoot
41
42
43 def loadChromosome(db, chromID, chromPath, chromOut):
44     seqArray = []
45     cfGenome = Genome("cfamiliaris", 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     cfGenome.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     cfGenome = Genome("cfamiliaris", 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 = ("cfamiliaris", gid)
92         gidVersion = 1
93         geneEntries.append((geneID, chrom, start, stop, sense, "gene", gidVersion))
94
95     print "Adding %d gene entries" % len(geneEntries)
96     cfGenome.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     cfGenome = Genome("cfamiliaris", 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 = ("cfamiliaris", gid)
127         gidVersion = 1
128         featureEntries.append((geneID, gidVersion, chrom, start, stop, sense, fType))
129
130     print "Adding %d feature entries" % len(featureEntries)
131     cfGenome.addFeatureEntryBatch(featureEntries)
132
133
134 def loadGeneAnnotations(db, annotPath):
135     geneAnnotations = []
136     annotFile = open(annotPath, "r")
137     cfGenome = Genome("cfamiliaris", dbFile=db)
138     for line in annotFile:
139         try:
140             cols = line.split("\t")
141             locID = cols[0]
142             geneDesc = cols[6]
143             if len(locID) > 0:
144                 geneAnnotations.append((("cfamiliaris", locID), string.replace(geneDesc.strip(), "'", "p")))
145         except:
146             pass
147
148     print "Adding %d annotations" % len(geneAnnotations)
149     cfGenome.addAnnotationBatch(geneAnnotations)
150
151
152 def loadGeneOntology(db, goPath, goDefPath):
153     cfGenome = Genome("cfamiliaris", dbFile=db)
154     goDefFile = open(goDefPath, "r")
155     goFile = open(goPath, "r")
156     idb = geneinfoDB()
157     goDefs = {}
158     goArray = []
159     for goDefEntry in goDefFile:
160         if goDefEntry[0] != "!":
161             cols = goDefEntry.split("\t")
162             goDefs[cols[0]] = (cols[1], cols[2].strip())
163
164     goEntries = goFile.readlines()
165     prevGID = ''
166     for entry in goEntries:
167         try:
168             fields = entry.split("\t")
169             if fields[0] != "9615":
170                 continue
171
172             locID = fields[1].strip()
173             gID = ("cfamiliaris", locID)
174             if prevGID != gID:
175                 prevGID = gID
176                 gene_name = ""
177                 synonyms = idb.geneIDSynonyms(gID)
178                 if len(synonyms) >0:
179                     for entry in synonyms:
180                         gene_name += ","   
181                         gene_name += entry
182                 else:
183                     gene_name = " "
184
185             goArray.append((gID, fields[2], "", gene_name[1:], "", string.replace(goDefs[fields[2]][0], "'", "p"), goDefs[fields[2]][1], ""))
186         except:
187             print "locus ID %s could not be added" % locID
188             pass
189
190     print "adding %d go entries" % len(goArray)
191     cfGenome.addGoInfoBatch(goArray)
192
193
194 def createDBFile(db):
195     cfGenome = Genome("cfamiliaris",  dbFile=db)
196     cfGenome.createGeneDB(db)
197
198
199 def createDBindices(db):
200     cfGenome = Genome("cfamiliaris", dbFile=db)
201     cfGenome.createIndices()
202
203
204 def buildDogDB(db=geneDB):
205     genePath = "%s/download/seq_gene.md" % cisRoot
206     chromos = {"1": "%s/download/chr1.fa" % cisRoot,
207                "2": "%s/download/chr2.fa" % cisRoot,
208                "3": "%s/download/chr3.fa" % cisRoot,
209                "4": "%s/download/chr4.fa" % cisRoot,
210                "5": "%s/download/chr5.fa" % cisRoot,
211                "6": "%s/download/chr6.fa" % cisRoot,
212                "7": "%s/download/chr7.fa" % cisRoot,
213                "8": "%s/download/chr8.fa" % cisRoot,
214                "9": "%s/download/chr9.fa" % cisRoot,
215                "10": "%s/download/chr10.fa" % cisRoot,
216                "11": "%s/download/chr11.fa" % cisRoot,
217                "12": "%s/download/chr12.fa" % cisRoot,
218                "13": "%s/download/chr13.fa" % cisRoot,
219                "14": "%s/download/chr14.fa" % cisRoot,
220                "15": "%s/download/chr15.fa" % cisRoot,
221                "16": "%s/download/chr16.fa" % cisRoot,
222                "17": "%s/download/chr17.fa" % cisRoot,
223                "18": "%s/download/chr18.fa" % cisRoot,
224                "19": "%s/download/chr19.fa" % cisRoot,
225                "20": "%s/download/chr20.fa" % cisRoot,
226                "21": "%s/download/chr21.fa" % cisRoot,
227                "22": "%s/download/chr22.fa" % cisRoot,
228                '23': "%s/download/chr23.fa" % cisRoot,
229                "24": "%s/download/chr24.fa" % cisRoot,
230                "25": "%s/download/chr25.fa" % cisRoot,
231                "26": "%s/download/chr26.fa" % cisRoot,
232                "27": "%s/download/chr27.fa" % cisRoot,
233                "28": "%s/download/chr28.fa" % cisRoot,
234                "29": "%s/download/chr29.fa" % cisRoot,
235                "30": "%s/download/chr30.fa" % cisRoot,
236                "31": "%s/download/chr31.fa" % cisRoot,
237                "32": "%s/download/chr32.fa" % cisRoot,
238                "33": "%s/download/chr33.fa" % cisRoot,
239                "34": "%s/download/chr34.fa" % cisRoot,
240                "35": "%s/download/chr35.fa" % cisRoot,
241                "36": "%s/download/chr36.fa" % cisRoot,
242                "37": "%s/download/chr37.fa" % cisRoot,
243                "38": "%s/download/chr38.fa" % cisRoot,
244                "X": "%s/download/chrX.fa" % cisRoot,
245                "Un": "%s/download/chrUn.fa" % cisRoot
246     }
247
248     print "Creating database %s" % db
249     createDBFile(db)
250
251     print "Adding gene entries"
252     loadGeneEntries(db, genePath)
253
254     print "Adding gene features"
255     loadGeneFeatures(db, genePath)
256
257     for chromID in chromos.keys():
258         print "Loading chromosome %s" % chromID
259         loadChromosome(db, chromID, chromos[chromID], "/C_familiaris/chromo%s.bin" % chromID)
260
261     print "Creating Indices"
262     createDBindices(db)
263
264     print "Finished creating database %s" % db