Mercurial > repos > urgi-team > teiso
comparison TEisotools-1.0/commons/core/coord/MergedPath.py @ 6:20ec0d14798e draft
Uploaded
author | urgi-team |
---|---|
date | Wed, 20 Jul 2016 05:00:24 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:4093a2fb58be | 6:20ec0d14798e |
---|---|
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 import collections | |
32 from commons.core.coord.Path import Path | |
33 from commons.core.LoggerFactory import LoggerFactory | |
34 | |
35 LOG_DEPTH = "repet.commons.core.coord" | |
36 | |
37 ## Merge Path objects with the same ID in a single object | |
38 # | |
39 class MergedPath(object): | |
40 | |
41 __slots__ = ("_lPaths", "_length", "_ID", "_nbPaths", "_log") | |
42 | |
43 ## Constructor | |
44 # | |
45 def __init__(self, lPaths = None, verbosity = 0): | |
46 self._log = LoggerFactory.createLogger("%s.%s" % (LOG_DEPTH, self.__class__.__name__), verbosity = verbosity) | |
47 self._lPaths = [] | |
48 self._length = 0 | |
49 self._ID = None | |
50 self._nbPaths = 0 | |
51 | |
52 if lPaths: | |
53 for p in lPaths: | |
54 self.add(p) | |
55 | |
56 ## repr | |
57 # | |
58 def __repr__(self): | |
59 return self.toString() | |
60 | |
61 ## Equal operator | |
62 # | |
63 def __eq__(self, o): | |
64 equal = True | |
65 if type(o) is not type(self): | |
66 equal = False | |
67 elif self.getID() != o.getID() or self.getLength() != o.getLength() or self._nbPaths != o._nbPaths: | |
68 equal = False | |
69 else: | |
70 equal = collections.Counter(self.getPathsList()) == collections.Counter(self.getPathsList()) | |
71 return equal | |
72 | |
73 ## Not equal operator | |
74 # | |
75 def __ne__(self, o): | |
76 return not self.__eq__(o) | |
77 | |
78 ## Try to add a Path instance to this MergedPath instance | |
79 # | |
80 # @param path a Path instance | |
81 # | |
82 def add(self, path): | |
83 if not isinstance(path, Path): | |
84 msg = "Error when adding to MergedPath : Not a Path" | |
85 self._log.warning(msg) | |
86 elif self._ID and self._ID != path.getIdentifier(): | |
87 msg = "Couldn't add Path (%i) to MergedPath (%i) : Different IDs" % (path.getIdentifier(), self._ID) | |
88 self._log.warning(msg) | |
89 else: | |
90 self._ID = path.getIdentifier() | |
91 self._lPaths.append(path) | |
92 self._length += path.getAlignInstance().getLengthOnQuery() | |
93 self._nbPaths = len(self._lPaths) | |
94 | |
95 ## Get the total length of the Paths in this MergedPath instance | |
96 # | |
97 def getLength(self): | |
98 return self._length | |
99 | |
100 ## Get the list of Paths objects in this MergedPath instance | |
101 # | |
102 def getPathsList(self): | |
103 return self._lPaths | |
104 | |
105 ## Get the ID of the Path | |
106 # | |
107 def getID(self): | |
108 return self._ID | |
109 | |
110 ## Get the number of paths (fragments) | |
111 # | |
112 def getNbPaths(self): | |
113 return self._nbPaths | |
114 | |
115 ## Get the weighted identity | |
116 # | |
117 def getWeightedIdentity(self): | |
118 res = 0 | |
119 if self._nbPaths > 0: | |
120 sumIdentity = sum((abs(path.range_query.end - path.range_query.start) + 1) * path.identity for path in self._lPaths) | |
121 sumSizes = sum(abs(path.range_query.end - path.range_query.start) + 1 for path in self._lPaths) | |
122 res = sumIdentity / sumSizes | |
123 res = float("{0:.2f}".format(res)) # Truncate to 2 decimals | |
124 return res | |
125 | |
126 ## Return the attributes as a formatted string | |
127 # | |
128 def toString(self): | |
129 string = "Empty MergedPath()" | |
130 if self._ID: | |
131 string = "ID: %i" % self._ID | |
132 string += "\tLength: %i" % self._length | |
133 string += "\tNbPaths: %i" % self._nbPaths | |
134 string += "\tPaths:\n" | |
135 string += "\n".join([p.toString() for p in self._lPaths]) | |
136 return string |