Beispiel #1
0
 def test_fileformat_ps(self):
     ''' test we can pick ps output format
     '''
     temp = tempfile.NamedTemporaryFile(suffix='.ps')
     ext, surf = fileformat(temp.name, 50, 100)
     self.assertEqual(ext, 'ps')
     self.assertEqual(type(surf), cairo.PSSurface)
Beispiel #2
0
    def test_fileformat_svg(self):
        ''' test we can pick svg output format
        '''

        temp = tempfile.NamedTemporaryFile(suffix='.svg')
        ext, surf = fileformat(temp.name, 50, 100)
        self.assertEqual(ext, 'svg')
        self.assertEqual(type(surf), cairo.SVGSurface)
Beispiel #3
0
    def test_fileformat_png(self):
        ''' test we can pick png output format
        '''
        temp = tempfile.NamedTemporaryFile(suffix='.png')
        ext, surf = fileformat(temp.name, 50, 100)
        self.assertEqual(ext, 'png')
        self.assertEqual(type(surf), cairo.ImageSurface)

        # check that we've set the height and width correctly
        self.assertEqual(surf.get_height(), 100)
        self.assertEqual(surf.get_width(), 50)
Beispiel #4
0
def covplot(seqfiles, chrom, start, end, fastafile, out=None):
    '''
    '''
    if type(seqfiles) is not list:
        seqfiles = [seqfiles]

    chrom = str(chrom)
    with pysam.FastaFile(fastafile) as handle:
        reference = handle.fetch(start=start, end=end, region=chrom)
        ref = reference

    axis_offset = 75
    cov_height = 200
    height = len(seqfiles) * cov_height + (len(seqfiles) -
                                           1) * 50 + axis_offset

    out_type, surface = fileformat(out,
                                   width=(end - start) * 10,
                                   height=height)
    context = cairo.Context(surface)

    plot_axis(context, start, end, axis_offset - 10)
    plot_grid(context, start, end, axis_offset, height)

    for seqfile in seqfiles:
        seq = pysam.AlignmentFile(seqfile, 'rb')
        coords = OrderedDict({0: -1e9})
        reps = (parse_read(x, coords, ref, start)
                for x in seq.fetch(chrom, start, end))

        plot_coverage(context, get_coverage(reps), axis_offset, start,
                      (end - start) * 10, cov_height)
        axis_offset += cov_height

        if seqfiles.index(seqfile) < len(seqfiles) - 1:
            insert_spacer(context, coords, start, end)
            axis_offset += 50

    context.save()

    if out_type == 'png':
        surface.write_to_png(out)
    elif out_type is None:
        return surface.write_to_png()
Beispiel #5
0
 def test_fileformat_unknown(self):
     ''' test we raise error for unknown output format
     '''
     with self.assertRaises(AssertionError):
         fileformat('a.zzz', 50, 100)
Beispiel #6
0
def seqplot(seqfiles,
            chrom,
            start,
            end,
            fastafile,
            out=None,
            by_strand=False,
            merge_delta=0.05):
    ''' the plotting function
    
    Args:
        seqfiles: list of paths to sequence files
        chrom: chromosome to fetch reads from
        start: start nucleotide of plotting window.
        end: end nucleotide of plotting window.
        fastafile: path to reference FASTA file.
        out: path to write image file to, or None to return bytes-encoded png
        by_strand: whether to shade reads by strand
        merge_delta: difference allowed between neighboring qualities before not merging
    
    Returns:
        None, or if out is None, returns image plot as bytes-encoded png
    '''

    if type(seqfiles) is not list:
        seqfiles = [seqfiles]

    chrom = str(chrom)
    with pysam.FastaFile(fastafile) as handle:
        reference = handle.fetch(start=start, end=end, region=chrom)
        ref = reference

    axis_offset = 75
    height = get_height(seqfiles, chrom, start, end, axis_offset)

    out_type, surface = fileformat(out,
                                   width=(end - start) * 10,
                                   height=height)
    context = cairo.Context(surface)

    depths = [axis_offset]
    for seqfile in seqfiles:
        seq = pysam.AlignmentFile(seqfile, 'r')
        coords = OrderedDict({max(depths): -1e9})
        reps = (parse_read(x, coords, ref, start)
                for x in seq.fetch(chrom, start, end))

        _plot(context, reps, start, end, axis_offset, height, reference,
              by_strand, merge_delta)
        reference = None  # don't plot the reference in subsequent BAMs

        if seqfiles.index(seqfile) < len(seqfiles) - 1:
            insert_spacer(context, coords, start, end)

        depths.append(max(coords))

    context.save()

    if out_type == 'png':
        surface.write_to_png(out)
    elif out_type is None:
        return surface.get_data()
Beispiel #7
0
def seqplot(seqfiles, chrom, start, end, fastafile, out=None, by_strand=False, add_names=False, verbose=False):
    ''' the plotting function
    
    Args:
        seqfiles: list of paths to sequence files
        chrom: chromosome to fetch reads from
        start: start nucleotide of plotting window.
        end: end nucleotide of plotting window.
        fastafile: path to reference FASTA file.
        out: path to write image file to, or None to return bytes-encoded png
        by_strand: whether to shade reads by strand
        add_names: whether to print the name of the reference and the reads next to the sequence
        verbose: whether to print additional information to STDOUT
    
    Returns:
        None, or if out is None, returns image plot as bytes-encoded png
    '''
    if type(seqfiles) is not list:
        seqfiles = [seqfiles]
    
    if add_names:
        extraspace = 150
    else:
        extraspace = 0
    
    if verbose:
        print("FILE = {}\nREGION = {}:{}-{}".format(fastafile, chrom, start, end))
        
    chrom = str(chrom)
        
    with pysam.FastaFile(fastafile) as handle:
        reference = handle.fetch(chrom, start, end)
        ref = reference
    
    axis_offset = 75
    height = get_height(seqfiles, chrom, start, end, axis_offset)
    
    out_type, surface = fileformat(out, width=(end - start) * 10 + extraspace, height=height)
    context = cairo.Context(surface)
    
    depths = [axis_offset]
    right_border = 0
    for seqfile in seqfiles:
        seq = pysam.AlignmentFile(seqfile, 'rb')
        refname = seq.get_reference_name(0)
        coords = OrderedDict({max(depths): -1e9})
        reps = ( parse_read(x, coords, ref, start) for x in seq.fetch(chrom, start, end) )

        right_border = _plot(context, reps, start, end, axis_offset, height, right_border, reference, by_strand, refname)
        
        reference = None # don't plot the reference in subsequent BAMs
        
        if seqfiles.index(seqfile) < len(seqfiles) - 1:
            insert_spacer(context, coords, start, end)
        
        depths.append(max(coords))
    
    context.save()
    
    if out_type == 'png':
        surface.write_to_png(out)
    elif out_type is None:
        return surface.write_to_png()