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
|
54
|
13 def seg_to_matrix(seg_handle, out_handle, NORMAL_CNV):
|
31
|
14 """
|
|
15 Turn a segment file into a segmented matrix.
|
|
16
|
|
17 seg_handle -- CGData.GenomicSegment object
|
|
18 out_handle -- File handle to write to
|
|
19 """
|
|
20 s = segLib.new_segment()
|
|
21 t = segLib.new_target_set()
|
|
22 for target in seg_handle.get_key_list():
|
|
23 for seg in seg_handle.get_by(target):
|
|
24 segLib.add_segment(s, t, target, seg.chrom, seg.chrom_start, seg.chrom_end, ctypes.c_float(seg.value))
|
|
25
|
|
26 def printback(s):
|
|
27 out_handle.write(s)
|
|
28 return 0
|
|
29
|
|
30 printbackTYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
|
54
|
31 segLib.print_matrix(s, t, printbackTYPE(printback), NORMAL_CNV)
|