comparison smart_toolShed/commons/core/coord/Match.py @ 0:e0f8dcca02ed

Uploaded S-MART tool. A toolbox manages RNA-Seq and ChIP-Seq data.
author yufei-luo
date Thu, 17 Jan 2013 10:52:14 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e0f8dcca02ed
1 # Copyright INRA (Institut National de la Recherche Agronomique)
2 # http://www.inra.fr
3 # http://urgi.versailles.inra.fr
4 #
5 # This software is governed by the CeCILL license under French law and
6 # abiding by the rules of distribution of free software. You can use,
7 # modify and/ or redistribute the software under the terms of the CeCILL
8 # license as circulated by CEA, CNRS and INRIA at the following URL
9 # "http://www.cecill.info".
10 #
11 # As a counterpart to the access to the source code and rights to copy,
12 # modify and redistribute granted by the license, users are provided only
13 # with a limited warranty and the software's author, the holder of the
14 # economic rights, and the successive licensors have only limited
15 # liability.
16 #
17 # In this respect, the user's attention is drawn to the risks associated
18 # with loading, using, modifying and/or developing or reproducing the
19 # software by the user in light of its specific status of free software,
20 # that may mean that it is complicated to manipulate, and that also
21 # therefore means that it is reserved for developers and experienced
22 # professionals having in-depth computer knowledge. Users are therefore
23 # encouraged to load and test the software's suitability as regards their
24 # requirements in conditions enabling the security of their systems and/or
25 # data to be ensured and, more generally, to use and operate it in the
26 # same conditions as regards security.
27 #
28 # The fact that you are presently reading this means that you have had
29 # knowledge of the CeCILL license and that you accept its terms.
30
31
32 import sys
33 from commons.core.coord.Range import Range
34 from commons.core.coord.Path import Path
35
36
37 ## Handle a chain of match(es) between two sequences, query and subject, with an identifier and the length of the input sequences
38 #
39 class Match( Path ):
40
41 ## Constructor
42 #
43 def __init__(self):
44 Path.__init__(self)
45 self.query_length = -1
46 self.query_length_perc = -1 # length of the match on the query / length of the query
47 self.query_seqlength = -1
48 self.match_length_perc = -1 # length of the match on the query / total length of the subject
49 self.subject_length = -1
50 self.subject_length_perc = -1 # length of the match on the subject / length of the subject
51 self.subject_seqlength = -1
52
53 ## Equal operator
54 #
55 def __eq__(self, o):
56 if o == None \
57 or self.query_length != o.query_length or self.query_length_perc != o.query_length_perc\
58 or self.query_seqlength != o.query_seqlength or self.subject_length != o.subject_length\
59 or self.subject_length_perc != o.subject_length_perc or self.subject_seqlength != o.subject_seqlength\
60 or self.match_length_perc != o.match_length_perc:
61 return False
62 return Path.__eq__(self, o)
63
64 ## Return the length of the match on the query divided by the total length of the query
65 #
66 def getLengthPercOnQuery(self):
67 return self.query_length_perc
68
69 ## Return the length of the match on the subject divided by the total length of the subject
70 #
71 def getLengthPercOnSubject(self):
72 return self.subject_length_perc
73
74 ## Return the length of the match on the subject
75 #
76 def getLengthMatchOnSubject(self):
77 return self.subject_length
78
79 ## Set attributes from a tuple
80 #
81 # @param tuple: a tuple with (query name,query start,query end,
82 # query length, query length perc (between 0-1), match length perc (between 0-1), subject name,
83 # subject start,subject end,subject length, subject length percentage (between 0-1), e_value,score,identity,id)
84 #
85 def setFromTuple( self, tuple ):
86 queryStart = int(tuple[1])
87 queryEnd = int(tuple[2])
88 subjectStart = int(tuple[7])
89 subjectEnd = int(tuple[8])
90 if queryStart < queryEnd:
91 self.range_query = Range(tuple[0],queryStart,queryEnd)
92 self.range_subject = Range(tuple[6],subjectStart,subjectEnd)
93 else:
94 self.range_query = Range(tuple[0],queryEnd,queryStart)
95 self.range_subject = Range(tuple[6],subjectEnd,subjectStart)
96 self.query_length = int(tuple[3])
97 self.query_length_perc = float(tuple[4])
98 self.query_seqlength = int( self.query_length / self.query_length_perc )
99 self.match_length_perc = float(tuple[5])
100 self.subject_length = int(tuple[9])
101 self.subject_length_perc = float(tuple[10])
102 self.subject_seqlength = int( self.subject_length / self.subject_length_perc )
103 self.e_value = float(tuple[11])
104 self.score = float(tuple[12])
105 self.identity = float(tuple[13])
106 self.id = int(tuple[14])
107
108 ## Reset
109 #
110 def reset( self ):
111 Path.reset( self )
112 self.query_length = -1
113 self.query_length_perc = -1
114 self.query_seqlength = -1
115 self.match_length_perc = -1
116 self.subject_length = -1
117 self.subject_length_perc = -1
118 self.subject_seqlength = -1
119
120 ## Return a formated string of the attribute data
121 #
122 def toString( self ):
123 string = "%s" % ( self.range_query.toString() )
124 string += "\t%i\t%f" % ( self.query_length,
125 self.query_length_perc )
126 string += "\t%f" % ( self.match_length_perc )
127 string += "\t%s" % ( self.range_subject.toString() )
128 string += "\t%i\t%f" % ( self.subject_length,
129 self.subject_length_perc )
130 string += "\t%g\t%i\t%f" % ( self.e_value,
131 self.score,
132 self.identity )
133 string += "\t%i" % ( self.id )
134 return string
135
136 ## Return a Path instance
137 #
138 def getPathInstance( self ):
139 p = Path()
140 tuple = ( self.id,
141 self.range_query.seqname,
142 self.range_query.start,
143 self.range_query.end,
144 self.range_subject.seqname,
145 self.range_subject.start,
146 self.range_subject.end,
147 self.e_value,
148 self.score,
149 self.identity )
150 p.setFromTuple( tuple )
151 return p
152
153 ## Give information about a match whose query is included in the subject
154 #
155 # @return string
156 #
157 def getQryIsIncluded( self ):
158 string = "query %s (%d bp: %d-%d) is contained in subject %s (%d bp: %d-%d): id=%.2f - %.3f - %.3f - %.3f" %\
159 ( self.range_query.seqname, self.query_seqlength, self.range_query.start, self.range_query.end,
160 self.range_subject.seqname, self.subject_seqlength, self.range_subject.start, self.range_subject.end,
161 self.identity, self.query_length_perc, self.match_length_perc, self.subject_length_perc )
162 return string
163
164 def increaseLengthPercOnQuery(self, coverage):
165 self.query_length_perc += coverage
166
167 ## Compare the object with another match and see if they are equal
168 # (same identity, E-value and score + same subsequences whether in query or subject)
169 #
170 # @return True if objects are equals False otherwise
171 #
172 def isDoublonWith( self, match, verbose=0 ):
173
174 # if both matches have same identity, score and E-value
175 if self.identity == match.identity and self.score == match.score and self.e_value == match.e_value:
176
177 # if query and subject are identical
178 if ( self.range_query.seqname == match.range_query.seqname \
179 and self.range_subject.seqname == match.range_subject.seqname ):
180
181 # if the coordinates are equal
182 if self.range_query.__eq__( match.range_query ) and self.range_subject.__eq__( match.range_subject ):
183 return True
184
185 else:
186 if verbose > 0: print "different coordinates"; sys.stdout.flush()
187 return False
188
189 # if query and subject are reversed but identical
190 elif self.range_query.seqname == match.range_subject.seqname and self.range_subject.seqname == match.range_query.seqname:
191
192 # if the coordinates are equal
193 if self.range_query.__eq__( match.range_subject ) and self.range_subject.__eq__( match.range_query ):
194 return True
195
196 else:
197 if verbose > 0: print "different coordinates"; sys.stdout.flush()
198 return False
199
200 else:
201 if verbose > 0: print "different sequence names"; sys.stdout.flush()
202 return False
203
204 else:
205 if verbose > 0: print "different match numbers"; sys.stdout.flush()
206 return False