annotate commons/core/coord/Align.py @ 22:1e3f2c2657a3

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