annotate TEisotools-1.1.a/commons/core/coord/MatchUtils.py @ 13:feef9a0db09d draft

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