annotate TEisotools-1.1.a/commons/core/seq/SequenceModificationsCollection.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 import os
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 import time
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35 import shutil
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36 from commons.core.seq.BioseqDB import BioseqDB
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37 from commons.core.seq.SequenceModifications import SequenceModifications
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38 from commons.core.checker.RepetException import RepetException
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40 class SequenceModificationsCollection(object):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42 def __init__(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43 self._lSeqModif = []
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 = ""
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48 result += "%s\n" % iSeqModif.__str__()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 return result
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 def __eq__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 if type(o) is type(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 self.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 o.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 return self._lSeqModif == o._lSeqModif
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56 return False
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 def __ne__(self, o):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 return not self.__eq__(o)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 def clear(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 self._lSeqModif = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 def add(self, iSeqModif, override = False):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65 for seqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 if seqModif.getOriginalHeader() == iSeqModif.getOriginalHeader():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 if override:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 self._lSeqModif.pop(self._lSeqModif.index(seqModif))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70 raise RepetException("ERROR: '%s' already in SequenceModificationsCollection" % iSeqModif.getOriginalHeader())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 self._lSeqModif.append(iSeqModif)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 def get(self, header, mutated = False):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 if mutated:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 linkToGoodMethod = iSeqModif.getMutatedHeader
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 linkToGoodMethod = iSeqModif.getOriginalHeader
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 if linkToGoodMethod() == header:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 return iSeqModif
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83 return None
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85 def getHeadersList(self, mutated = False):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 lHeaders = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 if mutated:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 lHeaders.append(iSeqModif.getMutatedHeader())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 lHeaders.append(iSeqModif.getOriginalHeader())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 lHeaders.sort(key = lambda header: header.lower())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 return lHeaders
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 def sort(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 self._lSeqModif.sort(key = lambda seqMod: seqMod.getOriginalHeader().lower(), reverse = False)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 def writeMutations(self, fileName, outFormat = ""):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 self.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 with open(fileName, "w") as fH:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 if outFormat.lower() in ["gff", "gff3"]:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 fH.write("##gff-version 3\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 for mutation in iSeqModif.getMutations():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 pos = mutation[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107 old = mutation[1]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 new = mutation[2]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 fH.write("%s\tMutateSequence\tSNP\t%i\t%i\t.\t.\t.\tName=SNP_%i;REF=%s;ALT=%s\n" % (iSeqModif.getOriginalHeader(), pos, pos, pos, old, new))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 fH.write("#Mutations:\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 fH.write("seqName\tposition\toldNt\tnewNt\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 for mutation in iSeqModif.getMutations():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 fH.write("%s\t%i\t%s\t%s\n" % (iSeqModif.getOriginalHeader(), mutation[0], mutation[1], mutation[2]))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117 def writeInsertions(self, fileName, outFormat = ""):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118 self.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 with open(fileName, "w") as fH:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120 if outFormat.lower() in ["gff", "gff3"]:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 fH.write("##gff-version 3\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 for iRange in iSeqModif.getInsertions():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 if iRange.getSeqname() != ".":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 fH.write("%s\tMutateSequence\tinsertion\t%s\t%s\t.\t.\t.\tName=insertion_%s-%s;insert=%s\n" % (iSeqModif.getOriginalHeader(), iRange.getStart(), iRange.getEnd(), iRange.getStart(), iRange.getEnd(), iRange.getSeqname()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127 fH.write("%s\tMutateSequence\tinsertion\t%s\t%s\t.\t.\t.\tName=insertion_%s-%s\n" % (iSeqModif.getOriginalHeader(), iRange.getStart(), iRange.getEnd(), iRange.getStart(), iRange.getEnd()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 fH.write("#Insertions:\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 fH.write("seqName\tstart\tend\tinsertedSeqName\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132 for iRange in iSeqModif.getInsertions():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133 fH.write("%s\t%i\t%i\t%s\n" % (iSeqModif.getOriginalHeader(), iRange.getStart(), iRange.getEnd(), iRange.getSeqname()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135 def writeDeletions(self, fileName, outFormat = ""):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136 self.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137 with open(fileName, "w") as fH:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138 if outFormat.lower() in ["gff", "gff3"]:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 fH.write("##gff-version 3\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 for iRange in iSeqModif.getDeletions():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142 fH.write("%s\tMutateSequence\tdeletion\t%s\t%s\t.\t.\t.\tName=deletion_%s-%s\n" % (iSeqModif.getOriginalHeader(), iRange.getStart(), iRange.getEnd(), iRange.getStart(), iRange.getEnd()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144 fH.write("#Deletions:\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145 fH.write("seqName\tstart\tend\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147 for iRange in iSeqModif.getDeletions():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 fH.write("%s\t%i\t%i\n" % (iSeqModif.getOriginalHeader(), iRange.getStart(), iRange.getEnd()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
150 def write(self, mutationsFileName = "", insertionsFileName = "", deletionsFileName = "", outFormat = ""):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
151 self.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
152 self.writeMutations(mutationsFileName, outFormat)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
153 self.writeInsertions(insertionsFileName, outFormat)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
154 self.writeDeletions(deletionsFileName, outFormat)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
155
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
156 def writeVCF(self, VCFFileName, fastaFileName, software = "MutateSequences"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
157 self.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
158 tmpVCFFileName = "%s.tmp" % VCFFileName
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
159 VCFFH = open(tmpVCFFileName, "w")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
160 VCFFH.write("##fileformat=VCFv4.1\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
161 VCFFH.write("##fileDate=%s\n" % time.strftime("%Y%m%d"))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
162 VCFFH.write("##reference=%s\n" % os.path.abspath(fastaFileName))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
163 VCFFH.write("##INFO=<ID=SVLEN,Number=.,Type=Integer,Description=\"Difference in length between REF and ALT alleles\">\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
164 VCFFH.write("##INFO=<ID=SVTYPE,Number=1,Type=String,Description=\"Type of structural variant\">\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
165 VCFFH.write("##INFO=<ID=ALTSTART,Number=1,Type=Integer,Description=\"ALT start position on query sequence\">\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
166 VCFFH.write("##INFO=<ID=SOFTWARE,Number=1,Type=String,Description=\"Software used to generate this VCF\">\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
167 VCFFH.write("##INFO=<ID=INSERTED,Number=1,Type=String,Description=\"Inserted sequence name\">\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
168 VCFFH.write("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\n")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
169
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
170 iBSDB = BioseqDB(fastaFileName)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
171
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
172 for iSeqModif in self._lSeqModif:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
173 for mutation in iSeqModif.getMutations():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
174 pos = mutation[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
175 old = mutation[1]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
176 new = mutation[2]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
177 VCFFH.write("%s\t%s\t.\t%s\t%s\t.\t.\tAN=2;REF=%s;ALT=%s;SOFTWARE=%s\n" % (iSeqModif.getOriginalHeader(), pos, old, new, old, new, software))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
178
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
179 for insRange in iSeqModif.getInsertions():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
180 if insRange.getStart() != 1:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
181 refSeq = iBSDB.fetch(iSeqModif.getOriginalHeader()).getNtFromPosition(insRange.getStart() - 1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
182 altSeq = "."
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
183
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
184 INFO = "SVTYPE=INS;AN=2;SVLEN=%d;SOFTWARE=%s" % (insRange.getEnd() - insRange.getStart() + 1, software)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
185 if insRange.getSeqname() != ".":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
186 INFO += ";INSERTED=%s" % insRange.getSeqname()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
187 VCFLine = "%s\t%d\t.\t%s\t%s\t%s\t%s\t%s\n" % (iSeqModif.getOriginalHeader(), insRange.getStart() - 1, refSeq, altSeq, ".", ".", INFO)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
188
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
189 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
190 refSeq = iBSDB.fetch(iSeqModif.getOriginalHeader()).getNtFromPosition(insRange.getStart())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
191 refSeq = "."
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
192 altSeq = "."
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
193
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
194 INFO = "SVTYPE=INS;AN=2;SVLEN=%d;SOFTWARE=%s" % (insRange.getEnd() - insRange.getStart() + 1, software)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
195 if insRange.getSeqname() != ".":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
196 INFO += ";INSERTED=%s" % insRange.getSeqname()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
197 VCFLine = "%s\t%d\t.\t%s\t%s\t%s\t%s\t%s\n" % (iSeqModif.getOriginalHeader(), insRange.getStart(), refSeq, altSeq, ".", ".", INFO)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
198
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
199 VCFFH.write(VCFLine)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
200
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
201 for delRange in iSeqModif.getDeletions():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
202 if delRange.getStart() != 1:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
203 refSeq = iBSDB.fetch(iSeqModif.getOriginalHeader()).subseq(delRange.getStart() - 1, delRange.getEnd()).getSequence()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
204 altSeq = refSeq[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
205
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
206 INFO = "SVTYPE=DEL;AN=2;SVLEN=-%d;SOFTWARE=%s" % (len(refSeq)-1, software)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
207 VCFLine = "%s\t%d\t.\t%s\t%s\t%s\t%s\t%s\n" % (iSeqModif.getOriginalHeader(), delRange.getStart() - 1, refSeq, altSeq, ".", ".", INFO)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
208
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
209 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
210 refSeq = iBSDB.fetch(iSeqModif.getOriginalHeader()).subseq(delRange.getStart(), delRange.getEnd() + 1).getSequence()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
211 altSeq = refSeq[-1]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
212 altSeq = "."
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
213
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
214 INFO = "SVTYPE=DEL;AN=2;SVLEN=-%d;SOFTWARE=%s" % (len(refSeq)-1, software)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
215 VCFLine = "%s\t%d\t.\t%s\t%s\t%s\t%s\t%s\n" % (iSeqModif.getOriginalHeader(), delRange.getStart(), refSeq, altSeq, ".", ".", INFO)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
216
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
217 VCFFH.write(VCFLine)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
218
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
219 #This command line can sort this VCF file properly. But can't manage to launch it properly through os.system or subprocess...
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
220 # cmd = "(head -n 9 %s && tail -n +10 %s | head -n -1 | sort -f -k1,1 -k2,2n) > %s" % (tmpVCFFileName, tmpVCFFileName, VCFFileName)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
221 shutil.move(tmpVCFFileName, VCFFileName)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
222
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
223 def getCollectionBasedOnMutatedSequence(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
224 transformedSeqModifCollec = SequenceModificationsCollection()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
225
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
226 for header in self.getHeadersList():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
227 currentSeqModif = self.get(header)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
228
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
229 lModifsTuples = [("insertion", iRange) for iRange in currentSeqModif.getInsertions()]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
230 for iRange in currentSeqModif.getDeletions():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
231 lModifsTuples.append(("deletion", iRange))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
232 lModifsTuples.sort(key = lambda modifTuple: modifTuple[1].getStart(), reverse = False)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
233
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
234 sumIns = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
235 sumDel = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
236
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
237 iseqModif = SequenceModifications(currentSeqModif.getMutatedHeader(), currentSeqModif.getOriginalHeader())
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
238 for modifTuple in lModifsTuples:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
239 varType = modifTuple[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
240 varRange = modifTuple[1]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
241
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
242 if varType == "insertion":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
243 iseqModif.addDeletion(varRange.getStart() + sumIns - sumDel, varRange.getEnd() + sumIns - sumDel)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
244 sumIns += varRange.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
245
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
246 if varType == "deletion":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
247 iseqModif.addInsertion(varRange.getStart() + sumIns - sumDel, varRange.getEnd() + sumIns - sumDel)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
248 sumDel += varRange.getLength()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
249
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
250 for tSnp in currentSeqModif.getMutations():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
251 iseqModif.addMutation((tSnp[0], tSnp[2], tSnp[1]))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
252
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
253 iseqModif.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
254 transformedSeqModifCollec.add(iseqModif)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
255
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
256 transformedSeqModifCollec.sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
257
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
258 return transformedSeqModifCollec
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
259
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
260 def loadSeqModifCollectionFromFiles(self, inInsertionsFileName, inDeletionsFileName, inSNPsFileName, SNPsrate = "0.020000"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
261 self.clear()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
262
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
263 with open(inInsertionsFileName, "r") as f:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
264 line = f.readline()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
265 while line:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
266 if "seqName" not in line and "#" not in line:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
267 splittedLine = line.split()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
268 seqname = splittedLine[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
269 start = int(splittedLine[1])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
270 end = int(splittedLine[2])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
271 insertedSeqName = splittedLine[3]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
272
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
273 if self.get(seqname) is None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
274 self.add(SequenceModifications(seqname))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
275 self.get(seqname).setMutatedHeader("%s_mutated_%s" % (seqname, SNPsrate))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
276 self.get(seqname).addInsertion(start, end, insertedSeqName)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
277 line = f.readline()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
278
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
279 with open(inDeletionsFileName, "r") as f:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
280 line = f.readline()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
281 while line:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
282 if "seqName" not in line and "#" not in line:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
283 splittedLine = line.split()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
284 seqname = splittedLine[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
285 start = int(splittedLine[1])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
286 end = int(splittedLine[2])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
287
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
288 if self.get(seqname) is None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
289 self.add(SequenceModifications(seqname))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
290 self.get(seqname).setMutatedHeader("%s_mutated_%s" % (seqname, SNPsrate))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
291 self.get(seqname).addDeletion(start, end)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
292 line = f.readline()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
293
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
294 with open(inSNPsFileName, "r") as f:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
295 line = f.readline()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
296 while line:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
297 if "seqName" not in line and "#" not in line:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
298 splittedLine = line.split()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
299 seqname = splittedLine[0]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
300 position = int(splittedLine[1])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
301 oldNt = splittedLine[2]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
302 newNt = splittedLine[3]
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
303
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
304 if self.get(seqname) is None:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
305 self.add(SequenceModifications(seqname))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
306 self.get(seqname).setMutatedHeader("%s_mutated_%s" % (seqname, SNPsrate))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
307 self.get(seqname).addMutation((position, oldNt, newNt))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
308 line = f.readline()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
309
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
310 for header in self.getHeadersList():
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
311 self.get(header).sort()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
312 self.sort()