annotate commons/core/parsing/VarscanFile.py @ 64:783e6ed4eb66

Minor bug correction. Added casts to str in Galaxy XML files. Also closed the writer in the Python script "changeTagName."
author m-zytnicki
date Mon, 19 Oct 2015 14:16:44 +0200
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from commons.core.parsing.VarscanHit import VarscanHit
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 from commons.core.parsing.VarscanHit_WithTag import VarscanHit_WithTag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.parsing.VarscanHit_v2_2_8 import VarscanHit_v2_2_8
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.checker.CheckerException import CheckerException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 from commons.core.parsing.VarscanHit_v2_2_8_WithTag import VarscanHit_v2_2_8_WithTag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 class VarscanFile(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 def __init__(self, varscanFileName = ""):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 self._varscanFileName = varscanFileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 self._varscanFieldSeparator = "\t"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self._lVarscanHits = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 self._typeOfVarscanFile = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 def __eq__(self, o):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 return self._varscanFieldSeparator == o._varscanFieldSeparator and self._lVarscanHits == o._lVarscanHits and self._varscanFileName == o._varscanFileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 def setVarscanHitsList(self, lVarscanHits):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 self._lVarscanHits = lVarscanHits
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 def setHeaderVarcanFile(self, headerVarcanFile):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self._headerVarcanFile = headerVarcanFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 def setTypeOfVarscanFile(self, type):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
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":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self._typeOfVarscanFile = type
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 self._typeOfVarscanFile = ""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 def getVarscanHitsList(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 return self._lVarscanHits
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 def getHeaderVarcanFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 return self._headerVarcanFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 def getListOfVarscanHits(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 return self._lVarscanHits
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 def getTypeOfVarscanFile(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 return self._typeOfVarscanFile
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 def parse(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 varscanFile = open(self._varscanFileName, "r")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 currentLineNumber = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 line = varscanFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 if "Chrom\tPosition" in line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 self.setHeaderVarcanFile(line)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 line = varscanFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 while line != "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 if not "Chrom\tPosition" in line:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 currentLineNumber += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 line = line.strip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 lResults = line.split(self._varscanFieldSeparator)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 if len(lResults) == 12:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 currentVarscanLine = self.createVarscanHit(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 self._typeOfVarscanFile = "Varscan_2_2"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 elif len(lResults) == 13:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 currentVarscanLine = self.createVarscanHitWithTag(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 self._typeOfVarscanFile = "Varscan_2_2_WithTag"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 elif len(lResults) == 19:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 currentVarscanLine = self.createVarscanHit_v2_2_8(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 self._typeOfVarscanFile = "Varscan_2_2_8"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 elif len(lResults) == 20:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 currentVarscanLine = self.createVarscanHit_v2_2_8_WithTag(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 self._typeOfVarscanFile = "Varscan_2_2_8_WithTag"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 raise CheckerException ("Warning: this line (l.%s) is not a valid varscan line !" % currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 self._lVarscanHits.append(currentVarscanLine)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 line = varscanFile.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 varscanFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 def createVarscanObjectFromLine(self, line, currentLineNumber):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 if self._typeOfVarscanFile == "Varscan_2_2":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 VarscanHit = self.createVarscanHit(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 return VarscanHit
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 elif self._typeOfVarscanFile == "Varscan_2_2_WithTag":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 return self.createVarscanHitWithTag(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 elif self._typeOfVarscanFile == "Varscan_2_2_8":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 return self.createVarscanHit_v2_2_8(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 elif self._typeOfVarscanFile == "Varscan_2_2_8_WithTag":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 return self.createVarscanHit_v2_2_8_WithTag(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 def createVarscanHit(self, line, currentLineNumber):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 iVarscanHit = VarscanHit()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 iVarscanHit.setAttributesFromString(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 return iVarscanHit
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 def createVarscanHitWithTag(self, line, currentLineNumber):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 iVarscanHitWithTag = VarscanHit_WithTag()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 iVarscanHitWithTag.setAttributesFromString(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 return iVarscanHitWithTag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 def createVarscanHit_v2_2_8(self, line, currentLineNumber):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 iVarscanHit = VarscanHit_v2_2_8()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 iVarscanHit.setAttributesFromString(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 return iVarscanHit
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 def createVarscanHit_v2_2_8_WithTag(self, line, currentLineNumber):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 iVarscanHitWithTag = VarscanHit_v2_2_8_WithTag()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 iVarscanHitWithTag.setAttributesFromString(line, currentLineNumber)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 return iVarscanHitWithTag
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 def selectTypeOfVarscanHitObject(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 if self._typeOfVarscanFile == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 raise CheckerException ("Error: no varscan object found !")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 elif self._typeOfVarscanFile == "Varscan_2_2":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 return VarscanHit()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 elif self._typeOfVarscanFile == "Varscan_2_2_WithTag":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 return VarscanHit_WithTag()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 elif self._typeOfVarscanFile == "Varscan_2_2_8":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 return VarscanHit_v2_2_8()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 elif self._typeOfVarscanFile == "Varscan_2_2_8_WithTag":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 return VarscanHit_v2_2_8_WithTag()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145