annotate commons/core/coord/Match.py @ 16:6135c3075bc5

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