annotate SMART/Java/Python/structure/TranscriptList.py @ 18:94ab73e8a190

Uploaded
author m-zytnicki
date Mon, 29 Apr 2013 03:20:15 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 from SMART.Java.Python.structure.Transcript import Transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 from SMART.Java.Python.mySql.MySqlTable import MySqlTable
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from SMART.Java.Python.structure.Interval import Interval
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from SMART.Java.Python.misc.Progress import Progress
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 class TranscriptList(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 """A class that codes for a list of transcript"""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 def __init__(self, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 self.transcripts = dict()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 self.longestTranscript = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def getTranscript(self, chromosome, index):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 return self.transcripts[chromosome][index]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 def getChromosomes(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 return self.transcripts.keys()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 def getTranscriptsOnChromosome(self, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 if chromosome not in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 return []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 return self.transcripts[chromosome]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 def addTranscript(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 if transcript.getChromosome() in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.transcripts[transcript.getChromosome()].append(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self.transcripts[transcript.getChromosome()] = [transcript]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 self.longestTranscript = max(self.longestTranscript, transcript.getEnd() - transcript.getStart())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 def removeTranscript(self, chromosome, i):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 del self.transcripts[chromosome][i]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 def removeAll(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self.transcripts = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 def getNbTranscripts(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 nbTranscripts = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 for chromosome in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 nbTranscripts += len(self.transcripts[chromosome])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 return nbTranscripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 def getSize(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 size = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 for chromosome in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 for transcript in self.transcripts[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 size += transcript.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 return size
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 def sort(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 for chromosome in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 self.transcripts[chromosome].sort(lambda x, y: x.getStart() - y.getStart())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 def removeOverlapWith(self, transcriptList):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 transcriptList.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 for chromosome in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 progress = Progress(len(self.transcripts[chromosome]), "Handling chromosome %s" % (chromosome), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 for thisTranscriptId in range(len(self.transcripts[chromosome])):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 for thatTranscriptId in range(len(transcriptList.transcripts[chromosome])):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 if self.transcripts[chromosome][thisTranscriptId].overlapWith(transcriptList.transcripts[chromosome][thatTranscriptId]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 self.transcripts[chromosome][thisTranscriptId] = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 if self.transcripts[chromosome][thisTranscriptId].getEnd() > transcriptList.transcripts[chromosome][thatTranscriptId]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 self.transcripts[chromosome] = [transcript for transcript in self.transcripts[chromosome] if transcript != None]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 def removeOverlapWithExon(self, transcriptList):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 transcriptList.sort()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 for chromosome in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 progress = Progress(len(self.transcripts[chromosome]), "Handling chromosome %s" % (chromosome), self.verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 for thisTranscriptId in range(len(self.transcripts[chromosome])):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 progress.inc()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 for thatTranscriptId in range(len(transcriptList.transcripts[chromosome])):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 if self.transcripts[chromosome][thisTranscriptId].overlapWithExon(transcriptList.transcripts[chromosome][thatTranscriptId]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 self.transcripts[chromosome][thisTranscriptId] = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 if self.transcripts[chromosome][thisTranscriptId].getEnd() > transcriptList.transcripts[chromosome][thatTranscriptId]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 self.transcripts[chromosome] = [transcript for transcript in self.transcripts[chromosome] if transcript != None]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 progress.done()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 def setDefaultTagValue(self, name, value):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 for transcript in self.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 transcript.setTag(name, value)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 def storeDatabase(self, mySqlConnection):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 transcriptsTable = MySqlTable("TmpTranscriptsTable", mySqlConnection)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 transcriptsTable.create(Transcript.getSqlVariables(), Transcript.getSqlTypes())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 intervalsVariables = Interval.getSqlVariables()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 intervalsVariables.append("idTranscript")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 intervalsTypes = Interval.getSqlTypes()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 intervalsTypes["idTranscript"] = "int"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 intervalsTable = MySqlTable("TmpIntervalsTable", mySqlConnection)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 intervalsTable.create(intervalsVariables, intervalsTypes)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 for chromosome in self.transcripts:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 for transcript in self.transcripts[chromosome]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 idTranscript = transcriptsTable.addLine(transcript.getSqlValues())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 for exon in transcript.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 intervalValues = exon.getSqlValues()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 intervalValues["idTranscript"] = idTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 intervalsTable.addLine(intervalValues)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 def getIterator(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 chromosomes = self.transcripts.keys()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 currentChromosome = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 currentTranscript = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 if currentChromosome >= len(chromosomes):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 elif currentTranscript >= len(self.transcripts[chromosomes[currentChromosome]]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 currentTranscript = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 currentChromosome += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 elif self.transcripts[chromosomes[currentChromosome]][currentTranscript] == None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 currentTranscript += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 yield self.transcripts[chromosomes[currentChromosome]][currentTranscript]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 currentTranscript += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 def __str__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 string = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 for transcript in self.getIterator():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 string += str(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 return string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172