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