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