annotate TEisotools-1.1.a/commons/core/coord/Align.py @ 15:255c852351c5 draft

Uploaded
author urgi-team
date Thu, 21 Jul 2016 07:36:44 -0400
parents feef9a0db09d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
4 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
10 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
15 # liability.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
16 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
27 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
30
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
31 import time
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
32 from commons.core.coord.Map import Map
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
33 from commons.core.coord.Range import Range
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35 ## Handle a match between two sequences, query and subject (pair of coordinates with E-value, score and identity)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37 class Align( object ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39 __slots__ = ("range_query", "range_subject", "e_value", "score", "identity", '__dict__')
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41 ## Constructor
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43 # @param range_q: a Range instance for the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44 # @param range_s: a Range instance for the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45 # @param e_value: E-value of the match
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
46 # @param identity: identity percentage of the match
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47 # @param score: score of the match
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 def __init__(self, range_q=Range(), range_s=Range(), e_value=0, score=0, identity=0):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 self.range_query = range_q
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 self.range_subject = range_s
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 self.e_value = float(e_value)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 self.score = float(score)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 self.identity = float(identity)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56 ## Return True if the instance is empty, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 def isEmpty(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 return self.range_query.isEmpty() or self.range_subject.isEmpty()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 ## Equal operator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 def __eq__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 if type(o) is not type(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 return self.range_query==o.range_query and self.range_subject==o.range_subject and \
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 self.e_value==o.e_value and self.score==o.score and self.identity==o.identity
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70 ## Unequal operator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 # @param o a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 def __ne__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75 return not self.__eq__(o)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 ## Convert the object into a string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 # @note used in 'print myObject'
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 def __str__( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 return self.toString()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84 ## Read attributes from an Align file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 # @param fileHandler: file handler of the file being read
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 # @return: 1 on success, 0 at the end of the file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 def read(self, fileHandler):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 self.reset()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 line = fileHandler.readline()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 if line == "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 return 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 tokens = line.split("\t")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95 if len(tokens) < 5:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 return 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 self.setFromTuple(tokens)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 return 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 ## Set attributes from tuple
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 # @param tuple a tuple with (queryName,queryStart,queryEnd,subjectName,subjectStar,subjectEnd,E-value,score,identity)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 # @note data are loaded such that the query is always on the direct strand
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 def setFromTuple( self, tuple ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 #TODO: we need to create Range instances because of __eq__() and isEmpty() tests, but WHY ???
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107 self.range_query = Range()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 self.range_subject = Range()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 if int(tuple[1]) < int(tuple[2]):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 self.range_query.setFromTuple( ( tuple[0], tuple[1], tuple[2] ) )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 self.range_subject.setFromTuple( ( tuple[3], tuple[4], tuple[5] ) )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 self.range_query.setFromTuple( ( tuple[0], tuple[2], tuple[1] ) )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 self.range_subject.setFromTuple( ( tuple[3], tuple[5], tuple[4] ) )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 self.e_value = float(tuple[6])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116 self.score = float(tuple[7])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117 self.identity = float(tuple[8])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 ## Reset
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 def reset( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 self.range_query.reset()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 self.range_subject.reset()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 self.e_value = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 self.score = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126 self.identity = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 ## Return the attributes as a formatted string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 def toString(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 string = "%s" % ( self.range_query.toString() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132 string += "\t%s" % ( self.range_subject.toString() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133 string += "\t%g\t%i\t%f" % ( self.e_value, self.score, self.identity )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134 return string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137 ## Return the attributes as a GFF-formatted string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 def toStringAsGff( self, source="REPET", type="match", phase=".", ID="", Parent="" ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140 if not self.isSubjectOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 self.reverse()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142 string = "%s" % ( self.getQueryName() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 string += "\t%s" % ( source )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144 string += "\t%s" % ( type )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145 string += "\t%s" % ( self.getQueryMin() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 string += "\t%s" % ( self.getQueryMax() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147 string += "\t%g" % ( self.e_value )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 string += "\t%s" % ( self.getQueryStrand() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149 string += "\t%s" % ( phase )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
150 attributes = ""
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
151 if ID != "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
152 attributes += "ID=%s" % ( ID )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
153 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
154 attributes += "ID=%i" % ( str(time.time())[-8:-1].replace(".","") )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
155 if Parent != "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
156 attributes += ";Parent=%s" % ( Parent )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
157 attributes += ";Target=%s %i %i" % ( self.getSubjectName(), self.getSubjectStart(), self.getSubjectEnd() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
158 string += "\t%s" % ( attributes )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
159 return string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
160
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
161
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
162 ## Reverse query and subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
163 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
164 def reverse(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
165 self.range_query.reverse()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
166 self.range_subject.reverse()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
167
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
168 ## Show the attributes
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
169 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
170 def show(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
171 print self.toString()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
172
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
173 ## Write attributes into an Align file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
174 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
175 # @param fileHandler: file handler of the file being filled
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
176 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
177 def write(self, fileHandler):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
178 fileHandler.write("%s\n" % (self.toString()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
179
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
180 ## Save attributes into an Align file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
181 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
182 # @param file: name of the file being filled
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
183 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
184 def save(self, file):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
185 fileHandler = open( file, "a" )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
186 self.write( fileHandler )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
187 fileHandler.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
188
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
189 ## Return the score
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
190 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
191 def getScore(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
192 return self.score
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
193
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
194 ## Return the identity
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
195 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
196 def getIdentity(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
197 return self.identity
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
198
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
199 def getEvalue(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
200 return self.e_value
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
201
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
202 ## Return the length on the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
203 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
204 def getLengthOnQuery(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
205 return self.range_query.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
206
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
207 ## Return the name of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
208 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
209 def getQueryName( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
210 return self.range_query.seqname
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
211
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
212 ## Return the start of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
213 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
214 def getQueryStart( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
215 return self.range_query.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
216
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
217 ## Return the end of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
218 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
219 def getQueryEnd( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
220 return self.range_query.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
221
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
222 ## Return the min of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
223 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
224 def getQueryMin( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
225 return self.range_query.getMin()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
226
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
227 ## Return the max of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
228 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
229 def getQueryMax( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
230 return self.range_query.getMax()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
231
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
232 ## Return the strand of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
233 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
234 def getQueryStrand( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
235 return self.range_query.getStrand()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
236
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
237 ## Return the length on the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
238 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
239 def getLengthOnSubject(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
240 return self.range_subject.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
241
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
242 ## Return the name of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
243 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
244 def getSubjectName( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
245 return self.range_subject.seqname
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
246
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
247 ## Return the start of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
248 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
249 def getSubjectStart( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
250 return self.range_subject.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
251
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
252 ## Return the end of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
253 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
254 def getSubjectEnd( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
255 return self.range_subject.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
256
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
257 ## Return the min of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
258 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
259 def getSubjectMin( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
260 return self.range_subject.getMin()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
261
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
262 ## Return the max of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
263 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
264 def getSubjectMax( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
265 return self.range_subject.getMax()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
266
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
267 ## Return the strand of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
268 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
269 def getSubjectStrand( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
270 return self.range_subject.getStrand()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
271
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
272 ## Return the query as a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
273 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
274 def getQueryAsRange( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
275 return self.range_query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
276
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
277 ## Return the subject as a Range instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
278 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
279 def getSubjectAsRange( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
280 return self.range_subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
281
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
282 ## Set the name of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
283 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
284 def setQueryName( self, name ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
285 self.range_query.seqname = name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
286
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
287 ## Set the start of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
288 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
289 def setQueryStart( self, start ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
290 self.range_query.start = start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
291
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
292 ## Set the end of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
293 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
294 def setQueryEnd( self, end ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
295 self.range_query.end = end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
296
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
297 ## Set the name of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
298 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
299 def setSubjectName( self, name ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
300 self.range_subject.seqname = name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
301
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
302 ## Set the start of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
303 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
304 def setSubjectStart( self, start ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
305 self.range_subject.start = start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
306
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
307 ## Set the end of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
308 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
309 def setSubjectEnd( self, end ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
310 self.range_subject.end = end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
311
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
312 ## Merge the instance with another Align instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
313 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
314 # @param o an Align instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
315 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
316 def merge(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
317 if self.range_query.seqname != o.range_query.seqname \
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
318 or self.range_subject.seqname != o.range_subject.seqname:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
319 return
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
320 self.range_query.merge(o.range_query)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
321 self.range_subject.merge(o.range_subject)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
322 self.score = max(self.score,o.score)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
323 self.e_value = min(self.e_value,o.e_value)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
324 self.identity = max(self.identity,o.identity)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
325
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
326 ## Return a Map instance with the subject mapped on the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
327 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
328 def getSubjectAsMapOfQuery(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
329 iMap = Map()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
330 iMap.name = self.range_subject.seqname
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
331 iMap.seqname = self.range_query.seqname
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
332 if self.range_subject.isOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
333 iMap.start = self.range_query.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
334 iMap.end = self.range_query.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
335 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
336 iMap.start = self.range_query.end
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
337 iMap.end = self.range_query.start
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
338 return iMap
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
339
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
340 ## Return True if query is on direct strand
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
341 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
342 def isQueryOnDirectStrand( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
343 return self.range_query.isOnDirectStrand()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
344
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
345 ## Return True if subject is on direct strand
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
346 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
347 def isSubjectOnDirectStrand( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
348 return self.range_subject.isOnDirectStrand()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
349
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
350 ## Return True if query and subject are on the same strand, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
351 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
352 def areQrySbjOnSameStrand(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
353 return self.isQueryOnDirectStrand() == self.isSubjectOnDirectStrand()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
354
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
355 ## Return False if query and subject are on the same strand, True otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
356 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
357 def areQrySbjOnOppositeStrands(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
358 return not self.areQrySbjOnSameStrand()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
359
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
360 ## Set attributes from string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
361 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
362 # @param string a string formatted like queryName queryStart queryEnd subjectName subjectStart subjectEnd E-value score identity
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
363 # @param sep field separator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
364 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
365 def setFromString(self, string, sep="\t"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
366 if string[-1] == "\n":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
367 string = string[:-1]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
368 self.setFromTuple( string.split(sep) )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
369
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
370 ## Return a first Map instance for the query and a second for the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
371 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
372 def getMapsOfQueryAndSubject(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
373 iMapQuery = Map( name="repet",
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
374 seqname=self.range_query.seqname,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
375 start=self.range_query.start,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
376 end=self.range_query.end )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
377 iMapSubject = Map( name="repet",
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
378 seqname=self.range_subject.seqname,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
379 start=self.range_subject.start,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
380 end=self.range_subject.end )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
381 return iMapQuery, iMapSubject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
382
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
383 ## Write query coordinates as Map in a file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
384 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
385 # @param fileHandler: file handler of the file being filled
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
386 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
387 def writeSubjectAsMapOfQuery( self, fileHandler ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
388 m = self.getSubjectAsMapOfQuery()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
389 m.write( fileHandler )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
390
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
391 ## Return a bin for fast database access
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
392 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
393 def getBin(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
394 return self.range_query.getBin()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
395
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
396 ## Switch query and subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
397 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
398 def switchQuerySubject( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
399 tmpRange = self.range_query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
400 self.range_query = self.range_subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
401 self.range_subject = tmpRange
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
402 if not self.isQueryOnDirectStrand():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
403 self.reverse()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
404
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
405 ## Return True if the query overlaps with the query of another Align instance, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
406 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
407 def isQueryOverlapping( self, iAlign ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
408 return self.getQueryAsRange().isOverlapping( iAlign.getQueryAsRange() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
409
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
410 ## Return True if the subject overlaps with the subject of another Align instance, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
411 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
412 def isSubjectOverlapping( self, iAlign ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
413 return self.getSubjectAsRange().isOverlapping( iAlign.getSubjectAsRange() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
414
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
415 ## Return True if the Align instance overlaps with another Align instance, False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
416 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
417 def isOverlapping( self, iAlign ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
418 if self.isQueryOverlapping( iAlign ) and self.isSubjectOverlapping( iAlign ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
419 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
420 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
421 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
422
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
423 ## Update the score
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
424 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
425 # @note the new score is the length on the query times the percentage of identity
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
426 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
427 def updateScore( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
428 newScore = self.getLengthOnQuery() * self.getIdentity() / 100.0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
429 self.score = newScore