simplify output msgs of callvar
[MACS.git] / MACS3 / IO / BedGraphIO.pyx
blobd8ec0ca13642859754bc45c040145930fbf5ade1
1 # cython: language_level=3
2 # cython: profile=True
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
9 the distribution).
10 """
12 # ------------------------------------
13 # python modules
14 # ------------------------------------
15 import io
17 from MACS3.Signal.BedGraph import bedGraphTrackI
19 # ------------------------------------
20 # constants
21 # ------------------------------------
23 # ------------------------------------
24 # C lib
25 # ------------------------------------
27 from libc.stdio cimport *
28 from libc.stdlib cimport *
30 # ------------------------------------
31 # Misc functions
32 # ------------------------------------
34 # ------------------------------------
35 # Classes
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.
50 """
51 cdef:
52 str bedGraph_filename
54 def __init__ ( self, str bedGraph_filename ):
55 """f must be a filename or a file handler.
57 """
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:
66 chr1 100 200 1
67 chr1 250 350 2
69 Then the region chr1:200..250 should be filled with
70 baseline_value. Default of baseline_value is 0.
71 """
72 cdef bytes i
74 data = bedGraphTrackI(baseline_value=baseline_value)
75 add_func = data.add_loc
76 # python open file
77 bedGraph_file = open( self.bedGraph_filename, "rb" )
79 for i in bedGraph_file:
80 if i.startswith(b"track"):
81 continue
82 elif i.startswith(b"#"):
83 continue
84 elif i.startswith(b"browse"):
85 continue
86 else:
87 fs = i.split()
88 add_func(fs[0],atoi(fs[1]),atoi(fs[2]),atof(fs[3]))
90 bedGraph_file.close()
91 return data