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