annotate SMART/Java/Python/structure/Transcript.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children 94ab73e8a190
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 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 from SMART.Java.Python.structure.Interval import Interval
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from SMART.Java.Python.structure.Sequence import Sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 class Transcript(Interval):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 A class that models an transcript, considered as a specialized interval (the bounds of the transcript) that contains exons (also represented as intervals)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 @ivar exons: a list of exons (intervals)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 @type exons: list of L{Interval{Interval}}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 def __init__(self, transcript = None, verbosity = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 @param transcript: transcript to be copied
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 @param verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 super(Transcript, self).__init__(None, verbosity)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.exons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 self.introns = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 if transcript != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 self.copy(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 def copy(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 Copy method
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 @param transcript: transcript to be copied
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 @type transcript: class L{Transcript<Transcript>} or L{Interval<Interval>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 super(Transcript, self).copy(transcript)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 if transcript.__class__.__name__ == "Transcript":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 exons = transcript.getExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 if len(exons) > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 for exon in exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 exonCopy = Interval(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 self.addExon(exonCopy)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 def setDirection(self, direction):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 Set the direction of the interval
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 Possibly parse different formats
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 Impact all exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 @param direction: direction of the transcript (+ / -)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 @type direction: int or string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 super(Transcript, self).setDirection(direction)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 exon.setDirection(direction)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 def setChromosome(self, chromosome):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 Set the chromosome
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 @param chromosome: chromosome on which the transcript is
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 @type chromosome: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 super(Transcript, self).setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 exon.setChromosome(chromosome)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 def addExon(self, exon):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 Add an exon to the list of exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 @param exon: a new exon
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 @type exon: class L{Interval<Interval>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 if not self.exons and not exon.overlapWith(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 firstExon = Interval()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 firstExon.setStart(self.getStart())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 firstExon.setEnd(self.getEnd())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 firstExon.setDirection(self.getDirection())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 firstExon.setChromosome(self.getChromosome())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 self.exons.append(firstExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 newExon = Interval(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 newExon.setDirection(self.getDirection())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 self.exons.append(newExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 if newExon.getStart() < self.getStart():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 self.setStart(newExon.getStart())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 if newExon.getEnd() > self.getEnd():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 self.setEnd(newExon.getEnd())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 def setStart(self, start):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 Set the new start, move the first exon accordingly (if exists)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 @param start: the new start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 @type start: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 super(Transcript, self).setStart(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 if self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 self.exons[0].setStart(start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 def setEnd(self, end):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 Set the new end, move the last exon accordingly (if exists)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 @param end: the new end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 @type end: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 super(Transcript, self).setEnd(end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 if self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 self.exons[-1].setEnd(end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 def reverse(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 Reverse the strand of the transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 super(Transcript, self).reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 exon.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 def getUniqueName(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 Try to give a unique name by possibly adding occurrence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 if "nbOccurrences" in self.tags and "occurrence" in self.tags and self.tags["nbOccurrences"] != 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 return "%s-%d" % (self.name, self.tags["occurrence"])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 return self.name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 def getNbExons(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 Get the number of exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 return max(1, len(self.exons))
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 getExon(self, i):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 Get a specific exon
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 @param i: the rank of the exon
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 @type i: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 if len(self.exons) == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 if i != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 raise Exception("Cannot get exon #%i while there is no exon in the transcript" % (i))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 return self
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 return self.exons[i]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 def getExons(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 Get all the exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 if len(self.exons) == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 return [Interval(self)]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 return self.exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 def getIntrons(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 Get all the introns
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 Compute introns on the fly
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 if self.introns != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 return self.introns
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 self.introns = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 exonStart = self.getExon(0)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 for cpt, exonEnd in enumerate(self.exons[1:]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 intron = Interval()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 intron.setName("%s_intron%d" % (self.getName(), cpt+1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 intron.setChromosome(self.getChromosome())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 intron.setDirection(self.getDirection())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 if self.getDirection() == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 intron.setEnd(exonEnd.getStart() - 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 intron.setStart(exonStart.getEnd() + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 intron.setStart(exonEnd.getEnd() + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 intron.setEnd(exonStart.getStart() - 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 intron.setDirection(self.getDirection())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 if intron.getSize() > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 self.introns.append(intron)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 exonStart = exonEnd
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 intron.setSize(intron.getEnd() - intron.getStart() + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 return self.introns
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 def getSize(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 Get the size of the transcript (i.e. the number of nucleotides)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 Compute size on the fly
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 if len(self.exons) == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 return self.getSizeWithIntrons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225 size = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 size += exon.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 return size
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 def getSizeWithIntrons(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 Get the size of the interval (i.e. distance from start to end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 return super(Transcript, self).getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 def overlapWithExon(self, transcript, nbNucleotides = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 Check if the exons of this transcript overlap with the exons of another transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 @param transcript: transcript to be compared to
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 @param nbNucleotides: minimum number of nucleotides to declare and overlap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 @type nbNucleotides: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 if not self.overlapWith(transcript, nbNucleotides):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 for thisExon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 for thatExon in transcript.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 if thisExon.overlapWith(thatExon, nbNucleotides):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 def include(self, transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 Whether this transcript includes the other one
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 @param transcript: object to be compared to
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 if not super(Transcript, self).include(transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 for thatExon in transcript.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 for thisExon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 if thisExon.include(thatExon):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 def merge(self, transcript, normalization = False):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 Merge with another transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 Merge exons if they overlap, otherwise add exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 @param transcript: transcript to be merged to
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 @param normalization: whether the sum of the merge should be normalized wrt the number of mappings of each elements
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 @type normalization: boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 if self.getChromosome() != transcript.getChromosome() or self.getDirection() != transcript.getDirection():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 raise Exception("Cannot merge '%s' with '%s'!" % (self, transcript))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 theseExons = self.getExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 thoseExons = transcript.getExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 for thatExon in thoseExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 toBeRemoved = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 for thisIndex, thisExon in enumerate(theseExons):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 if thisExon.overlapWith(thatExon):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 thatExon.merge(thisExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 toBeRemoved.append(thisIndex)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 theseExons.append(thatExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 for thisIndex in reversed(toBeRemoved):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 del theseExons[thisIndex]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 self.removeExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 self.setStart(min(self.getStart(), transcript.getStart()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 self.setEnd(max(self.getEnd(), transcript.getEnd()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 if len(theseExons) > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 for thisExon in theseExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 self.addExon(thisExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303 self.setName("%s--%s" % (self.getUniqueName(), transcript.getUniqueName()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 super(Transcript, self).merge(transcript, normalization)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 def getDifference(self, transcript, sameStrand = False):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 Get the difference between this cluster and another one
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 @param transcript: object to be compared to
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 @type transcript: class L{Transcript<Transcript>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 @param sameStrand: do the comparison iff the transcripts are on the same strand
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 @type sameStrand: boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 @return: a transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 newTranscript = Transcript()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 newTranscript.copy(self)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 if self.getChromosome() != transcript.getChromosome():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319 return newTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 if not self.overlapWith(transcript):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 return newTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 if sameStrand and self.getDirection() != transcript.getDirection():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 return newTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 newTranscript.removeExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 if transcript.getEnd() > newTranscript.getStart():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 newTranscript.setStart(transcript.getEnd() + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 if transcript.getStart() < newTranscript.getEnd():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 newTranscript.setEnd(transcript.getStart() + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 theseExons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 exonCopy = Interval()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 exonCopy.copy(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 theseExons.append(exonCopy)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 for thatExon in transcript.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 newExons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 for thisExon in theseExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 newExons.extend(thisExon.getDifference(thatExon))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 theseExons = newExons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 if not theseExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 return None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341 newStart, newEnd = theseExons[0].getStart(), theseExons[0].getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 for thisExon in theseExons[1:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 newStart = min(newStart, thisExon.getStart())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 newEnd = max(newEnd, thisExon.getEnd())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345 newTranscript.setEnd(newEnd)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 newTranscript.setStart(newStart)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347 newTranscript.exons = theseExons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 return newTranscript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 def getSqlVariables(cls):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 Get the properties of the object that should be saved in a database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 variables = Interval.getSqlVariables()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 variables.append("exons")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 return variables
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 getSqlVariables = classmethod(getSqlVariables)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361 def setSqlValues(self, array):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363 Set the values of the properties of this object as given by a results line of a SQL query
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 @param array: the values to be copied
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 @type array: a list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 super(Transcript, self).setSqlValues(array)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368 mergedExons = array[8]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 if not mergedExons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 for exonCount, splittedExon in enumerate(mergedExons.split(",")):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372 start, end = splittedExon.split("-")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 exon = Interval()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 exon.setChromosome(self.getChromosome())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 exon.setDirection(self.getDirection())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376 exon.setName("%s_exon%d" % (self.getName(), exonCount+1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377 exon.setStart(int(start))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 exon.setEnd(int(end))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379 self.addExon(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382 def getSqlValues(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384 Get the values of the properties that should be saved in a database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386 values = super(Transcript, self).getSqlValues()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387 values["size"] = self.getSize()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
388 if self.getNbExons() == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
389 values["exons"] = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
390 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
391 values["exons"] = ",".join(["%d-%d" % (exon.getStart(), exon.getEnd()) for exon in self.getExons()])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
392 return values
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
393
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
394
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
395 def getSqlTypes(cls):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
396 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
397 Get the types of the properties that should be saved in a database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
398 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
399 types = Interval.getSqlTypes()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
400 types["exons"] = "varchar"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
401 return types
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
402 getSqlTypes = classmethod(getSqlTypes)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
403
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
404
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
405 def getSqlSizes(cls):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
406 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
407 Get the sizes of the properties that should be saved in a database
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
408 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
409 sizes = Interval.getSqlSizes()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
410 sizes["exons"] = 10000
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
411 return sizes
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
412 getSqlSizes = classmethod(getSqlSizes)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
413
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
414
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
415 def sortExons(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
416 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
417 Sort the exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
418 Increasing order if transcript is on strand "+", decreasing otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
419 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
420 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
421 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
422 exons = self.getExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
423 exons.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
424 self.exons = exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
425
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
426
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
427 def sortExonsIncreasing(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
428 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
429 Sort the exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
430 Increasing order
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
431 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
432 exons = self.getExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
433 sortedExons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
434 while len(exons) > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
435 minExon = exons[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
436 for index in range(1, len(exons)):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
437 if minExon.getStart() > exons[index].getStart():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
438 minExon = exons[index]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
439 sortedExons.append(minExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
440 exons.remove(minExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
441 self.exons = sortedExons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
442
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
443
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
444 def extendStart(self, size):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
445 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
446 Extend the transcript by the 5' end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
447 @param size: the size to be extended
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
448 @type size: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
449 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
450 if len(self.exons) != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
451 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
452 if self.getDirection() == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
453 self.exons[0].setStart(max(0, self.exons[0].getStart() - size))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
454 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
455 self.exons[0].setEnd(self.exons[0].getEnd() + size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
456 super(Transcript, self).extendStart(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
457 self.bin = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
458
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
459
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
460 def extendEnd(self, size):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
461 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
462 Extend the transcript by the 3' end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
463 @param size: the size to be extended
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
464 @type size: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
465 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
466 if len(self.exons) != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
467 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
468 if self.getDirection() == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
469 self.exons[-1].setEnd(self.exons[-1].getEnd() + size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
470 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
471 self.exons[-1].setStart(max(0, self.exons[-1].getStart() - size))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
472 super(Transcript, self).extendEnd(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
473 self.bin = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
474
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
475
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
476 def extendExons(self, size):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
477 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
478 Extend all the exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
479 @param size: the size to be extended
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
480 @type size: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
481 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
482 if len(self.exons) != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
483 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
484 exons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
485 previousExon = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
486 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
487 exon.extendStart(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
488 exon.extendEnd(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
489 exon.setDirection(self.getDirection())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
490 if previousExon != None and previousExon.overlapWith(exon):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
491 previousExon.merge(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
492 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
493 if previousExon != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
494 exons.append(previousExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
495 previousExon = exon
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
496 exons.append(previousExon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
497 self.exons = exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
498 super(Transcript, self).extendStart(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
499 super(Transcript, self).extendEnd(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
500 self.bin = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
501
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
502
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
503 def restrictStart(self, size = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
504 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
505 Restrict the transcript by some nucleotides, start from its start position
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
506 Remove the exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
507 @param size: the size to be restricted to
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
508 @type size: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
509 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
510 newExons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
511 if self.getDirection() == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
512 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
513 if exon.getStart() <= self.getStart() + size - 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
514 if exon.getEnd() > self.getStart() + size - 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
515 exon.setEnd(self.getStart() + size - 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
516 newExons.append(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
517 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
518 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
519 if exon.getEnd() >= self.getEnd() - size + 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
520 if exon.getStart() < self.getEnd() - size + 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
521 exon.setStart(self.getEnd() - size + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
522 newExons.append(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
523 super(Transcript, self).restrictStart(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
524 self.exons = newExons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
525
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
526
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
527 def restrictEnd(self, size = 1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
528 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
529 Restrict the transcript by some nucleotides, end from its end position
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
530 Remove the exons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
531 @param size: the size to be restricted to
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
532 @type size: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
533 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
534 newExons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
535 if self.getDirection() == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
536 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
537 if exon.getEnd() >= self.getEnd() - size + 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
538 if exon.getStart() < self.getEnd() - size + 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
539 exon.setStart(self.getEnd() - size + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
540 newExons.append(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
541 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
542 for exon in self.exons:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
543 if exon.getEnd() >= self.getEnd() - size + 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
544 if exon.getStart() < self.getEnd() - size + 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
545 exon.setEnd(self.getEnd() - size + 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
546 newExons.append(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
547 super(Transcript, self).restrictEnd(size)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
548 self.exons = newExons
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
549
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
550
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
551 def removeExons(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
552 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
553 Remove the exons and transforms the current transcript into a mere interval
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
554 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
555 self.exons = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
556 self.bin = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
557
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
558
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
559 def printGtf(self, title):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
560 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
561 Export this transcript using GTF2.2 format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
562 @param title: the title of the transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
563 @type title: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
564 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
565 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
566 transcriptId = self.getUniqueName()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
567 geneId = "%s_gene" % (transcriptId)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
568 direction = "+"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
569 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
570 direction = "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
571 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
572 string = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
573 for i, exon in enumerate(self.getExons()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
574 exonCopy = Interval()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
575 exonCopy.copy(exon)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
576 if "ID" in exonCopy.getTagValues():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
577 del exonCopy.tags["ID"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
578 feature = "exon"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
579 if "feature" in exonCopy.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
580 feature = exonCopy.getTagValue("feature")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
581 del exonCopy.tags["feature"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
582 score = "."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
583 if "score" in exonCopy.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
584 score = "%d" % (int(exonCopy.getTagValue("score")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
585 del exonCopy.tags["score"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
586 if "Parent" in exonCopy.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
587 del exonCopy.tags["Parent"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
588 exonCopy.setName("%s_part%d" % (self.getName(), i+1))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
589 comment = exonCopy.getTagValues("; ", " ", "\"")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
590 string += "%s\t%s\t%s\t%d\t%d\t%s\t%s\t.\ttranscript_id \"%s\"; gene_id \"%s\"; %s\n" % (exonCopy.getChromosome(), title, feature, exonCopy.getStart(), exonCopy.getEnd(), score, direction, transcriptId, geneId, comment)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
591 return string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
592
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
593
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
594 def printGff2(self, title):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
595 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
596 Export this transcript using GFF2 format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
597 @param title: the title of the transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
598 @type title: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
599 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
600 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
601 direction = "+"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
602 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
603 direction = "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
604 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
605 comment = self.getTagValues()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
606 if comment != None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
607 comment = ";%s" % (comment)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
608 score = "."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
609 if "score" in self.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
610 score = "%d" % (int(self.getTagValue("score")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
611 feature = "transcript"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
612 if "feature" in self.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
613 feature = self.getTagValue("feature")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
614 string = "%s\t%s\t%s\t%d\t%d\t%s\t%s\t.\tGENE %s%s\n" % (self.getChromosome(), title, feature, self.getStart(), self.getEnd(), score, direction, self.name, comment)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
615 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
616 if "score" in exon.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
617 score = "%d" % (int(self.getTagValue("score")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
618 string += "%s\t%s\t_exon\t%d\t%d\t%s\t%s\t.\tGENE %s\n" % (self.getChromosome(), title, exon.getStart(), exon.getEnd(), score, direction, self.name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
619 return string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
620
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
621
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
622 def printGff3(self, title):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
623 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
624 Export this transcript using GFF3 format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
625 @param title: the title of the transcripts
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
626 @type title: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
627 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
628 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
629 direction = "+"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
630 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
631 direction = "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
632 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
633 if "ID" not in self.getTagValues():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
634 self.setTagValue("ID", self.getUniqueName())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
635 feature = "transcript"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
636 tags = self.tags
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
637 if "feature" in self.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
638 feature = self.getTagValue("feature")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
639 del self.tags["feature"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
640 score = "."
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
641 if "score" in self.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
642 score = "%d" % (int(self.getTagValue("score")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
643 del self.tags["score"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
644 comment = self.getTagValues(";", "=")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
645 string = "%s\t%s\t%s\t%d\t%d\t%s\t%s\t.\t%s\n" % (self.getChromosome(), title, feature, self.getStart(), self.getEnd(), score, direction, comment)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
646 if len(self.exons) > 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
647 for i, exon in enumerate(self.getExons()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
648 if "score" in exon.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
649 score = "%d" % (int(exon.getTagValue("score")))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
650 string += "%s\t%s\texon\t%d\t%d\t%s\t%s\t.\tID=%s-exon%d;Name=%s-exon%d;Parent=%s\n" % (self.getChromosome(), title, exon.getStart(), exon.getEnd(), score, direction, self.getTagValue("ID"), i+1, self.name, i+1, self.getTagValue("ID"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
651 self.tags = tags
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
652 return string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
653
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
654
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
655 def printEmbl(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
656 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
657 Export this transcript using EMBL format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
658 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
659 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
660 if len(self.exons) <= 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
661 position = "%d..%d" % (self.getStart(), self.getEnd())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
662 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
663 positions = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
664 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
665 positions.append("%d..%d" % (self.getStart(), self.getEnd()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
666 position = ",".join(positions)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
667 position = "join(%s)" % (position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
668 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
669 position = "complement(%s)" % (position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
670 feature = "misc_feature"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
671 if "feature" in self.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
672 if not self.getTagValue("feature").startswith("S-MART"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
673 feature = self.getTagValue("feature")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
674 string = "FT %s %s\n" % (feature, position)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
675 if "Name" in self.getTagNames():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
676 string += "FT /label=\"%s\"\n" % (self.getTagValue("Name"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
677 return string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
678
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
679
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
680 def printBed(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
681 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
682 Export this transcript using BED format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
683 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
684 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
685 name = self.name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
686 if "nbOccurrences" in self.getTagNames() and self.getTagValue("nbOccurrences") != 1 and self.getTagValue("occurrences"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
687 name = "%s-%d" % (name, self.getTagValue("occurrence"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
688 comment = self.getTagValues(";", "=")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
689 sizes = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
690 starts = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
691 direction = "+"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
692 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
693 direction = "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
694 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
695 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
696 sizes.append("%d" % (exon.getSize()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
697 starts.append("%d" % (exon.getStart() - self.getStart()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
698 return "%s\t%d\t%d\t%s\t1000\t%s\t%d\t%d\t0\t%d\t%s,\t%s,\n" % (self.getChromosome(), self.getStart(), self.getEnd()+1, name, direction, self.getStart(), self.getEnd()+1, self.getNbExons(), ",".join(sizes), ",".join(starts))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
699
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
700
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
701 def printSam(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
702 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
703 Export this transcript using SAM format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
704 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
705 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
706 name = self.name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
707 flag = 0 if self.getDirection() == 1 else 0x10
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
708 chromosome = self.getChromosome()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
709 genomeStart = self.getStart()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
710 quality = 255
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
711 mate = "*"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
712 mateGenomeStart = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
713 gapSize = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
714 sequence = "*"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
715 qualityString = "*"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
716 tags = "NM:i:0"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
717
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
718 lastExonEnd = None
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
719 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
720 exon = self.getExons()[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
721 cigar = "%dM" % (self.getExons()[0].getSize())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
722 lastExonEnd = exon.getEnd()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
723 for i, exon in enumerate(self.getExons()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
724 if i == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
725 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
726 cigar += "%dN" % (exon.getStart() - lastExonEnd - 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
727 cigar += "%dM" % (exon.getSize())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
728
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
729 return "%s\t%d\t%s\t%d\t%d\t%s\t%s\t%d\t%d\t%s\t%s\t%s\n" % (name, flag, chromosome, genomeStart, quality, cigar, mate, mateGenomeStart, gapSize, sequence, qualityString, tags)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
730
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
731
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
732 def printUcsc(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
733 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
734 Export this transcript using UCSC BED format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
735 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
736 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
737 if self.getChromosome().find("Het") != -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
738 return ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
739 name = self.name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
740 comment = self.getTagValues(";", "")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
741 sizes = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
742 starts = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
743 direction = "+"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
744 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
745 direction = "-"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
746 self.sortExonsIncreasing()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
747 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
748 sizes.append("%d" % (exon.getSize()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
749 starts.append("%d" % (exon.getStart() - self.getStart()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
750 return "%s\t%d\t%d\t%s\t1000\t%s\t%d\t%d\t0\t%d\t%s,\t%s,\n" % (self.getChromosome().replace("arm_", "chr"), self.getStart(), self.getEnd()+1, name, direction, self.getStart(), self.getEnd()+1, self.getNbExons(), ",".join(sizes), ",".join(starts))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
751
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
752
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
753 def printGBrowseReference(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
754 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
755 Export this transcript using GBrowse format (1st line only)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
756 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
757 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
758 return "reference = %s\n" % (self.getChromosome())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
759
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
760
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
761 def printGBrowseLine(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
762 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
763 Export this transcript using GBrowse format (2nd line only)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
764 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
765 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
766 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
767 coordinates = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
768 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
769 coordinates.append(exon.printCoordinates())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
770 coordinatesString = ",".join(coordinates)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
771 comment = self.getTagValues(";", "=")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
772 if comment:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
773 comment = "\t\"%s\"" % (comment)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
774 return "User_data\t%s\t%s%s\n" % (self.name, coordinatesString, comment)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
775
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
776
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
777 def printGBrowse(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
778 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
779 Export this transcript using GBrowse format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
780 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
781 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
782 return "%s%s" % (self.printGBrowseReference(), self.printGBrowseLine())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
783
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
784
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
785 def printCsv(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
786 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
787 Export this transcript using CSV format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
788 @return: a string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
789 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
790 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
791 string = "%s,%d,%d,\"%s\"," % (self.getChromosome(), self.getStart(), self.getEnd(), "+" if self.getDirection() == 1 else "-")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
792 if len(self.getExons()) == 1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
793 string += "None"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
794 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
795 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
796 string += "%d-%d " % (exon.getStart(), exon.getEnd())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
797 for tag in sorted(self.tags.keys()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
798 string += ",%s=%s" % (tag, str(self.tags[tag]))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
799 string += "\n"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
800 return string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
801
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
802
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
803 def extractSequence(self, parser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
804 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
805 Get the sequence corresponding to this transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
806 @param parser: a parser to a FASTA file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
807 @type parser: class L{SequenceListParser<SequenceListParser>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
808 @return: an instance of L{Sequence<Sequence>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
809 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
810 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
811 name = self.name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
812 if "ID" in self.getTagNames() and self.getTagValue("ID") != self.name:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
813 name += ":%s" % (self.getTagValue("ID"))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
814 sequence = Sequence(name)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
815 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
816 sequence.concatenate(exon.extractSequence(parser))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
817 return sequence
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
818
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
819
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
820 def extractWigData(self, parser):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
821 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
822 Get some wig data corresponding to this transcript
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
823 @param parser: a parser to a wig file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
824 @type parser: class L{WigParser<WigParser>}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
825 @return: a sequence of float
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
826 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
827 self.sortExons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
828 if parser.strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
829 strands = (-1, 1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
830 values = dict([(strand, []) for strand in strands])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
831 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
832 theseValues = exon.extractWigData(parser)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
833 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
834 for strand in strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
835 theseValues[strand].reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
836 for strand in strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
837 values[strand].extend(theseValues[strand])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
838 if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
839 for strand in strands:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
840 values[strand].reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
841 return values
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
842 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
843 values = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
844 for exon in self.getExons():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
845 theseValues = exon.extractWigData(parser)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
846 #if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
847 # theseValues.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
848 values.extend(theseValues)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
849 #if self.getDirection() == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
850 # values.reverse()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
851 return values