def parse_segments(vcffile):
    """ Extract all copy number segments from a CANVAS file

    VCF line looks like:
    chr1    788879  Canvas:GAIN:chr1:788880-821005  N       <CNV>   2       q10
    SVTYPE=CNV;END=821005;CNVLEN=32126      RC:BC:CN:MCC    157:4:3:2
    """
    from cStringIO import StringIO
    from cyvcf2 import VCF

    output = StringIO()
    for v in VCF(vcffile):
        chrom = v.CHROM
        start = v.start
        end = v.INFO.get('END') - 1
        cn, = v.format('CN')[0]
        print("\t".join(str(x) for x in (chrom, start, end, cn)), file=output)

    beds = BedTool(output.getvalue(), from_string=True)
    return beds
Extract all copy number segments from a CANVAS file

    VCF line looks like:
    chr1    788879  Canvas:GAIN:chr1:788880-821005  N       <CNV>   2       q10
    SVTYPE=CNV;END=821005;CNVLEN=32126      RC:BC:CN:MCC    157:4:3:2