annotate TEisotools-1.1.a/commons/core/coord/Match.py @ 13:feef9a0db09d draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 09:04:42 -0400
parents
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
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
32 import sys
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
33 from commons.core.coord.Range import Range
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.coord.Path import Path
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37 ## Handle a chain of match(es) between two sequences, query and subject, with an identifier and the length of the input sequences
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39 class Match( Path ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41 __slots__ = ("query_length", "query_length_perc", "query_seqlength", "match_length_perc", "subject_length", "subject_length_perc", "subject_seqlength")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43 ## Constructor
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45 def __init__(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
46 Path.__init__(self)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47 self.query_length = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48 self.query_length_perc = -1 # length of the match on the query / length of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 self.query_seqlength = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 self.match_length_perc = -1 # length of the match on the query / total length of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 self.subject_length = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 self.subject_length_perc = -1 # length of the match on the subject / length of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 self.subject_seqlength = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 ## Equal operator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57 def __eq__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 if type(o) is not type(self)\
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 or self.query_length != o.query_length or self.query_length_perc != o.query_length_perc\
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60 or self.query_seqlength != o.query_seqlength or self.subject_length != o.subject_length\
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 or self.subject_length_perc != o.subject_length_perc or self.subject_seqlength != o.subject_seqlength\
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 or self.match_length_perc != o.match_length_perc:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 return Path.__eq__(self, o)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 ## Not equal operator
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 def __ne__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 return not self.__eq__(o)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71 ## Return the length of the match on the query divided by the total length of the query
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 def getLengthPercOnQuery(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 return self.query_length_perc
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 ## Return the length of the match on the subject divided by the total length of the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78 def getLengthPercOnSubject(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 return self.subject_length_perc
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 ## Return the length of the match on the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83 def getLengthMatchOnSubject(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84 return self.subject_length
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 ## Set attributes from a tuple
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 # @param tuple: a tuple with (query name,query start,query end,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 # query length, query length perc (between 0-1), match length perc (between 0-1), subject name,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 # subject start,subject end,subject length, subject length percentage (between 0-1), e_value,score,identity,id)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 def setFromTuple( self, tuple ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 queryStart = int(tuple[1])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 queryEnd = int(tuple[2])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95 subjectStart = int(tuple[7])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 subjectEnd = int(tuple[8])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 if queryStart < queryEnd:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 self.range_query = Range(tuple[0],queryStart,queryEnd)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 self.range_subject = Range(tuple[6],subjectStart,subjectEnd)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 self.range_query = Range(tuple[0],queryEnd,queryStart)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 self.range_subject = Range(tuple[6],subjectEnd,subjectStart)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 self.query_length = int(tuple[3])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 self.query_length_perc = float(tuple[4])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 self.query_seqlength = int( self.query_length / self.query_length_perc )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 self.match_length_perc = float(tuple[5])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107 self.subject_length = int(tuple[9])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 self.subject_length_perc = float(tuple[10])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 self.subject_seqlength = int( self.subject_length / self.subject_length_perc )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 self.e_value = float(tuple[11])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 self.score = float(tuple[12])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 self.identity = float(tuple[13])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 self.id = int(tuple[14])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 ## Reset
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117 def reset( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118 Path.reset( self )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 self.query_length = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120 self.query_length_perc = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 self.query_seqlength = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 self.match_length_perc = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 self.subject_length = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 self.subject_length_perc = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 self.subject_seqlength = -1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127 ## Return a formated string of the attribute data
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 def toString( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 string = "%s" % ( self.range_query.toString() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 string += "\t%i\t%f" % ( self.query_length,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132 self.query_length_perc )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133 string += "\t%f" % ( self.match_length_perc )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134 string += "\t%s" % ( self.range_subject.toString() )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135 string += "\t%i\t%f" % ( self.subject_length,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136 self.subject_length_perc )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137 string += "\t%g\t%i\t%f" % ( self.e_value,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138 self.score,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 self.identity )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140 string += "\t%i" % ( self.id )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 return string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 ## Return a Path instance
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145 def getPathInstance( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 p = Path()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147 tuple = ( self.id,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 self.range_query.seqname,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149 self.range_query.start,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
150 self.range_query.end,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
151 self.range_subject.seqname,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
152 self.range_subject.start,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
153 self.range_subject.end,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
154 self.e_value,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
155 self.score,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
156 self.identity )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
157 p.setFromTuple( tuple )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
158 return p
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
159
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
160 ## Give information about a match whose query is included in the subject
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
161 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
162 # @return string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
163 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
164 def getQryIsIncluded( self ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
165 string = "query %s (%d bp: %d-%d) is contained in subject %s (%d bp: %d-%d): id=%.2f - %.3f - %.3f - %.3f" %\
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
166 ( self.range_query.seqname, self.query_seqlength, self.range_query.start, self.range_query.end,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
167 self.range_subject.seqname, self.subject_seqlength, self.range_subject.start, self.range_subject.end,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
168 self.identity, self.query_length_perc, self.match_length_perc, self.subject_length_perc )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
169 return string
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
170
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
171 def increaseLengthPercOnQuery(self, coverage):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
172 self.query_length_perc += coverage
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
173
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
174 ## Compare the object with another match and see if they are equal
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
175 # (same identity, E-value and score + same subsequences whether in query or subject)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
176 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
177 # @return True if objects are equals False otherwise
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
178 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
179 def isDoublonWith( self, match, verbose=0 ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
180
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
181 # if both matches have same identity, score and E-value
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
182 if self.identity == match.identity and self.score == match.score and self.e_value == match.e_value:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
183
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
184 # if query and subject are identical
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
185 if ( self.range_query.seqname == match.range_query.seqname \
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
186 and self.range_subject.seqname == match.range_subject.seqname ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
187
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
188 # if the coordinates are equal
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
189 if self.range_query.__eq__( match.range_query ) and self.range_subject.__eq__( match.range_subject ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
190 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
191
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
192 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
193 if verbose > 0: print "different coordinates"; sys.stdout.flush()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
194 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
195
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
196 # if query and subject are reversed but identical
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
197 elif self.range_query.seqname == match.range_subject.seqname and self.range_subject.seqname == match.range_query.seqname:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
198
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
199 # if the coordinates are equal
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
200 if self.range_query.__eq__( match.range_subject ) and self.range_subject.__eq__( match.range_query ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
201 return True
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
202
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
203 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
204 if verbose > 0: print "different coordinates"; sys.stdout.flush()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
205 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
206
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
207 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
208 if verbose > 0: print "different sequence names"; sys.stdout.flush()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
209 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
210
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
211 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
212 if verbose > 0: print "different match numbers"; sys.stdout.flush()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
213 return False