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