annotate TEisotools-1.0/commons/core/coord/MatchUtils.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 sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 import math
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.coord.Match import Match
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.utils.FileUtils import FileUtils
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36 from commons.core.checker.RepetException import RepetException
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 # # Static methods for the manipulation of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40 class MatchUtils (object):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 # # Return a list with Match instances from the given file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44 # @param inFile name of a file in the Match format
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 # @return a list of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 def getMatchListFromFile(inFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 lMatchInstances = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 inFileHandler = open(inFile, "r")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 line = inFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 if line[0:10] == "query.name":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 continue
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 m = Match()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 m.setFromString(line)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 lMatchInstances.append(m)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 return lMatchInstances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 # # Split a Match list in several Match lists according to the subject
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65 # @param lMatches a list of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 # @return a dictionary which keys are subject names and values Match lists
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 def getDictOfListsWithSubjectAsKey(lMatches):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 dSubject2MatchList = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 for iMatch in lMatches:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 if not dSubject2MatchList.has_key(iMatch.range_subject.seqname):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 dSubject2MatchList[ iMatch.range_subject.seqname ] = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74 dSubject2MatchList[ iMatch.range_subject.seqname ].append(iMatch)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 return dSubject2MatchList
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 # # Split a Match list in several Match lists according to the query
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 # @param lMatches a list of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 # @return a dictionary which keys are query names and values Match lists
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 def getDictOfListsWithQueryAsKey (lMatches):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 dQuery2MatchList = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 for iMatch in lMatches:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 if not dQuery2MatchList.has_key(iMatch.range_query.seqname):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 dQuery2MatchList[ iMatch.range_query.seqname ] = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 dQuery2MatchList[ iMatch.range_query.seqname ].append(iMatch)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 return dQuery2MatchList
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 # # Write Match instances contained in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 # @param lMatches a list of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 # @param fileName name of the file to write the Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 # @param mode the open mode of the file ""w"" or ""a""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 def writeListInFile(lMatches, fileName, mode = "w", header = None):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 fileHandler = open(fileName, mode)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 if header:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 fileHandler.write(header)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 for iMatch in lMatches:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 iMatch.write(fileHandler)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 fileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 # # Give path id list from a list of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 # @param lMatch list of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 # @return lId integer list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 def getIdListFromMatchList(lMatch):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 lId = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 for iMatch in lMatch:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115 lId.append(iMatch.id)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 return lId
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 # # Remove duplicated matches in a match list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 # ## replace old PyRepet.MatchDB.rmvDoublons()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 # @param lMatch list of Match instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 # @return lMatchesUniq match unique list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 def rmvDuplicateMatches(lMatch):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 lMatchesUniq = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 for match in lMatch:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 if len(lMatchesUniq) == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 lMatchesUniq.append(match)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 nbDoublons = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 for m in lMatchesUniq:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 if match.isDoublonWith(m):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 nbDoublons += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 if nbDoublons == 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 lMatchesUniq.append(match)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138 for match1 in lMatchesUniq:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 for match2 in lMatchesUniq:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140 if match1.id != match2.id:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 if match1.isDoublonWith(match2):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 raise RepetException ("*** Error: doublon not removed")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 return lMatchesUniq
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 # # Return the list of queries 'included' in subjects when two different databanks are used.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146 # #replace old pyRepet.MatchDB.filterDiffQrySbj()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 # @param iBioseqDB bioseqDB databank of queries
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 # @param thresIdentity float identity threshold
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150 # @param thresLength float length threshold
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 # @param verbose int verbosity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 # @return lMatches match list to keep according to length and identity thresholds
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154 # TODO: don't take into account match for sequence against itself. To do ?
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156 def filterDiffQrySbj(iBioseqDB, matchFile, thresIdentity = 0.95, thresLength = 0.98, verbose = 0):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 if verbose > 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 print "filtering matches (id>=%.2f,qlgth>=%.2f)..." % (thresIdentity, thresLength); sys.stdout.flush()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 thresIdentityPerc = math.floor(thresIdentity * 100)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 lQryToKeep = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
162 dQry2Matches = MatchUtils.getDictOfListsWithQueryAsKey(MatchUtils.getMatchListFromFile(matchFile))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
163
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
164 for seqH in iBioseqDB.idx.keys():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
165 # keep it if it has no match
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
166 if not dQry2Matches.has_key(seqH):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
167 if seqH not in lQryToKeep:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
168 lQryToKeep.append(seqH)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
169 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
170 isConditionsMet = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
171 for match in dQry2Matches[ seqH ]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
172 # check if they are above the thresholds
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
173 if match.identity >= thresIdentityPerc and match.query_length_perc >= thresLength:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
174 isConditionsMet = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
175 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
176 if not isConditionsMet and seqH not in lQryToKeep:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
177 lQryToKeep.append(seqH)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
178 return lQryToKeep
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
179
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
180 # # Count the number of distinct matches involved in at least one match above the thresholds.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
181 # #replace old pyRepet.coord.MatchDB.getNbDistinctSbjWithThres() and pyRepet.coord.MatchDB.getNbDistinctSbjWithThres()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
182 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
183 # @param thresIdentity float identity threshold
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
184 # @param thresLength float length threshold
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
185 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
186 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
187 def getNbDistinctSequencesInsideMatchesWithThresh(lMatches, thresIdentity = 0.95, thresLength = 0.98, whatToCount = "query"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
188 thresIdentityPerc = math.floor(thresIdentity * 100)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
189 countSbj = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
190 if whatToCount.lower() == "query":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
191 dMatches = MatchUtils.getDictOfListsWithQueryAsKey(lMatches)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
192 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
193 dMatches = MatchUtils.getDictOfListsWithSubjectAsKey(lMatches)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
194
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
195 for qry in dMatches.keys():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
196 countMatch = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
197 for match in dMatches[ qry ]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
198
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
199 if match.identity >= thresIdentityPerc and getattr(match, whatToCount.lower() + "_length_perc") >= thresLength:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
200 countMatch += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
201 if countMatch > 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
202 countSbj += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
203 return countSbj
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
204
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
205 # # Convert a 'match' file (output from Matcher) into an 'align' file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
206 # # replace old parser.tab2align
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
207 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
208 # @param inFileName a string input file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
209 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
210 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
211 def convertMatchFileToAlignFile(inFileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
212 basename = os.path.splitext(inFileName)[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
213 outFileName = "%s.align" % basename
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
214 outFile = open(outFileName, "w")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
215
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
216 lMatches = MatchUtils.getMatchListFromFile(inFileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
217
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
218 for match in lMatches:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
219 string = "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (match.getQueryName(), match.getQueryStart(), match.getQueryEnd(), match.getSubjectName(), match.getSubjectStart(), match.getSubjectEnd(), match.getEvalue(), match.getScore(), match.getIdentity())
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
220 outFile.write(string)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
221
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
222 outFile.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
223
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
224 # # Convert a 'match' file (output from Matcher) into an 'abc' file (MCL input file)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
225 # for each match, compute coverage on the smallest seq using this expression
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
226 # Min (|QMatchEnd - QMatchStart + 1|,|SMatchEnd - SMatchStart + 1|)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
227 #coverage = -----------------------------------------------------------------
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
228 # Min (QLength,SLength)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
229 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
230 # Use this coverage for arc value. Matches of a sequence on itself are filtered.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
231 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
232 # @param matchFileName string input match file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
233 # @param outFileName string output abc file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
234 # @param coverage float coverage filter threshold
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
235 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
236 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
237 def convertMatchFileIntoABCFileWithCoverageComputeOnSmallestSeq(matchFileName, outFileName, coverageThreshold = 0):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
238 sSeqNames = set()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
239 sSeqNamesUsed = set()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
240 with open(outFileName, "w") as outF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
241 with open(matchFileName) as inF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
242 inF.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
243 for inLine in inF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
244 splittedLine = inLine.split("\t")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
245 qName = splittedLine[0]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
246 sName = splittedLine[6]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
247 sSeqNames.add(qName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
248 sSeqNames.add(sName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
249 if qName != sName:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
250 matchMin = min(abs(float(splittedLine[2]) - float(splittedLine[1]) + 1), abs(float(splittedLine[8]) - float(splittedLine[7]) + 1))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
251 seqMin = min(int(splittedLine[3]) / float(splittedLine[4]), int(splittedLine[9]) / float(splittedLine[10]))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
252 coverage = round(matchMin / seqMin, 2)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
253 if coverage >= coverageThreshold:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
254 outF.write("%s\n" % "\t".join([qName, sName, str(coverage)]))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
255 sSeqNamesUsed.add(qName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
256 sSeqNamesUsed.add(sName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
257
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
258 with open("%s.unused" % outFileName, "w") as outSeqNameOfUnusedMatchesF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
259 for seqName in sorted(sSeqNames - sSeqNamesUsed):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
260 outSeqNameOfUnusedMatchesF.write("%s\n" % seqName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
261
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
262 if FileUtils.isEmpty(outFileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
263 print "WARNING: '%s' is empty." % outFileName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
264
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
265 # # Convert a 'match' file (output from Matcher) into an 'abc' file (MCL input file)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
266 # Use coverage on query for arc value
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
267 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
268 # @param matchFileName string input match file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
269 # @param outFileName string output abc file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
270 # @param coverage float query coverage filter threshold
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
271 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
272 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
273 def convertMatchFileIntoABCFileOnQueryCoverage(matchFileName, outFileName, coverage = 0):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
274 with open(matchFileName) as inF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
275 with open(outFileName, "w") as outF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
276 inF.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
277 for inLine in inF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
278 splittedLine = inLine.split("\t")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
279 if float(splittedLine[4]) >= coverage:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
280 outLine = "\t".join([splittedLine[0], splittedLine[6], splittedLine[4]])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
281 outLine += "\n"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
282 outF.write(outLine)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
283
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
284 if FileUtils.isEmpty(outFileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
285 print "WARNING: '%s' is empty." % outFileName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
286
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
287 # # Adapt the path IDs as the input file is the concatenation of several 'Match' files, and remove the extra header lines.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
288 # # replace old parser.tabnum2id
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
289 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
290 # @param fileName a string input file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
291 # @param outputFileName a string output file name (optional)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
292 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
293 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
294 def generateMatchFileWithNewPathId(fileName, outputFileName = None):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
295 if outputFileName is None:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
296 outFile = open(fileName, "w")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
297 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
298 outFile = open(outputFileName, "w")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
299 outFile.write("query.name\tquery.start\tquery.end\tquery.length\tquery.length.%\tmatch.length.%\tsubject.name\tsubject.start\tsubject.end\tsubject.length\tsubject.length.%\tE.value\tScore\tIdentity\tpath\n")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
300
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
301 lMatches = MatchUtils.getMatchListFromFile(fileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
302 count = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
303 dMatchKeyIdcount = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
304
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
305 for match in lMatches:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
306 key_id = str(match.getIdentifier()) + "-" + match.getQueryName() + "-" + match.getSubjectName()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
307 if not key_id in dMatchKeyIdcount.keys():
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
308 newPath = count
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
309 count += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
310 dMatchKeyIdcount[ key_id ] = newPath
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
311 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
312 newPath = dMatchKeyIdcount[ key_id ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
313
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
314 match.id = newPath
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
315 outFile.write(match.toString() + "\n")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
316 outFile.close()