6
|
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
|
|
32 from commons.core.parsing.VarscanHit import VarscanHit
|
|
33 from commons.core.parsing.VarscanHit_WithTag import VarscanHit_WithTag
|
|
34 from commons.core.parsing.VarscanHit_v2_2_8 import VarscanHit_v2_2_8
|
|
35 from commons.core.checker.CheckerException import CheckerException
|
|
36 from commons.core.parsing.VarscanHit_v2_2_8_WithTag import VarscanHit_v2_2_8_WithTag
|
|
37
|
|
38 class VarscanFile(object):
|
|
39
|
|
40 def __init__(self, varscanFileName = ""):
|
|
41 self._varscanFileName = varscanFileName
|
|
42 self._varscanFieldSeparator = "\t"
|
|
43 self._lVarscanHits = []
|
|
44 self._typeOfVarscanFile = ""
|
|
45
|
|
46 def __eq__(self, o):
|
|
47 return self._varscanFieldSeparator == o._varscanFieldSeparator and self._lVarscanHits == o._lVarscanHits and self._varscanFileName == o._varscanFileName
|
|
48
|
|
49 def setVarscanHitsList(self, lVarscanHits):
|
|
50 self._lVarscanHits = lVarscanHits
|
|
51
|
|
52 def setHeaderVarcanFile(self, headerVarcanFile):
|
|
53 self._headerVarcanFile = headerVarcanFile
|
|
54
|
|
55 def setTypeOfVarscanFile(self, type):
|
|
56 if type == "Varscan_2_2" or type == "Varscan_2_2_WithTag" or type == "Varscan_2_2_8" or type == "Varscan_2_2_8_WithTag":
|
|
57 self._typeOfVarscanFile = type
|
|
58 else:
|
|
59 self._typeOfVarscanFile = ""
|
|
60
|
|
61 def getVarscanHitsList(self):
|
|
62 return self._lVarscanHits
|
|
63
|
|
64 def getHeaderVarcanFile(self):
|
|
65 return self._headerVarcanFile
|
|
66
|
|
67 def getListOfVarscanHits(self):
|
|
68 return self._lVarscanHits
|
|
69
|
|
70 def getTypeOfVarscanFile(self):
|
|
71 return self._typeOfVarscanFile
|
|
72
|
|
73 def parse(self):
|
|
74 varscanFile = open(self._varscanFileName, "r")
|
|
75 currentLineNumber = 0
|
|
76 line = varscanFile.readline()
|
|
77 if "Chrom\tPosition" in line:
|
|
78 self.setHeaderVarcanFile(line)
|
|
79 line = varscanFile.readline()
|
|
80 while line != "":
|
|
81 if not "Chrom\tPosition" in line:
|
|
82 currentLineNumber += 1
|
|
83 line = line.strip()
|
|
84 lResults = line.split(self._varscanFieldSeparator)
|
|
85 if len(lResults) == 12:
|
|
86 currentVarscanLine = self.createVarscanHit(line, currentLineNumber)
|
|
87 self._typeOfVarscanFile = "Varscan_2_2"
|
|
88 elif len(lResults) == 13:
|
|
89 currentVarscanLine = self.createVarscanHitWithTag(line, currentLineNumber)
|
|
90 self._typeOfVarscanFile = "Varscan_2_2_WithTag"
|
|
91 elif len(lResults) == 19:
|
|
92 currentVarscanLine = self.createVarscanHit_v2_2_8(line, currentLineNumber)
|
|
93 self._typeOfVarscanFile = "Varscan_2_2_8"
|
|
94 elif len(lResults) == 20:
|
|
95 currentVarscanLine = self.createVarscanHit_v2_2_8_WithTag(line, currentLineNumber)
|
|
96 self._typeOfVarscanFile = "Varscan_2_2_8_WithTag"
|
|
97 else:
|
|
98 raise CheckerException ("Warning: this line (l.%s) is not a valid varscan line !" % currentLineNumber)
|
|
99 self._lVarscanHits.append(currentVarscanLine)
|
|
100 line = varscanFile.readline()
|
|
101 varscanFile.close()
|
|
102
|
|
103 def createVarscanObjectFromLine(self, line, currentLineNumber):
|
|
104 if self._typeOfVarscanFile == "Varscan_2_2":
|
|
105 VarscanHit = self.createVarscanHit(line, currentLineNumber)
|
|
106 return VarscanHit
|
|
107 elif self._typeOfVarscanFile == "Varscan_2_2_WithTag":
|
|
108 return self.createVarscanHitWithTag(line, currentLineNumber)
|
|
109 elif self._typeOfVarscanFile == "Varscan_2_2_8":
|
|
110 return self.createVarscanHit_v2_2_8(line, currentLineNumber)
|
|
111 elif self._typeOfVarscanFile == "Varscan_2_2_8_WithTag":
|
|
112 return self.createVarscanHit_v2_2_8_WithTag(line, currentLineNumber)
|
|
113
|
|
114 def createVarscanHit(self, line, currentLineNumber):
|
|
115 iVarscanHit = VarscanHit()
|
|
116 iVarscanHit.setAttributesFromString(line, currentLineNumber)
|
|
117 return iVarscanHit
|
|
118
|
|
119 def createVarscanHitWithTag(self, line, currentLineNumber):
|
|
120 iVarscanHitWithTag = VarscanHit_WithTag()
|
|
121 iVarscanHitWithTag.setAttributesFromString(line, currentLineNumber)
|
|
122 return iVarscanHitWithTag
|
|
123
|
|
124 def createVarscanHit_v2_2_8(self, line, currentLineNumber):
|
|
125 iVarscanHit = VarscanHit_v2_2_8()
|
|
126 iVarscanHit.setAttributesFromString(line, currentLineNumber)
|
|
127 return iVarscanHit
|
|
128
|
|
129 def createVarscanHit_v2_2_8_WithTag(self, line, currentLineNumber):
|
|
130 iVarscanHitWithTag = VarscanHit_v2_2_8_WithTag()
|
|
131 iVarscanHitWithTag.setAttributesFromString(line, currentLineNumber)
|
|
132 return iVarscanHitWithTag
|
|
133
|
|
134 def selectTypeOfVarscanHitObject(self):
|
|
135 if self._typeOfVarscanFile == "":
|
|
136 raise CheckerException ("Error: no varscan object found !")
|
|
137 elif self._typeOfVarscanFile == "Varscan_2_2":
|
|
138 return VarscanHit()
|
|
139 elif self._typeOfVarscanFile == "Varscan_2_2_WithTag":
|
|
140 return VarscanHit_WithTag()
|
|
141 elif self._typeOfVarscanFile == "Varscan_2_2_8":
|
|
142 return VarscanHit_v2_2_8()
|
|
143 elif self._typeOfVarscanFile == "Varscan_2_2_8_WithTag":
|
|
144 return VarscanHit_v2_2_8_WithTag()
|
|
145 |