annotate TEisotools-1.0/commons/core/utils/ClassifUtils.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 # Copyright INRA (Institut National de la Recherche Agronomique)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
4 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
10 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
15 # liability.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
16 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
27 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
30
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
31 import os
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32 import json
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 from collections import OrderedDict
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.tools.RenameHeaderClassif import RenameHeaderClassif
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36 class ClassifUtils(object):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 def _formatProfilesResultsAsDict(lProfilesResults):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40 if len(lProfilesResults) == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41 return OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 dResults = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 for refNameAndCoverage in lProfilesResults:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 refName, coverage = refNameAndCoverage.split(": ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 coverage = coverage.split("%(")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 coverageOnSubject = float(coverage.pop(1).replace("%)", ""))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 coverage = float(coverage.pop(0))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 profilesResult = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 profilesResult["cov"] = coverage
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 profilesResult["covOnSubject"] = coverageOnSubject
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 dResults[refName] = profilesResult
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 return dResults
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 def _formatCodingFeaturesAsDict(lineOfEvidence, dCoding):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 codingEvidences = lineOfEvidence.split("; ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 for codingTypeData in codingEvidences:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 codingTypeData = codingTypeData.split(": ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 codingType = codingTypeData.pop(0)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 codingTypeData = ": ".join(codingTypeData)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 codingTypeData = codingTypeData.split(", ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 if codingType == "TE_BLRtx":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 if not dCoding.has_key("TE_BLRtx"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 dCoding["TE_BLRtx"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 for refNameAndCoverage in codingTypeData:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 blrtxResult = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 refName, coverage = refNameAndCoverage.rsplit(": ", 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 coverage = float(coverage.replace("%", ""))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 blrtxResult["cov"] = coverage
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 dCoding["TE_BLRtx"][refName] = blrtxResult
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 if codingType == "TE_BLRx":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 if not dCoding.has_key("TE_BLRx"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 dCoding["TE_BLRx"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 for refNameAndCoverage in codingTypeData:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 blrxResult = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 refName, coverage = refNameAndCoverage.rsplit(": ", 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 coverage = float(coverage.replace("%", ""))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 blrxResult["cov"] = coverage
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 dCoding["TE_BLRx"][refName] = blrxResult
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 if codingType == "profiles":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 dCoding["profiles"] = ClassifUtils._formatProfilesResultsAsDict(codingTypeData)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 if codingType == "Other_profiles":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 dCoding["Other_profiles"] = ClassifUtils._formatProfilesResultsAsDict(codingTypeData)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 if codingType == "rDNA_BLRn":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 dCoding["rDNA_BLRn"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 codingTypeData = ", ".join(codingTypeData)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 refName, coverage = codingTypeData.rsplit(": ", 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 coverage = float(coverage.replace("%", ""))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 except ValueError:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 refName = codingTypeData
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 coverage = -1.0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 dCoding["rDNA_BLRn"]["name"] = refName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 dCoding["rDNA_BLRn"]["cov"] = coverage
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 if codingType == "HG_BLRn":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 dCoding["HG_BLRn"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 refName, coverage = codingTypeData[0].rsplit(": ", 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 coverage = float(coverage.replace("%", ""))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 dCoding["HG_BLRn"]["name"] = refName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 dCoding["HG_BLRn"]["cov"] = coverage
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 def _formatStructFeaturesAsDict(lineOfEvidence, dStruct):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 structEvidences = lineOfEvidence.split("; ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 for structTypeData in structEvidences:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 structTypeData = structTypeData.split(": ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 structType = structTypeData.pop(0)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 structTypeData = ": ".join(structTypeData)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 structTypeData = structTypeData.split(", ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 if structType == "TElength":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 dStruct["TElength"] = structTypeData.pop()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 if structType == "TermRepeats":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 dStruct["TermRepeats"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 for refNameAndLength in structTypeData:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 refName, length = refNameAndLength.rsplit(": ", 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 dStruct["TermRepeats"][refName] = int(length)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 if structType == "ORF":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137 if not dStruct.has_key("ORF"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138 dStruct["ORF"] = structTypeData
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140 if structType in ["SSR", "SSRtrf"]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 if not dStruct.has_key(structType):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 dStruct[structType] = structTypeData
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144 if "SSRCoverage" in structType :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 dummy, cov = structType.split("=")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146 dStruct["SSRCoverage"] = float(cov)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 if structType == "polyAtail":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 dStruct["polyAtail"] = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 if structType == "helitronExtremities":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 structTypeData = ", ".join(structTypeData)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 structTypeData = structTypeData.split("), ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154 dStruct["helitronExtremities"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155 for helitronData in structTypeData:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156 helName, helData = helitronData.split(": (")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 helData = helData.replace(")", "")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 eValue, start, end = helData.split(", ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 helitronExtResult = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 helitronExtResult["start"] = int(start)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
162 helitronExtResult["end"] = int(end)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
163 helitronExtResult["eValue"] = float(eValue)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
164 dStruct["helitronExtremities"][helName] = helitronExtResult
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
165
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
166 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
167 def _formatOtherFeaturesAsDict(lineOfEvidence, dOther):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
168 if lineOfEvidence != "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
169 ClassifUtils._formatCodingFeaturesAsDict(lineOfEvidence, dOther)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
170 ClassifUtils._formatStructFeaturesAsDict(lineOfEvidence, dOther)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
171
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
172 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
173 def getClassifLineAsDict(line):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
174 dClassif = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
175 iRenameHeaderClassif = RenameHeaderClassif()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
176 lClassifItem = line.split("\t")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
177 if len(lClassifItem) != 8:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
178 msg = "Can't parse line: \"%s\"\n" % line.strip()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
179 print("WARNING - ClassifUtils - %s" % msg)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
180 return dClassif
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
181
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
182 teClass = lClassifItem[4]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
183 teOrder = lClassifItem[5]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
184 # TODO: recompute wicker code like this or force the user to provide a classif file as input with the wicker code already added
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
185 wCode = iRenameHeaderClassif._decisionRuleForWickerCode(teClass, teOrder)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
186
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
187 dClassif["name"] = lClassifItem[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
188 dClassif["wCode"] = wCode
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
189 dClassif["length"] = int(lClassifItem[1])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
190 dClassif["strand"] = lClassifItem[2]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
191 dClassif["chimeric"] = False if lClassifItem[3] == "ok" else True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
192
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
193 dClassif["class"] = teClass
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
194 dClassif["order"] = teOrder
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
195
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
196 if(lClassifItem[6] == "complete"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
197 dClassif["complete"] = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
198 elif(lClassifItem[6] == "incomplete"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
199 dClassif["complete"] = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
200 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
201 dClassif["complete"] = None
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
202
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
203 allFields = lClassifItem[7].split("; ")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
204
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
205 CI = allFields.pop(0)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
206 CI = CI.split("=")[-1]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
207 if CI != "NA":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
208 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
209 CI = int(CI)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
210 except ValueError as e:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
211 print "Couldn't convert %s to int : %s" % (CI, e)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
212 dClassif["CI"] = CI
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
213
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
214 dClassif["coding"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
215 dClassif["struct"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
216 dClassif["other"] = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
217
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
218 allFields = "; ".join(allFields)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
219 codingField = ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
220 structField = ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
221 otherField = ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
222
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
223 codingStart = allFields.find("coding=(")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
224 if codingStart != -1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
225 pCount = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
226 trueStart = codingStart + len("coding=(")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
227 end = trueStart
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
228 for char in allFields[trueStart:]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
229 if char == "(":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
230 pCount += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
231 if char == ")":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
232 pCount -= 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
233 if pCount == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
234 break;
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
235 end += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
236 if pCount == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
237 codingField = allFields[trueStart:end]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
238
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
239 structStart = allFields.find("struct=(")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
240 if structStart != -1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
241 pCount = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
242 trueStart = structStart + len("struct=(")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
243 end = trueStart
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
244 for char in allFields[trueStart:]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
245 if char == "(":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
246 pCount += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
247 if char == ")":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
248 pCount -= 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
249 if pCount == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
250 break;
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
251 end += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
252 structField = allFields[trueStart:end]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
253
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
254 otherStart = allFields.find("other=(")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
255 if otherStart != -1:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
256 pCount = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
257 trueStart = otherStart + len("other=(")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
258 end = trueStart
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
259 for char in allFields[trueStart:]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
260 if char == "(":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
261 pCount += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
262 if char == ")":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
263 pCount -= 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
264 if pCount == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
265 break;
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
266 end += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
267 otherField = allFields[trueStart:end]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
268
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
269 if codingField != "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
270 ClassifUtils._formatCodingFeaturesAsDict(codingField, dClassif["coding"])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
271 if structField != "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
272 ClassifUtils._formatStructFeaturesAsDict(structField, dClassif["struct"])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
273 if otherField != "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
274 ClassifUtils._formatOtherFeaturesAsDict(otherField, dClassif["other"])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
275
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
276 return dClassif
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
277
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
278 ## Retrieve the classification informations of a classif file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
279 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
280 # @param fileName Name of the classif file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
281 # @return A dict containing the classification infos
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
282 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
283 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
284 def getClassifInfosAsDict(fileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
285 dConsensusInfo = OrderedDict()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
286
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
287 ext = os.path.splitext(fileName)[1]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
288 if ext != ".classif":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
289 msg = "Input file must be a classif file from TEdenovo\n"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
290 print("ERROR - ClassifUtils - %s" % msg)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
291 exit(1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
292
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
293 with open(fileName, "r") as classifFile:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
294 for line in classifFile:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
295 seqName = line.split("\t")[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
296 dConsensusInfo[seqName] = ClassifUtils.getClassifLineAsDict(line)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
297
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
298 return dConsensusInfo
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
299
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
300 ## Convert a classif file to JSON format
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
301 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
302 # @param fileName Name of the classif file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
303 # @param outFileName Name of the output JSON file (optional)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
304 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
305 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
306 def convertClassifToJson(fileName, outFileName = ""):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
307 dConsensusInfo = ClassifUtils.getClassifInfosAsDict(fileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
308 if outFileName == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
309 outFileName = "%s_classif.json" % (os.path.basename(fileName).rsplit(".", 1)[0])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
310 with open(outFileName, 'w') as outFile:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
311 json.dump(dConsensusInfo, outFile)