1 # cython: language_level=3
3 # Time-stamp: <2020-12-03 11:46:58 Tao Liu>
5 """Module Description: IO Module for bedGraph file
7 This code is free software; you can redistribute it and/or modify it
8 under the terms of the BSD License (see the file LICENSE included with
12 # ------------------------------------
14 # ------------------------------------
17 from MACS3
.Signal
.BedGraph
import bedGraphTrackI
19 # ------------------------------------
21 # ------------------------------------
23 # ------------------------------------
25 # ------------------------------------
27 from libc
.stdio cimport
*
28 from libc
.stdlib cimport
*
30 # ------------------------------------
32 # ------------------------------------
34 # ------------------------------------
36 # ------------------------------------
38 cdef class bedGraphIO
:
39 """File Parser Class for bedGraph File.
41 There are two assumptions in my bedGraphTrackI object:
43 1. Continuous: the next region should be after the previous one
44 unless they are on different chromosomes;
46 2. Non-overlapping: the next region should never have overlaps
47 with preceding region.
49 If any of the above two criteria is violated, parsering will fail.
54 def __init__
( self, str bedGraph_filename
):
55 """f must be a filename or a file handler.
58 self.bedGraph_filename
= bedGraph_filename
60 def build_bdgtrack
(self, double baseline_value
=0):
61 """Use this function to return a bedGraphTrackI object.
63 baseline_value is the value to fill in the regions not defined
64 in bedGraph. For example, if the bedGraph is like:
69 Then the region chr1:200..250 should be filled with
70 baseline_value. Default of baseline_value is 0.
74 data
= bedGraphTrackI
(baseline_value
=baseline_value
)
75 add_func
= data
.add_loc
77 bedGraph_file
= open( self.bedGraph_filename
, "rb" )
79 for i
in bedGraph_file
:
80 if i
.startswith
(b
"track"):
82 elif i
.startswith
(b
"#"):
84 elif i
.startswith
(b
"browse"):
88 add_func
(fs
[0],atoi
(fs
[1]),atoi
(fs
[2]),atof
(fs
[3]))