comparison commons/pyRepetUnit/convCoord/PathChunkConnector.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
children
comparison
equal deleted inserted replaced
30:5677346472b5 31:0ab839023fe4
1 from pyRepet.coord.Map import Map
2 import commons.core.sql.TablePathAdaptator
3
4
5 ## Connect overlapping chunks in a single fragment
6 #
7 class PathChunkConnector( object):
8
9 def __init__(self, mapFileName, db, table, verbose):
10
11 self._verbose = verbose
12 self._chunk = self._getChunkDictFromMapFileForConnectPathChunks( mapFileName )
13 self._tablePathAdaptator = commons.core.sql.TablePathAdaptator.TablePathAdaptator( db, table )
14
15 def run (self):
16 for num_chunk in xrange(1,len(self._chunk.keys())):
17 chunkName = "chunk"+str(num_chunk)
18 if self._verbose > 1:
19 print chunkName
20 next_chunkName="chunk"+str(num_chunk+1)
21
22 if next_chunkName not in self._chunk.keys():
23 break
24
25 start=self._chunk[chunkName][2]
26 end=self._chunk[next_chunkName][1]
27
28 if self._chunk[chunkName][0] == self._chunk[next_chunkName][0]:
29 lpath=self._tablePathAdaptator.getPathListIncludedInQueryCoord(self._chunk[chunkName][0],start,end)
30
31 if self._verbose > 1:
32 print "----------"
33
34 lpath.sort()
35 chg_path_id={}
36 pathnum_to_ins=[]
37 pathnum_to_del=[]
38
39 self._createDirectAndReversePaths(lpath)
40
41 self._mergeDirectPaths(chg_path_id, pathnum_to_ins, pathnum_to_del)
42
43 if self._verbose > 1:
44 print "..........."
45
46 self._mergeReversePaths(chg_path_id, pathnum_to_ins, pathnum_to_del)
47
48 if self._verbose > 1:
49 print "..........."
50 print pathnum_to_del
51
52 self._tablePathAdaptator.deleteFromIdList(pathnum_to_del)
53
54 if self._verbose > 1:
55 print pathnum_to_ins
56
57 self._tablePathAdaptator.deleteFromIdList(pathnum_to_ins)
58
59 self._insertDirectPaths(chg_path_id, pathnum_to_ins)
60
61 self._insertReversePaths(chg_path_id, pathnum_to_ins)
62
63
64 def _createDirectAndReversePaths(self, lpath):
65 self._dpath = []
66 self._rpath = []
67 for i in lpath:
68 if i.range_query.isOnDirectStrand() and i.range_subject.isOnDirectStrand():
69 self._dpath.append(i)
70 else:
71 self._rpath.append(i)
72
73 def _insertDirectPaths (self, chg_path_id, pathnum_to_ins):
74 self._insertPaths(chg_path_id, pathnum_to_ins, self._dpath)
75
76 def _insertReversePaths (self, chg_path_id, pathnum_to_ins):
77 self._insertPaths(chg_path_id, pathnum_to_ins, self._rpath)
78
79 def _insertPaths(self, chg_path_id, pathnum_to_ins, paths2Insert):
80 for i in paths2Insert:
81 if chg_path_id.has_key(i.id):
82 i.id = chg_path_id[i.id]
83
84 if self._verbose > 1:
85 i.show()
86
87 if i.id in pathnum_to_ins:
88 self._tablePathAdaptator.insert(i)
89 if self._verbose > 1:
90 print "--> inserted!"
91
92 if self._verbose > 1:
93 print "=========="
94
95 def _mergeDirectPaths(self, chg_path_id, pathnum_to_ins, pathnum_to_del):
96 self._mergePaths(chg_path_id, pathnum_to_ins, pathnum_to_del, self._dpath)
97
98 def _mergeReversePaths(self, chg_path_id, pathnum_to_ins, pathnum_to_del):
99 self._mergePaths(chg_path_id, pathnum_to_ins, pathnum_to_del, self._rpath)
100
101 def _mergePaths(self, chg_path_id, pathnum_to_ins, pathnum_to_del, dpath):
102 x = 0
103 while x < len(dpath) - 1:
104 x = x + 1
105 if self._verbose > 1:
106 print "++++"
107 dpath[x - 1].show()
108 dpath[x].show()
109
110 if dpath[x - 1].canMerge(dpath[x]):
111 chg_path_id[dpath[x].id] = dpath[x - 1].id
112 if dpath[x - 1].id not in pathnum_to_ins:
113 pathnum_to_ins.append(dpath[x - 1].id)
114
115 if dpath[x].id not in pathnum_to_del:
116 pathnum_to_del.append(dpath[x].id)
117
118 dpath[x - 1].merge(dpath[x])
119 del dpath[x]
120 x = x - 1
121 if self._verbose > 1:
122 print "--> merged"
123
124 def _getChunkDictFromMapFileForConnectPathChunks(self, mapFileName):
125 mapDict = {}
126 mapFile = open(mapFileName)
127 mapInstance = Map()
128 while True:
129 if not mapInstance.read(mapFile):
130 break
131 mapDict[mapInstance.name] = (mapInstance.seqname, mapInstance.start, mapInstance.end)
132
133 mapFile.close()
134 return mapDict