6
|
1 #!/usr/bin/env python
|
|
2
|
|
3 # Copyright INRA (Institut National de la Recherche Agronomique)
|
|
4 # http://www.inra.fr
|
|
5 # http://urgi.versailles.inra.fr
|
|
6 #
|
|
7 # This software is governed by the CeCILL license under French law and
|
|
8 # abiding by the rules of distribution of free software. You can use,
|
|
9 # modify and/ or redistribute the software under the terms of the CeCILL
|
|
10 # license as circulated by CEA, CNRS and INRIA at the following URL
|
|
11 # "http://www.cecill.info".
|
|
12 #
|
|
13 # As a counterpart to the access to the source code and rights to copy,
|
|
14 # modify and redistribute granted by the license, users are provided only
|
|
15 # with a limited warranty and the software's author, the holder of the
|
|
16 # economic rights, and the successive licensors have only limited
|
|
17 # liability.
|
|
18 #
|
|
19 # In this respect, the user's attention is drawn to the risks associated
|
|
20 # with loading, using, modifying and/or developing or reproducing the
|
|
21 # software by the user in light of its specific status of free software,
|
|
22 # that may mean that it is complicated to manipulate, and that also
|
|
23 # therefore means that it is reserved for developers and experienced
|
|
24 # professionals having in-depth computer knowledge. Users are therefore
|
|
25 # encouraged to load and test the software's suitability as regards their
|
|
26 # requirements in conditions enabling the security of their systems and/or
|
|
27 # data to be ensured and, more generally, to use and operate it in the
|
|
28 # same conditions as regards security.
|
|
29 #
|
|
30 # The fact that you are presently reading this means that you have had
|
|
31 # knowledge of the CeCILL license and that you accept its terms.
|
|
32
|
|
33 from operator import itemgetter
|
|
34 from commons.core.coord.Range import Range
|
|
35
|
|
36 class SequenceModifications(object):
|
|
37
|
|
38 def __init__(self, originalHeader = "", mutatedHeader = ""):
|
|
39 self._originalHeader = originalHeader
|
|
40 self._mutatedHeader = mutatedHeader
|
|
41 self._lMutations = []
|
|
42 self._lDeletionsRanges = []
|
|
43 self._lInsertionsRanges = []
|
|
44
|
|
45 def __str__(self):
|
|
46 result = "%s\t%s\n" % (self.getOriginalHeader(), self.getMutatedHeader())
|
|
47 result += "Insertions\n"
|
|
48 for insertion in self._lInsertionsRanges:
|
|
49 result += "%s\n" % insertion.toString()
|
|
50 result += "Deletions\n"
|
|
51 for insertion in self._lDeletionsRanges:
|
|
52 result += "%s\n" % insertion.toString()
|
|
53 result += "Mutations"
|
|
54 for mutation in self._lMutations:
|
|
55 result += "\n%i\t%s\t%s" % (mutation[0], mutation[1], mutation[2])
|
|
56 return result
|
|
57
|
|
58 def __eq__(self, o):
|
|
59 if type(o) is type(self):
|
|
60 self.sort()
|
|
61 o.sort()
|
|
62 return self._originalHeader == o._originalHeader and self._mutatedHeader == o._mutatedHeader and self._lMutations == o._lMutations \
|
|
63 and self._lDeletionsRanges == o._lDeletionsRanges and self._lInsertionsRanges == o._lInsertionsRanges
|
|
64 return False
|
|
65
|
|
66 def __ne__(self, o):
|
|
67 return not self.__eq__(o)
|
|
68
|
|
69 def getOriginalHeader(self):
|
|
70 return self._originalHeader
|
|
71
|
|
72 def getMutatedHeader(self):
|
|
73 return self._mutatedHeader
|
|
74
|
|
75 def getMutations(self):
|
|
76 self.sort()
|
|
77 return self._lMutations
|
|
78
|
|
79 def getInsertions(self):
|
|
80 self.sort()
|
|
81 return self._lInsertionsRanges
|
|
82
|
|
83 def getDeletions(self):
|
|
84 self.sort()
|
|
85 return self._lDeletionsRanges
|
|
86
|
|
87 def setOriginalHeader(self, originalHeader):
|
|
88 self._originalHeader = originalHeader
|
|
89
|
|
90 def setMutatedHeader(self, mutatedHeader):
|
|
91 self._mutatedHeader = mutatedHeader
|
|
92
|
|
93 def setMutations(self, lMutations):
|
|
94 self._lMutations = lMutations
|
|
95
|
|
96 def addMutation(self, tupleMute):
|
|
97 #tuple: (position, oldNT, newNT)
|
|
98 self._lMutations.append(tupleMute)
|
|
99
|
|
100 def addInsertion(self, start, end, insertedSeqName = "."):
|
|
101 self._lInsertionsRanges.append(Range(insertedSeqName, start, end))
|
|
102
|
|
103 def addDeletion(self, start, end):
|
|
104 self._lDeletionsRanges.append(Range(self.getOriginalHeader(), start, end))
|
|
105
|
|
106 def clear(self):
|
|
107 self._lMutations = []
|
|
108 self._lDeletionsRanges = []
|
|
109 self._lInsertionsRanges = []
|
|
110
|
|
111 def sort(self):
|
|
112 self._lMutations.sort(key = itemgetter(0), reverse = False)
|
|
113 self._lDeletionsRanges.sort(key = lambda delRange: delRange.getStart(), reverse = False)
|
|
114 self._lInsertionsRanges.sort(key = lambda insRange: insRange.getStart(), reverse = False) |