annotate seg2matrix/CGData/SegToMatrix.py @ 31:ab20c0d04f4a

add seg2matrix tool
author jingchunzhu
date Fri, 24 Jul 2015 13:10:11 -0700
parents
children 8ef79bd0be9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
1
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
2
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
3 import ctypes
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
4 import os
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
5
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
6 base = os.path.dirname(os.path.abspath(__file__))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
7 libFile = base + "/CsegToMatrix.so"
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
8 if os.path.exists(libFile):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
9 segLib = ctypes.cdll.LoadLibrary(libFile)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
10
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
11
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
12 def seg_to_matrix(seg_handle, out_handle):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
13 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
14 Turn a segment file into a segmented matrix.
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
15
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
16 seg_handle -- CGData.GenomicSegment object
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
17 out_handle -- File handle to write to
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
18 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
19 s = segLib.new_segment()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
20 t = segLib.new_target_set()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
21 for target in seg_handle.get_key_list():
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
22 for seg in seg_handle.get_by(target):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
23 segLib.add_segment(s, t, target, seg.chrom, seg.chrom_start, seg.chrom_end, ctypes.c_float(seg.value))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
24
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
25 def printback(s):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
26 out_handle.write(s)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
27 return 0
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
28
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
29 printbackTYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
30 segLib.print_matrix(s, t, printbackTYPE(printback))