|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Preprocess GENCODE basic GTF to extract canonical protein-coding transcripts for functional consequence annotation. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import gzip |
| 9 | + |
| 10 | + |
| 11 | +CHROM_FIELD = 0 |
| 12 | +ELEMENT_FIELD = 2 |
| 13 | +ATTRIBUTES_FIELD = 8 |
| 14 | +TRANSCRIPT_TYPES = {"protein_coding", "nonsense_mediated_decay"} |
| 15 | +CANONICAL = {"MANE_Plus_Clinical", "MANE_Select", "Ensembl_canonical"} |
| 16 | + |
| 17 | + |
| 18 | +# Flexibly open .gz or uncompressed file to read |
| 19 | +def _open(filename): |
| 20 | + if filename.endswith(".gz"): |
| 21 | + return gzip.open(filename, 'rt') |
| 22 | + else: |
| 23 | + return open(filename, 'r') |
| 24 | + |
| 25 | + |
| 26 | +# Extract transcript type and canonical status |
| 27 | +def parse_attributes(field): |
| 28 | + # format: key1 "value1"; key2 "value2"; |
| 29 | + # keys may be repeated so cannot convert directly to dictionary |
| 30 | + attributes_list = [tuple(x.replace('"', '').split(' ')) for x in field.rstrip(";").split("; ")] |
| 31 | + protein = False |
| 32 | + canonical = False |
| 33 | + for key, val in attributes_list: |
| 34 | + if key == "tag" and val in CANONICAL: |
| 35 | + canonical = True |
| 36 | + elif key == "transcript_type" and val in TRANSCRIPT_TYPES: |
| 37 | + protein = True |
| 38 | + return protein, canonical |
| 39 | + |
| 40 | + |
| 41 | +def process(gtf, outfile): |
| 42 | + with _open(gtf) as inp, open(outfile, 'w') as out: |
| 43 | + gene_line = "" |
| 44 | + for line in inp: |
| 45 | + if line.startswith("#"): |
| 46 | + continue |
| 47 | + fields = line.rstrip('\n').split('\t') |
| 48 | + |
| 49 | + # Drop mitochondria |
| 50 | + if fields[CHROM_FIELD] == 'chrM': |
| 51 | + continue |
| 52 | + |
| 53 | + # Store gene line to print if transcript is eligible |
| 54 | + if fields[ELEMENT_FIELD] == "gene": |
| 55 | + gene_line = line |
| 56 | + continue |
| 57 | + |
| 58 | + # Select protein-coding and canonical transcripts only |
| 59 | + protein, canonical = parse_attributes(fields[ATTRIBUTES_FIELD]) |
| 60 | + if protein and canonical: |
| 61 | + out.write(gene_line + line) |
| 62 | + gene_line = "" # only print gene line before first transcript line |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + parser = argparse.ArgumentParser() |
| 67 | + parser.add_argument('gtf', help="Input GTF from GENCODE") |
| 68 | + parser.add_argument('outfile', help="Output filename") |
| 69 | + args = parser.parse_args() |
| 70 | + |
| 71 | + process(args.gtf, args.outfile) |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + main() |
0 commit comments