-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_sam_for_refcontigs.py
95 lines (57 loc) · 1.84 KB
/
filter_sam_for_refcontigs.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/local/bin/python
# Python 2.7.6
# vcf_to_phylip.py
# 31 March 2015
# Mathias Scharmann
# usage example
# samtools view sample_TGCAT-RG.bam | python filter_sam_for_refcontigs.py --contiglist xxfu
"""
reads STDIN from samtools view and returns only lines containing the reference contigs of names species fied in the contiglist
"""
import argparse
import os
import sys
########################## HEAD
# this function checks if file exists and exits script if not
def extant_file(x):
"""
'Type' for argparse - checks that file exists but does not open.
"""
if not os.path.exists(x):
print "Error: {0} does not exist".format(x)
exit()
x = str(x)
return x
# breaks script if non-UNIX linebreaks in input file popmap
def linebreak_check(x):
if "\r" in open(x, "rb").readline():
print "Error: classic mac (CR) or DOS (CRLF) linebreaks in {0}".format(x)
exit()
######
# parses arguments from command line
def get_commandline_arguments ():
parser = argparse.ArgumentParser()
parser.add_argument("--contiglist", required=True, type=extant_file,
help="name/path of contig list input file", metavar="FILE")
args = parser.parse_args()
# finish
return args
################################## CORE
def read_contigfile(contiglist):
contigs = set()
with open(contiglist, "r") as INFILE:
for line in INFILE:
contigs.add(line.strip("\n"))
return contigs
################################## MAIN
args = get_commandline_arguments ()
retain_contigs = read_contigfile(args.contiglist)
for line in sys.stdin:
if line.split("\t")[2] in retain_contigs:
sys.stdout.write(line)
elif line.startswith("@"):
if line.split("\t")[1].split(":")[1] in retain_contigs:
sys.stdout.write(line)
elif line.startswith("@RG") or line.startswith("@PG"): # retain read group in header!!
sys.stdout.write(line)
#print args