18
|
1 from copy import deepcopy
|
|
2 from commons.core.sql.TablePathAdaptator import TablePathAdaptator
|
|
3 from commons.core.coord.PathUtils import PathUtils
|
|
4 from commons.core.coord.Map import Map
|
|
5 from commons.core.coord.Path import Path
|
|
6 import os
|
|
7
|
|
8 class ConvPathChr2Chunk(object):
|
|
9
|
|
10 def __init__(self, db, table, chunk_table, outtable):
|
|
11 self._tablename = table
|
|
12 self._chunk_table = chunk_table
|
|
13 self._db = db
|
|
14 self._outtable = outtable
|
|
15
|
|
16 def convert(self):
|
|
17 """
|
|
18 Convert a 'path' table format.
|
|
19 """
|
|
20 temp_file = str(os.getpid()) + ".on_chunk"
|
|
21 fout = open(temp_file,'w')
|
|
22
|
|
23 str_mask = "SELECT * FROM "+\
|
|
24 self._chunk_table + " WHERE chr='%s' AND ("+\
|
|
25 "(%d BETWEEN LEAST(start,end) AND GREATEST(start,end))"+\
|
|
26 " OR (%d BETWEEN LEAST(start,end) AND GREATEST(start,end))"+\
|
|
27 " OR (%d <= LEAST(start,end) AND %d >= GREATEST(start,end)));"
|
|
28
|
|
29 iTPA = TablePathAdaptator(self._db, self._tablename)
|
|
30 path_num_list = iTPA.getIdList()
|
|
31
|
|
32 for path_num in path_num_list:
|
|
33 slist = iTPA.getPathListFromId(path_num)
|
|
34 for r in slist:
|
|
35 r_min, r_max = PathUtils.getQueryMinMaxFromPathList([r])
|
|
36 sql_cmd = str_mask%(r.range_query.seqname,r_min,r_max,r_min,r_max)
|
|
37 self._db.execute(sql_cmd)
|
|
38 res = self._db.fetchall()
|
|
39 for i in res:
|
|
40 chunk = Map(i[0],i[1],int(i[2]),int(i[3]))
|
|
41
|
|
42 new_r = Path()
|
|
43 new_r = deepcopy(r)
|
|
44 new_r.range_query.seqname = chunk.name
|
|
45
|
|
46 if (r.range_query.start > chunk.start and r.range_query.start < chunk.end):
|
|
47 new_r.range_query.start = r.range_query.start - chunk.start + 1
|
|
48 if (r.range_query.end > chunk.start and r.range_query.end < chunk.end):
|
|
49 new_r.range_query.end = r.range_query.end - chunk.start + 1
|
|
50
|
|
51 if r.range_query.isOnDirectStrand():
|
|
52 if r.range_query.start <= chunk.start:
|
|
53 new_r.range_query.start = 1
|
|
54 if r.range_query.end >= chunk.end:
|
|
55 new_r.range_query.end = chunk.end - chunk.start + 1
|
|
56 else:
|
|
57 if r.range_query.end <= chunk.start:
|
|
58 new_r.range_query.end = 1
|
|
59 if r.range_query.start >= chunk.end:
|
|
60 new_r.range_query.start = chunk.end - chunk.start + 1
|
|
61
|
|
62 new_r.write(fout)
|
|
63
|
|
64 fout.close()
|
|
65
|
|
66 self._db.createTable(self._outtable, "path", temp_file)
|
|
67
|
|
68 os.remove(temp_file) |