annotate pyPRADA_1.2/tools/samtools-0.1.16/misc/zoom2sam.pl @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #!/usr/bin/perl -w
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 # Contact: lh3
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 # Version: 0.1.0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 use strict;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 use warnings;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 use Getopt::Std;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 &zoom2sam;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 exit;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 sub mating {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 my ($s1, $s2) = @_;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 my $isize = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 if ($s1->[2] ne '*' && $s1->[2] eq $s2->[2]) { # then calculate $isize
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 my $x1 = ($s1->[1] & 0x10)? $s1->[3] + length($s1->[9]) : $s1->[3];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 my $x2 = ($s2->[1] & 0x10)? $s2->[3] + length($s2->[9]) : $s2->[3];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 $isize = $x2 - $x1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 # update mate coordinate
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 if ($s2->[2] ne '*') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 @$s1[6..8] = (($s2->[2] eq $s1->[2])? "=" : $s2->[2], $s2->[3], $isize);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 $s1->[1] |= 0x20 if ($s2->[1] & 0x10);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 $s1->[1] |= 0x8;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 if ($s1->[2] ne '*') {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 @$s2[6..8] = (($s1->[2] eq $s2->[2])? "=" : $s1->[2], $s1->[3], -$isize);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 $s2->[1] |= 0x20 if ($s1->[1] & 0x10);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 $s2->[1] |= 0x8;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 sub zoom2sam {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 my %opts = ();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 getopts("p", \%opts);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 die("Usage: zoom2sam.pl [-p] <readLen> <aln.zoom>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 Warnings: This script only supports the default Illumina outputs.\n") if (@ARGV < 2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 my $is_paired = defined($opts{p});
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 my $len = shift(@ARGV);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 # core loop
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 my @s1 = ();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 my @s2 = ();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 my ($s_last, $s_curr) = (\@s1, \@s2);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 while (<>) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 &zoom2sam_aux($_, $s_curr, $is_paired, $len);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 if (@$s_last != 0 && $s_last->[0] eq $s_curr->[0]) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 &mating($s_last, $s_curr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 print join("\t", @$s_last), "\n";
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 print join("\t", @$s_curr), "\n";
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 @$s_last = (); @$s_curr = ();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 print join("\t", @$s_last), "\n" if (@$s_last != 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 my $s = $s_last; $s_last = $s_curr; $s_curr = $s;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 print join("\t", @$s_last), "\n" if (@$s_last != 0);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 sub zoom2sam_aux {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 my ($line, $s, $is_paired, $len) = @_;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 chomp($line);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 my @t = split("\t", $line);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 @$s = ();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 # read name
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 $s->[0] = $t[0];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 # initial flag (will be updated later)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 $s->[1] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 $s->[1] |= 1 | 1<<6 if ($s->[0] =~ /_F$/);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 $s->[1] |= 1 | 1<<7 if ($s->[0] =~ /_R$/);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 $s->[1] |= 2 if ($is_paired);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 # read & quality
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 $s->[9] = "*"; $s->[10] = "*";
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 # cigar
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 $s->[5] = $len . "M";
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 # coor
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 my @s = split(/\s+/, $t[1]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 $s->[2] = $s[0];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 $t[1] =~ /:(\d+)$/;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 $s->[3] = $1 + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 if ($s->[0] =~ /_[FR]$/) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 my $u = ($s->[0] =~ /_F$/)? 1 : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 my $w = ($t[2] eq '+')? 1 : 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 $s->[1] |= 0x10 if ($u ^ $w);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 $s->[0] =~ s/_[FR]$//;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 $s->[1] |= 0x10 if ($t[2] eq '-');
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 # mapQ
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 $s->[4] = 30;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 # mate coordinate
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 $s->[6] = '*'; $s->[7] = $s->[8] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 # aux
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 push(@$s, "NM:i:$t[3]");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 }