annotate TEisotools-1.0/commons/core/coord/AlignUtils.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
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32 import os
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 import sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 import shutil
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.coord.Align import Align
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
36
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
37
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
38 ## Static methods manipulating Align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40 class AlignUtils( object ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 ## Return a list with Align 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 Align format
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 def getAlignListFromFile( inFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 lAlignInstances = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 inFileHandler = open( inFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 line = inFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 a = Align()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 a.setFromString( line )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 lAlignInstances.append( a )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56 inFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57 return lAlignInstances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 getAlignListFromFile = staticmethod( getAlignListFromFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 ## Return a list with all the scores
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 # @param lAlignInstances: list of Align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66 def getListOfScores( lAlignInstances ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 lScores = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 for iAlign in lAlignInstances:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 lScores.append( iAlign.score )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 return lScores
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 getListOfScores = staticmethod( getListOfScores )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75 ## Return a list with all the scores from the given file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 # @param inFile name of a file in the Align format
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 def getScoreListFromFile(inFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 lScores = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 append = lScores.append
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 with open(inFile, "r") as inFileHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 line = inFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 while line:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 if line != "\n":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 append(int(line.split('\t')[7]))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 line = inFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 return lScores
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 getScoreListFromFile = staticmethod( getScoreListFromFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 ## for each line of a given Align file, write the coordinates on the query and the subject as two distinct lines in a Map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95 # @param alignFile: name of the input Align file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96 # @param mapFile: name of the output Map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 def convertAlignFileIntoMapFileWithQueriesAndSubjects( alignFile, mapFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 alignFileHandler = open( alignFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 mapFileHandler = open( mapFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 iAlign = Align()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 line = alignFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 iAlign.setFromString( line )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 iMapQ, iMapS = iAlign.getMapsOfQueryAndSubject()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 iMapQ.write( mapFileHandler )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 iMapS.write( mapFileHandler )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 alignFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 mapFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 convertAlignFileIntoMapFileWithQueriesAndSubjects = staticmethod( convertAlignFileIntoMapFileWithQueriesAndSubjects )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116 ## for each line of a given Align file, write the coordinates of the subject on the query as one line in a Map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 # @param alignFile: name of the input Align file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 # @param mapFile: name of the output Map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 def convertAlignFileIntoMapFileWithSubjectsOnQueries( alignFile, mapFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 alignFileHandler = open( alignFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 mapFileHandler = open( mapFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 iAlign = Align()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 line = alignFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 iAlign.setFromString( line )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 iMapQ = iAlign.getSubjectAsMapOfQuery()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 iMapQ.write( mapFileHandler )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 alignFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 mapFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 convertAlignFileIntoMapFileWithSubjectsOnQueries = staticmethod( convertAlignFileIntoMapFileWithSubjectsOnQueries )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138 ## return a list of Align instances sorted in decreasing order according to their score, then their length on the query and finally their initial order
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140 # @param lAligns: list of Align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 def getAlignListSortedByDecreasingScoreThenLength( lAligns ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 return sorted( lAligns, key=lambda iAlign: ( 1 / float(iAlign.getScore()), 1 / float(iAlign.getLengthOnQuery()) ) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 getAlignListSortedByDecreasingScoreThenLength = staticmethod( getAlignListSortedByDecreasingScoreThenLength )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 ## Convert an Align file into a Path file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150 # @param alignFile string name of the input Align file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 # @param pathFile string name of the output Path file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 def convertAlignFileIntoPathFile( alignFile, pathFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154 alignFileHandler = open( alignFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155 pathFileHandler = open( pathFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156 iAlign = Align()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 countAlign = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159 line = alignFileHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
162 countAlign += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
163 iAlign.setFromString( line, "\t" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
164 pathFileHandler.write( "%i\t%s\n" % ( countAlign, iAlign.toString() ) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
165 alignFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
166 pathFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
167
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
168 convertAlignFileIntoPathFile = staticmethod( convertAlignFileIntoPathFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
169
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
170
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
171 ## Sort an Align file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
172 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
173 def sortAlignFile( inFile, outFile="" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
174 if outFile == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
175 outFile = "%s.sort" % ( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
176 prg = "sort"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
177 cmd = prg
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
178 cmd += " -k 1,1 -k 4,4 -k 2,2n -k 3,3n -k 5,5n -k 6,6n -k 8,8n"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
179 cmd += " %s" % ( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
180 cmd += " > %s" % ( outFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
181 exitStatus = os.system( cmd )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
182 if exitStatus != 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
183 msg = "ERROR: '%s' returned '%i'" % ( prg, exitStatus )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
184 sys.stderr.write( "%s\n" % ( msg ) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
185 sys.exit( exitStatus )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
186
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
187 sortAlignFile = staticmethod( sortAlignFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
188
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
189
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
190 ## Write Align instances contained in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
191 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
192 # @param lAlign a list of Align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
193 # @param fileName name of the file to write the Align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
194 # @param mode the open mode of the file ""w"" or ""a""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
195 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
196 def writeListInFile( lAlign, fileName, mode="w" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
197 fileHandler = open( fileName, mode )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
198 for iAlign in lAlign:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
199 iAlign.write( fileHandler )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
200 fileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
201
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
202 writeListInFile = staticmethod( writeListInFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
203
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
204
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
205 ## Split a list of Align instances according to the name of the query
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
206 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
207 # @param lInAlign list of align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
208 # @return lOutAlignList list of align instances lists
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
209 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
210 def splitAlignListByQueryName( lInAlign ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
211 lSortedAlign = sorted(lInAlign, key=lambda o: o.range_query.seqname)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
212 lOutAlignList = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
213 if len(lSortedAlign) != 0 :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
214 lAlignForCurrentQuery = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
215 previousQuery = lSortedAlign[0].range_query.seqname
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
216 for align in lSortedAlign :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
217 currentQuery = align.range_query.seqname
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
218 if previousQuery != currentQuery :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
219 lOutAlignList.append(lAlignForCurrentQuery)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
220 previousQuery = currentQuery
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
221 lAlignForCurrentQuery = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
222 lAlignForCurrentQuery.append(align)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
223
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
224 lOutAlignList.append(lAlignForCurrentQuery)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
225
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
226 return lOutAlignList
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
227
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
228 splitAlignListByQueryName = staticmethod( splitAlignListByQueryName )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
229
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
230
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
231 ## Create an Align file from each list of Align instances in the input list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
232 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
233 # @param lAlignList list of lists with Align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
234 # @param pattern string
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
235 # @param dirName string
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
236 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
237 def createAlignFiles( lAlignList, pattern, dirName="" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
238 savedDir = os.getcwd()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
239 nbFiles = len(lAlignList)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
240 countFile = 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
241 if dirName != "" :
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
242 try:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
243 os.makedirs(dirName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
244 except:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
245 pass
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
246 os.chdir(dirName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
247
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
248 for lAlign in lAlignList:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
249 fileName = "%s_%s.align" % (pattern, str(countFile).zfill(len(str(nbFiles))))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
250 AlignUtils.writeListInFile(lAlign, fileName)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
251 countFile += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
252 os.chdir(savedDir)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
253
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
254 createAlignFiles = staticmethod( createAlignFiles )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
255
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
256
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
257 ## Return a list with Align instances sorted by query name, subject name, query start, query end and score
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
258 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
259 def sortList( lAligns ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
260 return sorted( lAligns, key=lambda iAlign: ( iAlign.getQueryName(),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
261 iAlign.getSubjectName(),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
262 iAlign.getQueryStart(),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
263 iAlign.getQueryEnd(),
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
264 iAlign.getScore() ) )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
265
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
266 sortList = staticmethod( sortList )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
267
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
268
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
269 ## Return a list after merging all overlapping Align instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
270 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
271 def mergeList( lAligns ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
272 lMerged = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
273
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
274 lSorted = AlignUtils.sortList( lAligns )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
275
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
276 prev_count = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
277 for iAlign in lSorted:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
278 if prev_count != len(lSorted):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
279 for i in lSorted[ prev_count + 1: ]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
280 if iAlign.isOverlapping( i ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
281 iAlign.merge( i )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
282 IsAlreadyInList = False
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
283 for newAlign in lMerged:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
284 if newAlign.isOverlapping( iAlign ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
285 IsAlreadyInList = True
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
286 newAlign.merge( iAlign )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
287 lMerged [ lMerged.index( newAlign ) ] = newAlign
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
288 if not IsAlreadyInList:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
289 lMerged.append( iAlign )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
290 prev_count += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
291
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
292 return lMerged
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
293
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
294 mergeList = staticmethod( mergeList )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
295
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
296
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
297 ## Merge all Align instance in a given Align file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
298 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
299 def mergeFile( inFile, outFile="" ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
300 if outFile == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
301 outFile = "%s.merged" % ( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
302 if os.path.exists( outFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
303 os.remove( outFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
304
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
305 tmpFile = "%s.sorted" % ( inFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
306 AlignUtils.sortAlignFile( inFile, tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
307
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
308 tmpF = open( tmpFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
309 dQrySbj2Aligns = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
310 prevPairQrySbj = ""
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
311 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
312 line = tmpF.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
313 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
314 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
315 iAlign = Align()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
316 iAlign.setFromString( line )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
317 pairQrySbj = "%s_%s" % ( iAlign.getQueryName(), iAlign.getSubjectName() )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
318 if not dQrySbj2Aligns.has_key( pairQrySbj ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
319 if prevPairQrySbj != "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
320 lMerged = AlignUtils.mergeList( dQrySbj2Aligns[ prevPairQrySbj ] )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
321 AlignUtils.writeListInFile( lMerged, outFile, "a" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
322 del dQrySbj2Aligns[ prevPairQrySbj ]
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
323 prevPairQrySbj = pairQrySbj
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
324 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
325 prevPairQrySbj = pairQrySbj
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
326 dQrySbj2Aligns[ pairQrySbj ] = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
327 dQrySbj2Aligns[ pairQrySbj ].append( iAlign )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
328 lMerged = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
329 if len(dQrySbj2Aligns.keys()) > 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
330 lMerged = AlignUtils.mergeList( dQrySbj2Aligns[ prevPairQrySbj ] )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
331 AlignUtils.writeListInFile( lMerged, outFile, "a" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
332 tmpF.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
333 os.remove( tmpFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
334
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
335 mergeFile = staticmethod( mergeFile )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
336
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
337
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
338 ## Update the scores of each match in the input file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
339 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
340 # @note the new score is the length on the query times the percentage of identity
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
341 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
342 def updateScoresInFile( inFile, outFile ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
343 inHandler = open( inFile, "r" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
344 outHandler = open( outFile, "w" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
345 iAlign = Align()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
346
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
347 while True:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
348 line = inHandler.readline()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
349 if line == "":
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
350 break
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
351 iAlign.reset()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
352 iAlign.setFromString( line, "\t" )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
353 iAlign.updateScore()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
354 iAlign.write( outHandler )
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
355
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
356 inHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
357 outHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
358
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
359 updateScoresInFile = staticmethod( updateScoresInFile )