annotate seg2matrix/CGData/SegToMatrix.py @ 35:8ef79bd0be9a

modify
author jingchunzhu <jingchunzhu@gmail.com>
date Fri, 24 Jul 2015 16:14:36 -0700
parents ab20c0d04f4a
children 59dbe857f5d4
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
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
13
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
14 def seg_to_matrix(seg_handle, out_handle):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
15 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
16 Turn a segment file into a segmented matrix.
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
17
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
18 seg_handle -- CGData.GenomicSegment object
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
19 out_handle -- File handle to write to
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
20 """
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
21 s = segLib.new_segment()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
22 t = segLib.new_target_set()
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
23 for target in seg_handle.get_key_list():
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
24 for seg in seg_handle.get_by(target):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
25 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
26
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
27 def printback(s):
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
28 out_handle.write(s)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
29 return 0
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
30
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
31 printbackTYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_char_p)
ab20c0d04f4a add seg2matrix tool
jingchunzhu
parents:
diff changeset
32 segLib.print_matrix(s, t, printbackTYPE(printback))