This repository was archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_gtf.py
More file actions
executable file
·74 lines (55 loc) · 3.04 KB
/
filter_gtf.py
File metadata and controls
executable file
·74 lines (55 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# --------------------------- filter gtf ----------------------------------------
# This script will filter a GTF file to remove any given list of transcript types
# It will print the transcript/gene ids to an output file
# This output file can be then be used in combination with grep to filter the gtf.
# eg. grep -v -f outfile.txt base.gtf
# -------------------------------------------------------------------------------
import argparse
import logging
import os
import gffutils
def main(args):
logging.info("GTF:{gtf_db}, Transtypes:{transtypes}, outfile:{out}".format(gtf_db=args.gtf_db,
transtypes=args.transtypes,
out=args.out))
filtered_ids = set()
# GTF db to be imported for filtering
gtf_db = gffutils.FeatureDB(args.gtf_db)
# Lets go through all the genes in the gtf
for gene in gtf_db.features_of_type('gene'):
# For every gene iterate over its transcripts
for transcript in gtf_db.children(gene, featuretype='transcript'):
# If it has any of the filtered attributes the process it
if set(transcript.attributes['transcript_type']).intersection(args.transtypes):
# If it is a single transcript gene we just remove that gene itself
if len(list(gtf_db.children(gene, featuretype='transcript'))) == 1:
filtered_ids.add(gene.id)
# Else remove just the transcripts
else:
filtered_ids.add(transcript.id)
# Write the filtered genes/transcripts to an outfile
with open(args.out, "w") as out_handle:
for filt_id in sorted(list(filtered_ids)):
out_handle.write("{}\n".format(filt_id))
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(levelname)s : %(message)s', level=logging.INFO)
def is_valid_file(parser, arg):
""" Check if file exists """
if not os.path.isfile(arg):
parser.error('The file at %s does not exist' % arg)
else:
return arg
epilog = "EXAMPLE: python " + os.path.basename(__file__) + \
" --gtf /path/to/gtf.db --transtype retained_intron " \
"--out /path/to/output.txt"
parser = argparse.ArgumentParser(description="Script to give transcripts/genes of given transcript type",
epilog=epilog)
required_args_group = parser.add_argument_group('required arguments')
required_args_group.add_argument('-g', '--gtfdb', dest='gtf_db', required=True,
type=lambda x: is_valid_file(parser, x))
required_args_group.add_argument('-t', '--transtypes', dest='transtypes', required=True, action="append")
parser.add_argument('-o', '--out', dest='out', help="DEFAULT: {GTF_BASE}_filt.txt")
args = parser.parse_args()
if not args.out:
args.out = "{base}_filt.txt".format(base=os.path.splitext(args.gtf_db)[0])
main(args)