-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsam-discard-dups.py
executable file
·124 lines (87 loc) · 3.04 KB
/
sam-discard-dups.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
"""
sam-discard-dups.py: part of the Personal Identification Pipeline
https://github.com/TeamErlich/personal-identification-pipeline
Copyright (C) 2016 Yaniv Erlich ([email protected])
All Rights Reserved.
This program is licensed under GPL version 3.
See LICENSE file for full details.
Version 0.3
"""
import sys,argparse, errno
from os.path import basename
version_info=\
"""
sam-discard-dups - version 0.1
"""
def parse_command_line():
# Define parameters
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="SAM - Discard duplicates/multiple-mappers",
version=version_info,
epilog="""
This script reads a SAM file, and discards ALL reads which map
multiple times (does not keep even one of the mapping locations).
TODO: Explain hard-clipped reads in minION runs
Example:
# Remove all duplicated reads dirnames:
$ %(prog)s input.sam > output.no-dups.sam
""")
# Positional parameter
parser.add_argument('filename', metavar='FILE', help='file to process');
args = parser.parse_args()
return args
def get_dup_read_ids(filename):
"""
Reads a SAM file, returns a set() of duplicated reads
(reads which are listed more than once)
"""
try:
seen_ids = set()
dup_ids = set()
sam=file(filename,'r')
for linenum,line in enumerate(sam):
err = "input error in '%s' line %d: " % (filename, linenum+1)
line = line.strip()
if line[:1]=='@':
continue
flds = line.split('\t')
read_id = flds[0]
if read_id in seen_ids:
dup_ids.add(read_id)
seen_ids.add(read_id)
return dup_ids
except IOError as e:
if e.errno == errno.EPIPE:
sys.exit(0) # exit silently
# TODO: this is a cop-out, hard to tell what's the exact error
# and give informative,useful error message to the user.
sys.exit("I/O error: %s" % (str(e)))
def filter_sam_dups(filename,ids_to_discard):
"""
Reads a SAM file, returns a set() of duplicated reads
(reads which are listed more than once)
"""
try:
sam=file(filename,'r')
for linenum,line in enumerate(sam):
err = "input error in '%s' line %d: " % (filename, linenum+1)
line = line.strip()
if line[:1]=='@':
print line
continue
flds = line.split('\t')
read_id = flds[0]
if not (read_id in ids_to_discard):
print line
except IOError as e:
if e.errno == errno.EPIPE:
sys.exit(0) # exit silently
# TODO: this is a cop-out, hard to tell what's the exact error
# and give informative,useful error message to the user.
sys.exit("I/O error: %s" % (str(e)))
if __name__ == "__main__":
args = parse_command_line()
dups = get_dup_read_ids(args.filename)
filter_sam_dups(args.filename, dups)