annotate TEisotools-1.1.a/commons/core/seq/SequenceModifications.py @ 15:255c852351c5 draft

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