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