annotate commons/core/coord/SetUtils.py @ 22:1e3f2c2657a3

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:26:07 -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
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 from commons.core.coord.Set import Set
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 ## Static methods for the manipulation of Path instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 class SetUtils( object ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 ## Change the identifier of each Set instance in the given list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 # @param newId new identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 def changeIdInList(lSets, newId):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 iSet.id = newId
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 changeIdInList = staticmethod( changeIdInList )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 ## Return the length of the overlap between two lists of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 # @param lSets1 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 # @param lSets2 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 # @return length of overlap
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 # @warning sequence names are supposed to be identical
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 def getOverlapLengthBetweenLists(lSets1, lSets2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 lSet1Sorted = SetUtils.getSetListSortedByIncreasingMinThenMax(lSets1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 lSet2Sorted = SetUtils.getSetListSortedByIncreasingMinThenMax(lSets2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 osize = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 i = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 j = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 while i!= len(lSet1Sorted):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 while j!= len(lSet2Sorted) and lSet1Sorted[i].getMin()>lSet2Sorted[j].getMax()\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 and not(lSet1Sorted[i].isOverlapping(lSet2Sorted[j])):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 j+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 jj=j
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 while jj!= len(lSet2Sorted) and lSet1Sorted[i].isOverlapping(lSet2Sorted[jj]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 osize+=lSet1Sorted[i].getOverlapLength(lSet2Sorted[jj])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 jj+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 i+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 return osize
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 getOverlapLengthBetweenLists = staticmethod( getOverlapLengthBetweenLists )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 ## Return True if the two lists of Set instances overlap, False otherwise
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 # @param lSets1 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78 # @param lSets2 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 def areSetsOverlappingBetweenLists( lSets1, lSets2 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 lSet1Sorted = SetUtils.getSetListSortedByIncreasingMinThenMax(lSets1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 lSet2Sorted = SetUtils.getSetListSortedByIncreasingMinThenMax(lSets2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 i=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 j=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 while i!= len(lSet1Sorted):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 while j!= len(lSet2Sorted) and lSet1Sorted[i].getMin()>lSet2Sorted[j].getMax()\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 and not(lSet1Sorted[i].isOverlapping(lSet2Sorted[j])):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 j+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 if j!= len(lSet2Sorted) and lSet1Sorted[i].isOverlapping(lSet2Sorted[j]):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 return True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 i+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 return False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 areSetsOverlappingBetweenLists = staticmethod( areSetsOverlappingBetweenLists )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 ## Merge all overlapping Set instances between two lists of Set and give the next identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 # @param lSets1 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 # @param lSets2 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 # @param max_id start id value for inserting new Set
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 # @return a new list of the merged Set instances and the next identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 def getListOfMergedSetsAndNextId(lSets1, lSets2, max_id=0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 lSets_merged = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 list2merge = SetUtils.getListOfIdListOfOverlappingSets ( lSets1,lSets2 )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 idlist1 = SetUtils.getDictOfListsWithIdAsKey(lSets1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 idlist2 = SetUtils.getDictOfListsWithIdAsKey(lSets2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 if max_id == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 max_id = max(idlist1.keys()) + 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 for i in list2merge:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 if i == []:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 continue
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 l = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 min_id = max(i)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 for j in i:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 if j>0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 if min_id>j:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118 min_id=j
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119 l.extend(idlist1[j])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 del idlist1[j]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 l.extend(idlist2[j*-1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 del idlist2[j*-1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 l = SetUtils.mergeSetsInList(l)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 SetUtils.changeIdInList(l, min_id)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 lSets_merged.extend(l)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 for id, alist in idlist1.items():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 lSets_merged.extend(alist)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 for id,alist in idlist2.items():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130 SetUtils.changeIdInList(alist,max_id)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 lSets_merged.extend(alist)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 max_id+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 return lSets_merged, max_id
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 getListOfMergedSetsAndNextId = staticmethod ( getListOfMergedSetsAndNextId )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 # ## Concatenate two Set instance lists and give the next identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138 # #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 # # @param lSets1 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 # # @param lSets2 list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 # # @param maxId start id value for inserting new Set
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 # # @return a new list of Set instances and the next identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 # #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 # @staticmethod
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 # def getSetsListOfTwoConcatenatedSetsListAndNextId(lSets1, lSets2, maxId = 0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 # lOutSets = lSets1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 # dId2SetsList2 = SetUtils.getDictOfListsWithIdAsKey(lSets2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 # if maxId == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 # dId2SetsList1 = SetUtils.getDictOfListsWithIdAsKey(lSets1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 # maxId = max(dId2SetsList1.keys())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 # for lSets in dId2SetsList2.values():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 # SetUtils.changeIdInList(lSets, maxId)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 # lOutSets.extend(lSets)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 # maxId += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 # return lOutSets, maxId
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 ## Return the sum of the length of each Set instance in the given list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 # @param lSets: list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
161 def getCumulLength(lSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
162 length = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
163 for i in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
164 length += i.getLength()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
165 return length
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
166
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
167 getCumulLength = staticmethod( getCumulLength )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
168
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
169 ## Return a tuple with min and max coordinates of Set instances in the given list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
170 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
171 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
172 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
173 def getListBoundaries(lSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
174 qmin = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
175 qmax = -1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
176 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
177 if qmin == -1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
178 qmin = iSet.start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
179 qmin = min(qmin, iSet.getMin())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
180 qmax = max(qmax, iSet.getMax())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
181 return (qmin, qmax)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
182
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
183 getListBoundaries = staticmethod( getListBoundaries )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
184
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
185 ## Show Set instances contained in the given list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
186 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
187 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
188 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
189 def showList(lSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
190 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
191 iSet.show()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
192
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
193 showList = staticmethod( showList )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
194
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
195 ## Write Set instances contained in the given list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
196 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
197 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
198 # @param fileName a file name
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
199 # @param mode the open mode of the file '"w"' or '"a"'
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
200 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
201 def writeListInFile(lSets, fileName, mode="w"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
202 fileHandler = open(fileName, mode)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
203 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
204 iSet.write(fileHandler)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
205 fileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
206
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
207 writeListInFile = staticmethod( writeListInFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
208
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
209 ## Split a Set list in several Set lists according to the identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
210 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
211 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
212 # @return a dictionary which keys are identifiers and values Set lists
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
213 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
214 def getDictOfListsWithIdAsKey(lSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
215 dId2SetList = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
216 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
217 if dId2SetList.has_key(iSet.id):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
218 dId2SetList[iSet.id].append(iSet)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
219 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
220 dId2SetList[iSet.id] = [iSet]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
221 return dId2SetList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
222
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
223 getDictOfListsWithIdAsKey = staticmethod( getDictOfListsWithIdAsKey )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
224
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
225
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
226 ## Split a Set list in several Set lists according to the identifier
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
227 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
228 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
229 # @return a dictionary which keys are identifiers and values Set lists
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
230 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
231 def getDictOfListsWithIdAsKeyFromFile( setFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
232 dId2SetList = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
233 setFileHandler = open( setFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
234 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
235 line = setFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
236 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
237 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
238 iSet = Set()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
239 iSet.setFromTuple( line[:-1].split("\t") )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
240 if not dId2SetList.has_key( iSet.id ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
241 dId2SetList[ iSet.id ] = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
242 dId2SetList[ iSet.id ].append( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
243 setFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
244 return dId2SetList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
245
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
246 getDictOfListsWithIdAsKeyFromFile = staticmethod( getDictOfListsWithIdAsKeyFromFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
247
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
248
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
249 ## Return a Map list from the given Set List
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
250 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
251 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
252 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
253 def getMapListFromSetList(lSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
254 lMaps = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
255 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
256 lMaps.append(iSet.set2map())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
257 return lMaps
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
258
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
259 getMapListFromSetList = staticmethod( getMapListFromSetList )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
260
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
261 ## Construct a Set list from a Map list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
262 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
263 # @param lMaps list of Map instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
264 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
265 def getSetListFromMapList(lMaps):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
266 lSets = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
267 c = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
268 for iMap in lMaps:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
269 c += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
270 lSets.append( Set(c, iMap.name, iMap.seqname, iMap.start, iMap.end) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
271 return lSets
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
272
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
273 getSetListFromMapList = staticmethod( getSetListFromMapList )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
274
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
275 ## Merge all overlapping Set instances in a list without considering the identifiers.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
276 # Start by sorting Set instances by their increasing Min coordinate.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
277 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
278 # @return: a new list of the merged Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
279 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
280 def mergeSetsInList(lSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
281 l=[]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
282 if len(lSets)==0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
283 return l
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
284
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
285 lSortedSets = SetUtils.getSetListSortedByIncreasingMinThenInvLength( lSets )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
286
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
287 prev_count = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
288 for iSet in lSortedSets[0:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
289 if prev_count != len(lSortedSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
290 for i in lSortedSets[ prev_count + 1: ]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
291 if iSet.isOverlapping( i ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
292 iSet.merge( i )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
293 IsAlreadyInList = False
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
294 for newSet in l:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
295 if newSet.isOverlapping( iSet ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
296 IsAlreadyInList = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
297 newSet.merge( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
298 l [ l.index( newSet ) ] = newSet
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
299 if not IsAlreadyInList:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
300 l.append( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
301 prev_count += 1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
302 return l
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
303
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
304 mergeSetsInList = staticmethod( mergeSetsInList )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
305
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
306 ## Unjoin a Set list according to another
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
307 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
308 # @param lToKeep: a list of Set instances to keep
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
309 # @param lToUnjoin: a list of Set instances to unjoin
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
310 # @return: lToUnjoin split in several list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
311 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
312 def getSetListUnjoined(lToKeep, lToUnjoin):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
313 lSortedToKeep = SetUtils.getSetListSortedByIncreasingMinThenMax( lToKeep )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
314 lSortedToUnjoin = SetUtils.getSetListSortedByIncreasingMinThenMax( lToUnjoin )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
315 if lSortedToUnjoin == []:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
316 return []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
317 if lSortedToKeep == []:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
318 return [ lSortedToUnjoin ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
319
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
320 i=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
321 resultListSet=[]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
322 while i<len(lSortedToKeep):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
323 j1=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
324 while j1<len(lSortedToUnjoin) and lSortedToKeep[i].getMin() > lSortedToUnjoin[j1].getMax():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
325 j1+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
326 if j1==len(lSortedToUnjoin):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
327 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
328 if j1!=0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
329 resultListSet.append(lSortedToUnjoin[:j1])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
330 del lSortedToUnjoin[:j1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
331 j1=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
332 if i+1==len(lSortedToKeep):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
333 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
334 j2=j1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
335 if j2<len(lSortedToUnjoin) and lSortedToKeep[i+1].getMin() > lSortedToUnjoin[j2].getMax():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
336 while j2<len(lSortedToUnjoin) and lSortedToKeep[i+1].getMin() > lSortedToUnjoin[j2].getMax():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
337 j2+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
338 resultListSet.append(lSortedToUnjoin[j1:j2])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
339 del lSortedToUnjoin[j1:j2]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
340 i+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
341
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
342 if resultListSet!=[] or i == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
343 resultListSet.append(lSortedToUnjoin)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
344 return resultListSet
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
345
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
346 getSetListUnjoined = staticmethod(getSetListUnjoined)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
347
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
348 ## Return new list of Set instances with no duplicate
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
349 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
350 # @param lSets list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
351 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
352 def getSetListWithoutDuplicates( lSets ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
353 if len(lSets) < 2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
354 return lSets
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
355 lSortedSet = SetUtils.getSetListSortedByIncreasingMinThenMax( lSets )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
356 lUniqSet = [ lSortedSet[0] ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
357 for iSet in lSortedSet[1:]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
358 if iSet != lUniqSet[-1]:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
359 lUniqSet.append( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
360 return lUniqSet
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
361
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
362 getSetListWithoutDuplicates = staticmethod( getSetListWithoutDuplicates )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
363
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
364 ## Return a list of Set instances sorted in increasing order according to the Min, then the Max, and finally their initial order
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
365 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
366 # @param lSets: list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
367 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
368 def getSetListSortedByIncreasingMinThenMax( lSets ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
369 return sorted( lSets, key=lambda iSet: ( iSet.getMin(), iSet.getMax() ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
370
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
371 getSetListSortedByIncreasingMinThenMax = staticmethod( getSetListSortedByIncreasingMinThenMax )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
372
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
373 ## Return a list of Set instances sorted in increasing order according to the min, then the inverse of the length, and finally their initial order
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
374 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
375 # @param lSets: list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
376 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
377 def getSetListSortedByIncreasingMinThenInvLength( lSets ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
378 return sorted( lSets, key=lambda iSet: ( iSet.getMin(), 1 / float(iSet.getLength()) ) )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
379
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
380 getSetListSortedByIncreasingMinThenInvLength = staticmethod( getSetListSortedByIncreasingMinThenInvLength )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
381
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
382 ## Return a list of Set instances sorted in increasing order according to the SeqName, then the Name, then the Min, then the Max and finally their initial order
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
383 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
384 # @param lSets: list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
385 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
386 def getSetListSortedBySeqThenRegionThenMinThenMax(lSets):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
387 return sorted(lSets, key=lambda iSet: (iSet.getSeqname(), iSet.getName(), iSet.getMin(), iSet.getMax()))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
388
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
389 getSetListSortedBySeqThenRegionThenMinThenMax = staticmethod(getSetListSortedBySeqThenRegionThenMinThenMax)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
390
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
391 ## Return a list of identifier lists of overlapping Sets from the subject list, according to the reference list
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
392 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
393 # @param lRef list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
394 # @param lSubject list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
395 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
396 def getListOfIdListOfOverlappingSets(lRef,lSubject):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
397 lSortedRef = SetUtils.getSetListSortedByIncreasingMinThenMax( lRef )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
398 lSortedSubject = SetUtils.getSetListSortedByIncreasingMinThenMax( lSubject )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
399
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
400 lOverlappingSet = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
401 lOverlappingSetCounter = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
402
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
403 id2LOverlappingSet_pos = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
404
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
405 i = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
406 j = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
407 while i!= len(lSortedRef):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
408 while j!= len(lSortedSubject) and lSortedRef[i].getMin()>lSortedSubject[j].getMax()\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
409 and not(lSortedRef[i].isOverlapping(lSortedSubject[j])\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
410 and lSortedRef[i].isOnDirectStrand()==lSortedSubject[j].isOnDirectStrand()):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
411 j+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
412 jj=j
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
413 while jj!= len(lSortedSubject) and lSortedRef[i].isOverlapping(lSortedSubject[jj])\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
414 and lSortedRef[i].isOnDirectStrand()==lSortedSubject[jj].isOnDirectStrand():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
415 id1=lSortedRef[i].id
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
416 id2=lSortedSubject[jj].id*-1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
417 if id2LOverlappingSet_pos.has_key(id1) \
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
418 and not id2LOverlappingSet_pos.has_key(id2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
419 lOverlappingSet[id2LOverlappingSet_pos[id1]].append(id2)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
420 id2LOverlappingSet_pos[id2]=id2LOverlappingSet_pos[id1]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
421 if id2LOverlappingSet_pos.has_key(id2) \
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
422 and not id2LOverlappingSet_pos.has_key(id1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
423 lOverlappingSet[id2LOverlappingSet_pos[id2]].append(id1)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
424 id2LOverlappingSet_pos[id1]=id2LOverlappingSet_pos[id2]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
425 if not id2LOverlappingSet_pos.has_key(id2) \
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
426 and not id2LOverlappingSet_pos.has_key(id1):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
427 lOverlappingSet.append([id1,id2])
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
428 id2LOverlappingSet_pos[id1]=lOverlappingSetCounter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
429 id2LOverlappingSet_pos[id2]=lOverlappingSetCounter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
430 lOverlappingSetCounter+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
431 jj+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
432 i+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
433
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
434 return lOverlappingSet
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
435
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
436 getListOfIdListOfOverlappingSets = staticmethod (getListOfIdListOfOverlappingSets)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
437
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
438 ## Return a list of sets without overlapping between two lists of sets
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
439 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
440 # @param lSet1 and lSet2
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
441 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
442 def getListOfSetWithoutOverlappingBetweenTwoListOfSet(lSet1, lSet2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
443 for i in lSet1:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
444 for idx,j in enumerate(lSet2):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
445 n=j.diff(i)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
446 if not n.isEmpty() and n.getLength()>=20:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
447 lSet2.append(n)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
448 lSet2WithoutOverlaps=[]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
449 for i in lSet2:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
450 if not i.isEmpty() and i.getLength()>=20:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
451 lSet2WithoutOverlaps.append(i)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
452 return lSet2WithoutOverlaps
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
453
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
454 getListOfSetWithoutOverlappingBetweenTwoListOfSet = staticmethod (getListOfSetWithoutOverlappingBetweenTwoListOfSet)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
455
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
456 ## Return a Set list from a Set file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
457 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
458 # @param setFile string name of a Set file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
459 # @return a list of Set instances
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
460 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
461 def getSetListFromFile( setFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
462 lSets = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
463 setFileHandler = open( setFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
464 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
465 line = setFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
466 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
467 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
468 iSet = Set()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
469 iSet.setFromString( line )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
470 lSets.append( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
471 setFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
472 return lSets
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
473
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
474 getSetListFromFile = staticmethod( getSetListFromFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
475
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
476
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
477 def convertSetFileIntoMapFile( setFile, mapFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
478 setFileHandler = open( setFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
479 mapFileHandler = open( mapFile, "w" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
480 iSet = Set()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
481 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
482 line = setFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
483 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
484 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
485 iSet.setFromString( line )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
486 iMap = iSet.getMapInstance()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
487 iMap.write( mapFileHandler )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
488 setFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
489 mapFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
490
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
491 convertSetFileIntoMapFile = staticmethod( convertSetFileIntoMapFile )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
492
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
493
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
494 def getDictOfListsWithSeqnameAsKey( lSets ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
495 dSeqnamesToSetList = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
496 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
497 if not dSeqnamesToSetList.has_key( iSet.seqname ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
498 dSeqnamesToSetList[ iSet.seqname ] = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
499 dSeqnamesToSetList[ iSet.seqname ].append( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
500 return dSeqnamesToSetList
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
501
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
502 getDictOfListsWithSeqnameAsKey = staticmethod( getDictOfListsWithSeqnameAsKey )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
503
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
504
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
505 def filterOnLength( lSets, minLength=0, maxLength=10000000000 ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
506 if minLength == 0 and maxLength == 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
507 return lSets
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
508 lFiltered = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
509 for iSet in lSets:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
510 if minLength <= iSet.getLength() <= maxLength:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
511 lFiltered.append( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
512 return lFiltered
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
513
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
514 filterOnLength = staticmethod( filterOnLength )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
515
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
516
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
517 def getListOfNames( setFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
518 lNames = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
519 setFileHandler = open( setFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
520 iSet = Set()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
521 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
522 line = setFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
523 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
524 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
525 iSet.setFromTuple( line[:-1].split("\t") )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
526 if iSet.name not in lNames:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
527 lNames.append( iSet.name )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
528 setFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
529 return lNames
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
530
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
531 getListOfNames = staticmethod( getListOfNames )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
532
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
533
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
534 def getDictOfDictsWithNamesThenIdAsKeyFromFile( setFile ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
535 dNames2DictsId = {}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
536 setFileHandler = open( setFile, "r" )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
537 while True:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
538 line = setFileHandler.readline()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
539 if line == "":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
540 break
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
541 iSet = Set()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
542 iSet.setFromTuple( line[:-1].split("\t") )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
543 if not dNames2DictsId.has_key( iSet.name ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
544 dNames2DictsId[ iSet.name ] = { iSet.id: [ iSet ] }
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
545 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
546 if not dNames2DictsId[ iSet.name ].has_key( iSet.id ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
547 dNames2DictsId[ iSet.name ][ iSet.id ] = [ iSet ]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
548 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
549 dNames2DictsId[ iSet.name ][ iSet.id ].append( iSet )
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
550 setFileHandler.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
551 return dNames2DictsId
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
552
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
553 getDictOfDictsWithNamesThenIdAsKeyFromFile = staticmethod( getDictOfDictsWithNamesThenIdAsKeyFromFile )