annotate PsiCLASS-1.0.2/samtools-0.1.19/misc/zoom2sam.pl @ 0:903fc43d6227 draft default tip

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