forked from roryk/junkdrawer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrepGTF.py
76 lines (61 loc) · 2.27 KB
/
grepGTF.py
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
75
76
import sys
from sets import Set
import logging
import os
from argparse import ArgumentParser
from gtfUtils import GTFtoDict, formatGTFLine, addAttributesToGTFline
def filterAttributes(gtflines, B, F, v=False):
Bfile = open(B, 'r')
filter_set = Set()
for line in Bfile:
filter_set.add(line.strip())
matched = []
notmatched = []
for line in gtflines:
linedict = addAttributesToGTFline(line)
if linedict[F] in filter_set:
matched.append(line)
else:
notmatched.append(line)
if v:
return notmatched
else:
return matched
def main():
logging.basicConfig(format='%(levelname)s: %(asctime)s %(message)s.',
level=logging.INFO)
description = "Keeps GTF lines of file A that have values in field F in file B. " \
"The -v option removes the lines instead of keeping them. " \
"For fields not in the attributes column, the names are: " \
"seqname, source, feature, start, end, score, strand, " \
"unknown."
parser = ArgumentParser(description=description)
parser.add_argument('A', metavar='A',
help='GTF file to be filtered')
parser.add_argument('F', metavar='F',
help='field to filter on')
parser.add_argument('B', metavar='B',
help='file with filter items')
parser.add_argument('-v', dest='inverse', default=False,
action='store_true',
help='exclude lines instead of keeping')
args = parser.parse_args()
if not os.path.isfile(args.A):
logging.error("%s cannot be found." %(args.A))
parser.print_help()
exit(-1)
if not os.path.isfile(args.B):
logging.error("%s cannot be found." %(args.B))
parser.print_help()
exit(-1)
gtflines = GTFtoDict(args.A)
gtflines = filterAttributes(gtflines, args.B, args.F, args.inverse)
logging.info("Writing GTF file.")
written = 0
for line in gtflines:
outline = formatGTFLine(line)
sys.stdout.write(outline)
written = written + 1
logging.info("Wrote %d lines." %(written))
if __name__ == "__main__":
main()