annotate TEisotools-1.0/commons/core/coord/Path.py @ 6:20ec0d14798e draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 05:00:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
4 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
10 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
15 # liability.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
16 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
27 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
30
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
31
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32 from commons.core.coord.Set import Set
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 from commons.core.coord.Align import Align
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.coord.Range import Range
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37 ## Handle a match between two sequences, query and subject (pair of coordinates with E-value, score and identity) with an identifier
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 class Path( Align ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 __slots__ = ("id")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 ## Constructor
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 # @param id identifier
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 # @param range_q: a Range instance for the query
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 # @param range_s: a Range instance for the subject
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 # @param e_value: E-value of the match
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 # @param score: score of the match
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 # @param identity: identity percentage of the match
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 def __init__( self, id=-1, range_q=Range(), range_s=Range(), e_value=0, score=0, identity=0 ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 self.id = int( id )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 Align.__init__( self, range_q, range_s, e_value, score, identity )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 ## Equal operator
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 def __eq__(self, o):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 if type(o) is not type(self) or self.id != o.id:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 return False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 return Align.__eq__(self, o)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 ## Not equal operator
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65 def __ne__(self, o):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 return not self.__eq__(o)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 ## repr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 def __repr__(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 return self.toString()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 ## Set attributes from tuple
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 # @param tuple a tuple with (id,queryName,queryStart,queryEnd,subjectName,subjectStar,subjectEnd,E-value,score,identity)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 # @note data are loaded such that the query is always on the direct strand
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 def setFromTuple(self, tuple):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 self.id = int(tuple[0])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 Align.setFromTuple(self, tuple[1:])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 ## Reset
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 def reset(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 self.id = -1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 Align.reset(self)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 ## Return the attributes as a formatted string
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 def toString(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 string = "%i" % ( self.id )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 string += "\t%s" % (Align.toString(self))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 return string
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 ## Return the identifier of the Path instance
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 def getIdentifier( self ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 return self.id
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 ## Return a Set instance with the subject mapped on the query
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 def getSubjectAsSetOfQuery(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 iSet = Set()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 iSet.id = self.id
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 iSet.name = self.range_subject.seqname
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 iSet.seqname = self.range_query.seqname
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 if self.range_subject.isOnDirectStrand():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 iSet.start = self.range_query.start
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 iSet.end = self.range_query.end
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 iSet.start = self.range_query.end
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 iSet.end = self.range_query.start
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 return iSet
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 #TODO: add tests !!!!
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 #WARNING: subject always in direct strand !!!
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 ## Return a Set instance with the subject mapped on the query
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 def getQuerySetOfSubject(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 iSet = Set()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 iSet.id = self.id
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 iSet.name = self.range_query.seqname
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 iSet.seqname = self.range_subject.seqname
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 if self.range_subject.isOnDirectStrand():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 iSet.start = self.range_subject.start
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 iSet.end = self.range_subject.end
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 iSet.start = self.range_subject.end
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 iSet.end = self.range_subject.start
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 return iSet
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 ## Return True if the instance can be merged with another Path instance, False otherwise
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 # @param o a Path instance
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137 def canMerge(self, o):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138 return o.id != self.id \
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 and o.range_query.seqname == self.range_query.seqname \
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140 and o.range_subject.seqname == self.range_subject.seqname \
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 and o.range_query.isOnDirectStrand() == self.range_query.isOnDirectStrand() \
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 and o.range_subject.isOnDirectStrand() == self.range_subject.isOnDirectStrand() \
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 and o.range_query.isOverlapping(self.range_query) \
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144 and o.range_subject.isOverlapping(self.range_subject)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146 ## Return an Align instance with the same attributes, except the identifier
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 def getAlignInstance(self):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 iAlign = Align()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150 lAttributes = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 lAttributes.append( self.range_query.seqname )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 lAttributes.append( self.range_query.start )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 lAttributes.append( self.range_query.end )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154 lAttributes.append( self.range_subject.seqname )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155 lAttributes.append( self.range_subject.start )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156 lAttributes.append( self.range_subject.end )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 lAttributes.append( self.e_value )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 lAttributes.append( self.score )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159 lAttributes.append( self.identity )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 iAlign.setFromTuple( lAttributes )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 return iAlign