annotate commons/pyRepetUnit/convCoord/ConvPathChr2Chunk.py @ 31:0ab839023fe4

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