annotate seg2matrix/CGData/SegToMatrix.py @ 54:59dbe857f5d4

introduce normal_CNV parameter
author jingchunzhu
date Thu, 17 Sep 2015 22:03:04 -0700
parents 8ef79bd0be9a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
1 import ctypes
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
2 import os
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
3
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
4 base = os.path.dirname(os.path.abspath(__file__))
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
5 libFile = base + "/CsegToMatrix.so"
35
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
6
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
7 os.system("rm -f "+ base+"/CsegToMatrix.so")
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
8 os.system("g++ -fPIC --shared "+ base +"/CsegToMatrix.cc -o "+ base +"/CsegToMatrix.so")
jingchunzhu <jingchunzhu@gmail.com>
parents: 31
diff changeset
9
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
10 if os.path.exists(libFile):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
11 segLib = ctypes.cdll.LoadLibrary(libFile)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
12
54
59dbe857f5d4 introduce normal_CNV parameter
jingchunzhu
parents: 35
diff changeset
13 def seg_to_matrix(seg_handle, out_handle, NORMAL_CNV):
31
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
14 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
15 Turn a segment file into a segmented matrix.
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
16
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
17 seg_handle -- CGData.GenomicSegment object
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
18 out_handle -- File handle to write to
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
19 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
20 s = segLib.new_segment()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
21 t = segLib.new_target_set()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
22 for target in seg_handle.get_key_list():
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
23 for seg in seg_handle.get_by(target):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
24 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
25
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
26 def printback(s):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
27 out_handle.write(s)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
28 return 0
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
29
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
30 printbackTYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
54
59dbe857f5d4 introduce normal_CNV parameter
jingchunzhu
parents: 35
diff changeset
31 segLib.print_matrix(s, t, printbackTYPE(printback), NORMAL_CNV)