annotate commons/core/coord/MatchUtils.py @ 32:3441fe98a2ba

Deleted selected files
author m-zytnicki
date Tue, 30 Apr 2013 14:34:10 -0400
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # http://www.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import math
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import sys
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 from commons.core.coord.Match import Match
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from commons.core.checker.RepetException import RepetException
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 ## Static methods for the manipulation of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 class MatchUtils ( object ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 ## Return a list with Match instances from the given file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 # @param inFile name of a file in the Match format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 # @return a list of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 def getMatchListFromFile(inFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 lMatchInstances = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 inFileHandler = open( inFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 line = inFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 if line[0:10] == "query.name":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 m = Match()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 m.setFromString( line )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 lMatchInstances.append( m )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 inFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 return lMatchInstances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 getMatchListFromFile = staticmethod( getMatchListFromFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 ## Split a Match list in several Match lists according to the subject
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 # @param lMatches a list of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 # @return a dictionary which keys are subject names and values Match lists
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 def getDictOfListsWithSubjectAsKey( lMatches ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 dSubject2MatchList = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 for iMatch in lMatches:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 if not dSubject2MatchList.has_key( iMatch.range_subject.seqname ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 dSubject2MatchList[ iMatch.range_subject.seqname ] = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 dSubject2MatchList[ iMatch.range_subject.seqname ].append( iMatch )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 return dSubject2MatchList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 getDictOfListsWithSubjectAsKey = staticmethod( getDictOfListsWithSubjectAsKey )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 ## Split a Match list in several Match lists according to the query
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 # @param lMatches a list of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 # @return a dictionary which keys are query names and values Match lists
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 def getDictOfListsWithQueryAsKey ( lMatches ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 dQuery2MatchList = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 for iMatch in lMatches:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 if not dQuery2MatchList.has_key( iMatch.range_query.seqname ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 dQuery2MatchList[ iMatch.range_query.seqname ] = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 dQuery2MatchList[ iMatch.range_query.seqname ].append( iMatch )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 return dQuery2MatchList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 getDictOfListsWithQueryAsKey = staticmethod( getDictOfListsWithQueryAsKey )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 ## Write Match instances contained in the given list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 # @param lMatches a list of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 # @param fileName name of the file to write the Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 # @param mode the open mode of the file ""w"" or ""a""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 def writeListInFile( lMatches, fileName, mode="w", header=None ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 fileHandler = open( fileName, mode )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 if header:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 fileHandler.write( header )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 for iMatch in lMatches:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 iMatch.write( fileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 fileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 writeListInFile = staticmethod( writeListInFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 ## Give path id list from a list of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 # @param lMatch list of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 # @return lId integer list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 def getIdListFromMatchList(lMatch):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 lId = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 for iMatch in lMatch:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 lId.append(iMatch.id)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 return lId
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 getIdListFromMatchList = staticmethod(getIdListFromMatchList)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 ## Remove duplicated matches in a match list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 # ## replace old PyRepet.MatchDB.rmvDoublons()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 # @param lMatch list of Match instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 # @return lMatchesUniq match unique list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 def rmvDuplicateMatches(lMatch):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 lMatchesUniq = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 for match in lMatch:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 if len(lMatchesUniq) == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 lMatchesUniq.append( match )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 nbDoublons = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 for m in lMatchesUniq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 if match.isDoublonWith( m ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 nbDoublons += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 if nbDoublons == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 lMatchesUniq.append( match )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 for match1 in lMatchesUniq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 for match2 in lMatchesUniq:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 if match1.id != match2.id:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 if match1.isDoublonWith( match2 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 raise RepetException ( "*** Error: doublon not removed" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 return lMatchesUniq
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 rmvDuplicateMatches = staticmethod(rmvDuplicateMatches)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 ## Return the list of queries 'included' in subjects when two different databanks are used.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 ##replace old pyRepet.MatchDB.filterDiffQrySbj()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 # @param iBioseqDB bioseqDB databank of queries
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 # @param thresIdentity float identity threshold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 # @param thresLength float length threshold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 # @param verbose int verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 # @return lMatches match list to keep according to length and identity thresholds
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 #TODO: don't take into account match for sequence against itself. To do ?
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 def filterDiffQrySbj(iBioseqDB, matchFile, thresIdentity=0.95, thresLength=0.98, verbose=0 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 if verbose > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166 print "filtering matches (id>=%.2f,qlgth>=%.2f)..." % ( thresIdentity, thresLength ); sys.stdout.flush()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168 thresIdentityPerc = math.floor( thresIdentity*100 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 lQryToKeep = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 dQry2Matches = MatchUtils.getDictOfListsWithQueryAsKey(MatchUtils.getMatchListFromFile(matchFile))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 for seqH in iBioseqDB.idx.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 # keep it if it has no match
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 if not dQry2Matches.has_key( seqH ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 if seqH not in lQryToKeep:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 lQryToKeep.append( seqH )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 isConditionsMet = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 for match in dQry2Matches[ seqH ]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 # check if they are above the thresholds
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 if match.identity >= thresIdentityPerc and match.query_length_perc >= thresLength:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182 isConditionsMet = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184 if not isConditionsMet and seqH not in lQryToKeep:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 lQryToKeep.append( seqH )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 return lQryToKeep
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 filterDiffQrySbj = staticmethod(filterDiffQrySbj)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 ## Count the number of distinct matches involved in at least one match above the thresholds.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 ##replace old pyRepet.coord.MatchDB.getNbDistinctSbjWithThres() and pyRepet.coord.MatchDB.getNbDistinctSbjWithThres()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192 # @param thresIdentity float identity threshold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194 # @param thresLength float length threshold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 def getNbDistinctSequencesInsideMatchesWithThresh(lMatches, thresIdentity=0.95, thresLength=0.98, whatToCount="query" ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 thresIdentityPerc = math.floor( thresIdentity*100 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 countSbj = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 if whatToCount.lower() == "query":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 dMatches = MatchUtils.getDictOfListsWithQueryAsKey(lMatches)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 dMatches = MatchUtils.getDictOfListsWithSubjectAsKey(lMatches)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 for qry in dMatches.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 countMatch = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206 for match in dMatches[ qry ]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208 if match.identity >= thresIdentityPerc and getattr(match,whatToCount.lower() +"_length_perc") >= thresLength:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 countMatch += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 if countMatch > 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 countSbj += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 return countSbj
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 getNbDistinctSequencesInsideMatchesWithThresh = staticmethod(getNbDistinctSequencesInsideMatchesWithThresh)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 ## Convert a 'match' file (output from Matcher) into an 'align' file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 ## replace old parser.tab2align
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 # @param inFileName a string input file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 def convertMatchFileToAlignFile(inFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222 basename = os.path.splitext(inFileName)[0]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 outFileName = "%s.align" % basename
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224 outFile = open(outFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 lMatches = MatchUtils.getMatchListFromFile(inFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 for match in lMatches:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 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() )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 outFile.write( string )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 convertMatchFileToAlignFile = staticmethod(convertMatchFileToAlignFile)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 ## Convert a 'match' file (output from Matcher) into an 'abc' file (MCL input file)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 # Use coverage on query for arc value
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 # @param matchFileName string input match file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 # @param outFileName string output abc file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 # @param coverage float query coverage filter threshold
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 def convertMatchFileIntoABCFileOnQueryCoverage(matchFileName, outFileName, coverage = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245 with open(matchFileName) as inF:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 with open(outFileName, "w") as outF:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247 inF.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248 inLine = inF.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 while inLine:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 splittedLine = inLine.split("\t")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 if float(splittedLine[4]) >= coverage:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 outLine = "\t".join([splittedLine[0], splittedLine[6], splittedLine[4]])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 outLine += "\n"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 outF.write(outLine)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 inLine = inF.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 ## Adapt the path IDs as the input file is the concatenation of several 'Match' files, and remove the extra header lines.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258 ## replace old parser.tabnum2id
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260 # @param fileName a string input file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 # @param outputFileName a string output file name (optional)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 def generateMatchFileWithNewPathId(fileName, outputFileName=None):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 if outputFileName is None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 outFile = open(fileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 outFile = open(outputFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 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")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 lMatches = MatchUtils.getMatchListFromFile(fileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 count = 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272 dMatchKeyIdcount = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274 for match in lMatches:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 key_id = str(match.getIdentifier()) + "-" + match.getQueryName() + "-" + match.getSubjectName()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 if not key_id in dMatchKeyIdcount.keys():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 newPath = count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 count += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 dMatchKeyIdcount[ key_id ] = newPath
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 newPath = dMatchKeyIdcount[ key_id ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 match.id = newPath
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284 outFile.write( match.toString()+"\n" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 outFile.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 generateMatchFileWithNewPathId = staticmethod(generateMatchFileWithNewPathId)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288