/
ns0005
79 lines (56 loc) · 1.73 KB
/
ns0005
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
#!/usr/bin/env perl
=head1 NAME
ns0005 - remove multiple-hit reads from a SAM file
=head1 SYNOPSIS
ns0005 [-h] bwa_sampe.sam
=head1 USAGE
$ perldoc ns0005
$ ns0005 -h
$ ns0005 all_hits.sam > unique_hits_only.sam
=head1 DESCRIPTION
This script reads a SAM file which was created
by bwa sampe (BWA Backtrack, but not MEM),
and removes multiple-hit reads; only unique hits remain.
The reduced output is also a SAM file that is printed onto
the standard output.
=head1 OPTIONS
-h Print usage
=head1 AUTHOR
Coded by Kohji OKAMURA, Ph.D.
=head1 HISTORY
2012-04-31 The first version from scratch
2013-05-27 POD is modified
2013-10-09 Renamed from jf0005.pl
2016-07-01 Clarify not to support BWA-MEM SAM
=cut
use warnings;
use strict;
use English;
use Getopt::Std;
my %opts;
my ($line1, $line2); # to read a pair of reads (R1 and R2)
my ($id1, $id2);
my ($mapq1, $mapq2);
my ($data1, $data2);
getopts 'h', \%opts;
if (defined $opts{h}) { exec "perldoc $0" }
while ($line1 = <ARGV>)
{
if ($line1 =~ m/^\@/) { print $line1; next } # header lines
else
{
$line2 = <ARGV>; # paired one
$line1 =~ m/^(\S+)\t\S+\t\S+\t\S+\t(\d+)\t(.+)\n$/
or die "Format error 1: $line1";
($id1, $mapq1, $data1) = ($1, $2, $3);
$line2 =~ m/^(\S+)\t\S+\t\S+\t\S+\t(\d+)\t(.+)\n$/
or die "Format error 2: $line2";
($id2, $mapq2, $data2) = ($1, $2, $3);
unless ($id1 eq $id2) { die "Format error 3: $id1 $id2" }
if ($mapq1 == 0 or $mapq2 == 0) { next }
$data1 =~ m/\sXT:A:U\s/ or next;
$data2 =~ m/\sXT:A:U\s/ or next;
print $line1, $line2;
}
}
exit