annotate TEisotools-1.0/commons/core/coord/MapUtils.py @ 6:20ec0d14798e draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 05:00:24 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
1 # Copyright INRA (Institut National de la Recherche Agronomique)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
2 # http://www.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
3 # http://urgi.versailles.inra.fr
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
4 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
5 # This software is governed by the CeCILL license under French law and
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
6 # abiding by the rules of distribution of free software. You can use,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
7 # modify and/ or redistribute the software under the terms of the CeCILL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
8 # license as circulated by CEA, CNRS and INRIA at the following URL
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
9 # "http://www.cecill.info".
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
10 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
11 # As a counterpart to the access to the source code and rights to copy,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
12 # modify and redistribute granted by the license, users are provided only
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
13 # with a limited warranty and the software's author, the holder of the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
14 # economic rights, and the successive licensors have only limited
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
15 # liability.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
16 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
17 # In this respect, the user's attention is drawn to the risks associated
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
18 # with loading, using, modifying and/or developing or reproducing the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
19 # software by the user in light of its specific status of free software,
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
20 # that may mean that it is complicated to manipulate, and that also
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
21 # therefore means that it is reserved for developers and experienced
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
22 # professionals having in-depth computer knowledge. Users are therefore
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
23 # encouraged to load and test the software's suitability as regards their
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
24 # requirements in conditions enabling the security of their systems and/or
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
25 # data to be ensured and, more generally, to use and operate it in the
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
26 # same conditions as regards security.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
27 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
28 # The fact that you are presently reading this means that you have had
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
29 # knowledge of the CeCILL license and that you accept its terms.
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
30
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
31 import os
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
32 import sys
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
33 from commons.core.coord.Map import Map
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.coord.Set import Set
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.checker.CheckerUtils import CheckerUtils
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 Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
39 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
40 class MapUtils( object ):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
41
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
42 ## Return a Map list from a Map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
43 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
44 # @param mapFile string name of a Map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
45 # @return a list of Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
46 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
47 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
48 def getMapListFromFile(mapFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
49 lMaps = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
50 with open(mapFile, "r") as mapF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
51 for line in mapF:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
52 iMap = Map()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
53 iMap.setFromString(line)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
54 lMaps.append(iMap)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
55 return lMaps
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
56
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
57
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
58 ## Return a list of Map instances sorted in increasing order according to the min, then the max, and finally their initial order
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
59 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
60 # @param lMaps list of Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
61 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
62 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
63 def getMapListSortedByIncreasingMinThenMax(lMaps):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
64 return sorted(lMaps, key = lambda iMap: (iMap.getMin(), iMap.getMax()))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
65
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
66
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
67 ## Return a list of Map instances sorted in increasing order according to the name, then the seqname, then the min, then the max
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
68 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
69 # @param lMaps list of Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
70 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
71 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
72 def getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax(lMaps):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
73 return sorted(lMaps, key = lambda iMap: (iMap.getName(), iMap.getSeqname(), iMap.getMin(), iMap.getMax()))
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
74
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
75
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
76 ## Return a dictionary which keys are Map names and values the corresponding Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
77 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
78 # @param mapFile string input map file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
79 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
80 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
81 def getDictPerNameFromMapFile(mapFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
82 dName2Maps = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
83 with open(mapFile) as mapFileHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
84 for line in mapFileHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
85 iMap = Map()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
86 iMap.setFromString(line, "\t")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
87 if dName2Maps.has_key(iMap.name):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
88 if iMap != dName2Maps[iMap.name]:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
89 msg = "ERROR: in file '%s' two different Map instances have the same name '%s'" % (mapFile, iMap.name)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
90 sys.stderr.write("%s\n" % msg)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
91 sys.exit(1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
92 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
93 dName2Maps[iMap.name] = iMap
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
94 return dName2Maps
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
95
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
96
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
97 ## Give a list of Set instances from a list of Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
98 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
99 # @param lMaps list of Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
100 # @return lSets list of Set instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
101 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
102 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
103 def mapList2SetList(lMaps):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
104 lSets = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
105 c = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
106 for iMap in lMaps:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
107 c += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
108 iSet = Set()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
109 iSet.id = c
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
110 iSet.name = iMap.getName()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
111 iSet.seqname = iMap.getSeqname()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
112 iSet.start = iMap.getStart()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
113 iSet.end = iMap.getEnd()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
114 lSets.append(iSet)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
115 return lSets
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
116
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
117
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
118 ## Merge the Map instances in a Map file using 'mapOp'
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
119 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
120 # @param mapFile string input map file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
121 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
122 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
123 def mergeCoordsInFile(mapFile, outFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
124 if not CheckerUtils.isExecutableInUserPath("mapOp"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
125 msg = "WARNING: can't find executable 'mapOp'"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
126 sys.stderr.write("%s\n" % msg)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
127 else:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
128 cmd = "mapOp"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
129 cmd += " -q %s" % mapFile
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
130 cmd += " -m"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
131 cmd += " 2>&1 > /dev/null"
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
132 returnStatus = os.system(cmd)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
133 if returnStatus != 0:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
134 print "ERROR: mapOp returned %i" % returnStatus
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
135 sys.exit(1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
136 os.rename("%s.merge" % mapFile, outFile)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
137
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
138
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
139 ## Return a dictionary which keys are Map seqnames and values the corresponding Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
140 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
141 # @param mapFile string input map file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
142 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
143 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
144 def getDictPerSeqNameFromMapFile(mapFile):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
145 dSeqName2Maps = {}
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
146 with open(mapFile) as mapFileHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
147 for line in mapFileHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
148 iMap = Map()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
149 iMap.setFromString(line, "\t")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
150 if not dSeqName2Maps.has_key(iMap.seqname):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
151 dSeqName2Maps[iMap.seqname] = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
152 dSeqName2Maps[iMap.seqname].append(iMap)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
153 return dSeqName2Maps
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
154
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
155
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
156 ## Convert an Map file into a Set file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
157 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
158 # @param mapFileName string input map file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
159 # @param setFileName string output set file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
160 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
161 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
162 def convertMapFileIntoSetFile(mapFileName, setFileName = ""):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
163 if not setFileName:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
164 setFileName = "%s.set" % mapFileName
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
165 setFileHandler = open(setFileName, "w")
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
166 count = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
167 iMap = Map()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
168 with open(mapFileName, "r") as mapFileHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
169 for line in mapFileHandler:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
170 count += 1
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
171 iMap.setFromString(line)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
172 iSet = Set()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
173 iSet.id = count
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
174 iSet.name = iMap.getName()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
175 iSet.seqname = iMap.getSeqname()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
176 iSet.start = iMap.getStart()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
177 iSet.end = iMap.getEnd()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
178 iSet.write(setFileHandler)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
179 setFileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
180
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
181
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
182 ## Write Map instances contained in the given list
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
183 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
184 # @param lMaps list of Map instances
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
185 # @param fileName a file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
186 # @param mode the open mode of the file '"w"' or '"a"'
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
187 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
188 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
189 def writeListInFile(lMaps, fileName, mode = "w"):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
190 fileHandler = open(fileName, mode)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
191 for iMap in lMaps:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
192 iMap.write(fileHandler)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
193 fileHandler.close()
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
194
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
195
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
196 ## Get the length of the shorter annotation in map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
197 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
198 # @param mapFile string input map file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
199 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
200 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
201 def getMinLengthOfMapFile(mapFileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
202 lSizes = []
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
203 with open(mapFileName) as fH:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
204 for line in fH:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
205 start = int(line.split('\t')[2])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
206 end = int(line.split('\t')[3])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
207 lSizes.append(end - start + 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
208 return min(lSizes)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
209
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
210
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
211 ## Get the length of the longest annotation in map file
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
212 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
213 # @param mapFile string input map file name
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
214 #
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
215 @staticmethod
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
216 def getMaxLengthOfMapFile(mapFileName):
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
217 maxLength = 0
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
218 with open(mapFileName) as fH:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
219 for line in fH:
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
220 start = int(line.split('\t')[2])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
221 end = int(line.split('\t')[3])
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
222 maxLength = max(maxLength, end - start + 1)
20ec0d14798e Uploaded
urgi-team
parents:
diff changeset
223 return maxLength