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