コード例 #1
0
def window_core(obj, chrom):
    bamfile = pysam.AlignmentFile(obj.bamname, 'rb')
    chr_length = chrom[1]
    chrom = chrom[0]
    print chrom
        
    pos_vect = np.zeros((chr_length / obj.window_size,))
    
    read_mid = 0
    
    # Loop through BAM reads, adding midpoints to position vector
    for read in bamfile.fetch(chrom):
        read_mid = sam.read_mid_compute(obj, read)
        if read_mid < 0: continue
        read_mid_index = read_mid / obj.window_size
        if read_mid_index <= len(pos_vect) - 1:
            pos_vect[read_mid_index] = pos_vect[read_mid_index] + 1
    
    # Pseudocount 0 windows
    if obj.pseudo:
        pos_vect[pos_vect==0] = 1
    
    # Normalize by Reads per million and by reads per kilobase
    #window_correct = 1e6 * 1e3 / (float(obj.window_size) * obj.nreads)

    # Normalize by Reads per million
    window_correct = 1e6 * obj.nreads
    pos_vect = window_correct * pos_vect

    if obj.smooth > 0:
        pos_vect = signal_utils.smooth(pos_vect, obj.smooth, window="flat")

    if not obj.output_type == "none":
        write_values(obj, pos_vect, chrom, chr_length)
コード例 #2
0
ファイル: BAMtoWindow.py プロジェクト: balodhi/seqAnalysis
def window_core(obj, chrom):

    bamfile = pysam.Samfile(obj.bamname, 'rb')
    #pdb.set_trace()
    chr_length = chrom[1]
    chrom = chrom[0]
    print chrom

    pos_vect = np.zeros((chr_length / obj.window_size, ))

    read_mid = 0

    # Loop through BAM reads, adding midpoints to position vector
    for read in bamfile.fetch(chrom):
        read_mid = sam.read_mid_compute(obj, read)
        if read_mid < 0: continue
        read_mid_index = read_mid / obj.window_size
        if read_mid_index <= len(pos_vect) - 1:
            pos_vect[read_mid_index] = pos_vect[read_mid_index] + 1

    # Pseudocount 0 windows
    if obj.pseudo:
        pos_vect[pos_vect == 0] = 1

    # Normalize by Reads per million and by reads per kilobase
    window_correct = 1e6 * 1e3 / (float(obj.window_size) * obj.nreads)
    pos_vect = window_correct * pos_vect

    if obj.smooth > 0:
        pos_vect = signal_utils.smooth(pos_vect, obj.smooth, window="flat")

    if not obj.output_type == "none":
        write_values(obj, pos_vect, chrom, chr_length)
コード例 #3
0
 def run(self):
     out_track_name = self.in_track._v_name + "_smooth" + str(self.smooth)
     print out_track_name
     #pdb.set_trace()
     test = tutil.checkIfNodeExists(self.out_track, out_track_name, True, False)
     if test: return
     
     for chrom in self.in_track._f_iterNodes():
         chr_name = chrom._v_name
         print chr_name
         if chr_name == "unknown": continue
         track_chr = self.in_track._f_getChild(chr_name)
         
         out_track_chr = signal_utils.smooth(track_chr[:], self.smooth, window="flat")
         
         self.out_track.createArray("/" + out_track_name, chr_name, out_track_chr)
         for name in track_chr._v_attrs._f_list():
             self.out_track.setNodeAttr("/" + "/".join([out_track_name, chr_name]), name, track_chr._v_attrs[name])
コード例 #4
0
def window_core(obj, chrom):    
    
    bamfile = pysam.Samfile(obj.bamname, 'rb')
    #pdb.set_trace()
    chr_length = chrom[1]
    chrom = chrom[0]
    print chrom
    
    # Normalize by Reads per million and by reads per kilobase
    window_correct = 1e6 * 1e3 / (float(obj.window_size) * obj.nreads)
        
    pos_vect = np.zeros((chr_length / obj.window_size,))
    
    read_mid = 0
    
    for read in bamfile.fetch(chrom):
        read_mid = sam.read_mid_compute(obj, read)
        if read_mid < 0: continue

        read_mid_index = read_mid / obj.window_size
        if read_mid_index <= len(pos_vect) - 1:
            pos_vect[read_mid_index] = pos_vect[read_mid_index] + 1
    
    out = "fixedStep chrom={0} start={1} step={2} span={2}\n".format(chrom, "1", obj.window_size)
    obj.wigfile.write(out)
    
    if obj.no_norm:
        for val in pos_vect:
            if obj.pseudo and val == 0: val = 1
            obj.wigfile.write(str(val) + "\n")

    pos_vect = window_correct * pos_vect
    #pdb.set_trace()
    if obj.smooth > 0:
        pos_vect = signal_utils.smooth(pos_vect, obj.smooth, window="flat")
    
    if not obj.no_output:    
        if obj.pseudo: pos_vect[pos_vect==0] = 1
        if not obj.no_norm:
             pos_vect = window_correct * pos_vect
             
        for val in pos_vect:
             obj.wigfile.write(str(val) + "\n")