annotate TEisotools-1.1.a/commons/core/coord/MapUtils.py @ 15:255c852351c5 draft

Uploaded
author urgi-team
date Thu, 21 Jul 2016 07:36:44 -0400
parents feef9a0db09d
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 from commons.core.coord.Map import Map
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.coord.Set import Set
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.checker.CheckerUtils import CheckerUtils
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38 ## static methods manipulating Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40 class MapUtils( object ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42 ## Return a Map list from a Map file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44 # @param mapFile string name of a Map file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45 # @return a list of Map 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 getMapListFromFile(mapFile):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 lMaps = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 with open(mapFile, "r") as mapF:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 for line in mapF:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 iMap = Map()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 iMap.setFromString(line)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 lMaps.append(iMap)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 return lMaps
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57
feef9a0db09d 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
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60 # @param lMaps list of Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 def getMapListSortedByIncreasingMinThenMax(lMaps):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 return sorted(lMaps, key = lambda iMap: (iMap.getMin(), iMap.getMax()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66
feef9a0db09d 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
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 # @param lMaps list of Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72 def getMapListSortedByIncreasingNameThenSeqnameThenMinThenMax(lMaps):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 return sorted(lMaps, key = lambda iMap: (iMap.getName(), iMap.getSeqname(), iMap.getMin(), iMap.getMax()))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 ## Return a dictionary which keys are Map names and values the corresponding Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78 # @param mapFile string input map file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 def getDictPerNameFromMapFile(mapFile):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 dName2Maps = {}
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83 with open(mapFile) as mapFileHandler:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84 for line in mapFileHandler:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85 iMap = Map()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 iMap.setFromString(line, "\t")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 if dName2Maps.has_key(iMap.name):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 if iMap != dName2Maps[iMap.name]:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 msg = "ERROR: in file '%s' two different Map instances have the same name '%s'" % (mapFile, iMap.name)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 sys.stderr.write("%s\n" % msg)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91 sys.exit(1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 dName2Maps[iMap.name] = iMap
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 return dName2Maps
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 ## Give a list of Set instances from a list of Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 # @param lMaps list of Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 # @return lSets list of Set instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 def mapList2SetList(lMaps):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 lSets = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 c = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 for iMap in lMaps:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107 c += 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 iSet = Set()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 iSet.id = c
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 iSet.name = iMap.getName()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 iSet.seqname = iMap.getSeqname()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 iSet.start = iMap.getStart()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 iSet.end = iMap.getEnd()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 lSets.append(iSet)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 return lSets
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118 ## Merge the Map instances in a Map file using 'mapOp'
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120 # @param mapFile string input map file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 def mergeCoordsInFile(mapFile, outFile):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 if not CheckerUtils.isExecutableInUserPath("mapOp"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125 msg = "WARNING: can't find executable 'mapOp'"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
126 sys.stderr.write("%s\n" % msg)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
127 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
128 cmd = "mapOp"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
129 cmd += " -q %s" % mapFile
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
130 cmd += " -m"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
131 cmd += " 2>&1 > /dev/null"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
132 returnStatus = os.system(cmd)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
133 if returnStatus != 0:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
134 print "ERROR: mapOp returned %i" % returnStatus
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
135 sys.exit(1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
136 os.rename("%s.merge" % mapFile, outFile)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
137
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
138
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
139 ## Return a dictionary which keys are Map seqnames and values the corresponding Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
140 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
141 # @param mapFile string input map file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
142 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
143 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
144 def getDictPerSeqNameFromMapFile(mapFile):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
145 dSeqName2Maps = {}
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
146 with open(mapFile) as mapFileHandler:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
147 for line in mapFileHandler:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
148 iMap = Map()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
149 iMap.setFromString(line, "\t")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
150 if not dSeqName2Maps.has_key(iMap.seqname):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
151 dSeqName2Maps[iMap.seqname] = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
152 dSeqName2Maps[iMap.seqname].append(iMap)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
153 return dSeqName2Maps
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
154
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
155
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
156 ## Convert an Map file into a Set file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
157 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
158 # @param mapFileName string input map file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
159 # @param setFileName string output set file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
160 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
161 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
162 def convertMapFileIntoSetFile(mapFileName, setFileName = ""):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
163 if not setFileName:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
164 setFileName = "%s.set" % mapFileName
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
165 setFileHandler = open(setFileName, "w")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
166 count = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
167 iMap = Map()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
168 with open(mapFileName, "r") as mapFileHandler:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
169 for line in mapFileHandler:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
170 count += 1
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
171 iMap.setFromString(line)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
172 iSet = Set()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
173 iSet.id = count
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
174 iSet.name = iMap.getName()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
175 iSet.seqname = iMap.getSeqname()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
176 iSet.start = iMap.getStart()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
177 iSet.end = iMap.getEnd()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
178 iSet.write(setFileHandler)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
179 setFileHandler.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
180
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
181
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
182 ## Write Map instances contained in the given list
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
183 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
184 # @param lMaps list of Map instances
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
185 # @param fileName a file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
186 # @param mode the open mode of the file '"w"' or '"a"'
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
187 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
188 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
189 def writeListInFile(lMaps, fileName, mode = "w"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
190 fileHandler = open(fileName, mode)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
191 for iMap in lMaps:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
192 iMap.write(fileHandler)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
193 fileHandler.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
194
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
195
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
196 ## Get the length of the shorter annotation in map file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
197 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
198 # @param mapFile string input map file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
199 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
200 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
201 def getMinLengthOfMapFile(mapFileName):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
202 lSizes = []
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
203 with open(mapFileName) as fH:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
204 for line in fH:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
205 start = int(line.split('\t')[2])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
206 end = int(line.split('\t')[3])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
207 lSizes.append(end - start + 1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
208 return min(lSizes)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
209
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
210
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
211 ## Get the length of the longest annotation in map file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
212 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
213 # @param mapFile string input map file name
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
214 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
215 @staticmethod
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
216 def getMaxLengthOfMapFile(mapFileName):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
217 maxLength = 0
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
218 with open(mapFileName) as fH:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
219 for line in fH:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
220 start = int(line.split('\t')[2])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
221 end = int(line.split('\t')[3])
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
222 maxLength = max(maxLength, end - start + 1)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
223 return maxLength