31
|
1
|
|
2
|
|
3 import ctypes
|
|
4 import os
|
|
5
|
|
6 base = os.path.dirname(os.path.abspath(__file__))
|
|
7 libFile = base + "/CsegToMatrix.so"
|
|
8 if os.path.exists(libFile):
|
|
9 segLib = ctypes.cdll.LoadLibrary(libFile)
|
|
10
|
|
11
|
|
12 def seg_to_matrix(seg_handle, out_handle):
|
|
13 """
|
|
14 Turn a segment file into a segmented matrix.
|
|
15
|
|
16 seg_handle -- CGData.GenomicSegment object
|
|
17 out_handle -- File handle to write to
|
|
18 """
|
|
19 s = segLib.new_segment()
|
|
20 t = segLib.new_target_set()
|
|
21 for target in seg_handle.get_key_list():
|
|
22 for seg in seg_handle.get_by(target):
|
|
23 segLib.add_segment(s, t, target, seg.chrom, seg.chrom_start, seg.chrom_end, ctypes.c_float(seg.value))
|
|
24
|
|
25 def printback(s):
|
|
26 out_handle.write(s)
|
|
27 return 0
|
|
28
|
|
29 printbackTYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
|
|
30 segLib.print_matrix(s, t, printbackTYPE(printback))
|