def make_feature_bed(gtf, feature, out=None):
    bed_lines = []
    with open(gtf) as f:
        line = f.readline().strip()
        while line != '':
            if line[0] != '
                line = line.split('\t')
                if line[2] == feature:
                    chrom = line[0]
                    start = str(int(line[3]) - 1)
                    end = line[4]
                    if feature == 'gene':
                        name = line[8].split(';')[0].split(' ')[1].strip('"')
                    else:
                        name = line[8].split(';')[1].split(' ')[2].strip('"')
                    strand = line[6]
                    bed_lines.append('\t'.join([chrom, start, end, name, '.',
                                                strand]) + '\n')
            line = f.readline().strip()
    bt = pbt.BedTool(''.join(bed_lines), from_string=True)
    bt = bt.sort()
    if out:
        bt.saveas(out)
    return bt