File: SAM_sortAny_to_count_stats.pl

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (176 lines) | stat: -rwxr-xr-x 4,415 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env perl

use strict;
use warnings;

use lib ("/usr/lib/trinityrnaseq/PerlLib");
use SAM_reader;
use SAM_entry;

my $usage = "\n\nusage: aligned_reads.sam [debug]\n\n"
    . "Note: uses the bitflag settings for counting entries. Proper-pairing supercedes other settings.\n\n";

my $sam_file = $ARGV[0] or die $usage;
my $DEBUG = $ARGV[1] || 0;


my $DEBUG_OFH;
if ($DEBUG) {
    open ($DEBUG_OFH, ">_debug.frag_classes") or die $!;
}

=notes

Entirely works off flag settings.

Proper pairs trump individual left/right alignments.

Improper pairs: left and right read alignments exist anywhere but not flagged as proper pairs.

=cut

main: {

    my $prev_read_name = "";
    my $prev_scaff_name = "";

    my %counts;

    my $count = 0;

    my $sam_reader = new SAM_reader($sam_file);
    while ($sam_reader->has_next()) {
        
        $count++;

        my $read = $sam_reader->get_next();
        
        if ($read->is_query_unmapped()) { next; } # ignore unaligned read entries.

        my $scaff_name = $read->get_scaffold_name();
        my $core_read_name = $read->get_core_read_name();
        
        if ($read->is_paired()) {
            
            if ($read->is_proper_pair()) {
                ## erase any single entries.
                if (exists $counts{ $core_read_name . "::L" }) {
                    delete $counts{ $core_read_name . "::L" };
                }
                if (exists $counts{ $core_read_name . "::R" } ) {
                    delete $counts{ $core_read_name . "::R" };
                }
                $counts{ $core_read_name . "::PP" } = 1;
            }
            else {
                # not propper pair
                unless (exists $counts{ $core_read_name . "::PP" }) {
                    if ($read->is_first_in_pair()) {
                        $counts{ $core_read_name . "::L" } = 1;
                    }
                    elsif ($read->is_second_in_pair()) {
                        $counts{ $core_read_name . "::R" } = 1;
                    }
                }
            }
        }
        else {
            $counts{ $core_read_name . "::S" } = 1;
        }
        
        if ($count % 100000 == 0) {
            my $count_print = $count;
            $count_print=~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
            print STDERR "\r[$count_print]   ";
        }
    }
    print STDERR "\n\n";
    
    ## identify improper pairs
    my @reads = keys %counts;
    foreach my $read (@reads) {
        if ($read =~ /::L$/) {
            my $core_name = $read;
            $core_name =~ s/::L$//;
            
            if (exists($counts{ $core_name . "::R" }) ) {
                ## count as improper pair instead
                $counts{ $core_name . "::IP" } = 1;
                delete $counts{$read};
                delete $counts{ $core_name . "::R" };
                
            }
        }
    }


    ## sum counts, generate summary
    
    my $count_PP = 0;
    my $count_L = 0;
    my $count_R = 0;
    my $count_S = 0;
    my $count_IP = 0; # improper pairs

    my $total = 0;

    foreach my $read (keys %counts) {
        
        my @x = split(/::/, $read);
        my $class = pop @x;
        my $core_read_name = join("::", @x);
        
        if ($class eq "PP") {
            $count_PP += 2;
            $total += 2;
        }
        elsif ($class eq "IP") {
            $count_IP += 2;
            $total += 2;

        }
        elsif ($class eq "L") {
            $count_L++;
            $total++;

        }
        elsif ($class eq "R") {
            $count_R++;
            $total++;

        }
        elsif ($class eq "S") {
            $count_S++;
            $total++;

        }
    
        if ($DEBUG) {
            print $DEBUG_OFH join("\t", $core_read_name, $class) . "\n";
                            
        }
        
    }
    
    print "Proper_pair:\t$count_PP\t" . sprintf("%.3f%%", $count_PP/$total*100) . "\n"
        . "Improper_pair:\t$count_IP\t" . sprintf("%.3f%%", $count_IP/$total*100) . "\n"
        . "Left-only:\t$count_L\t" . sprintf("%.3f%%", $count_L/$total*100) . "\n"
        . "Right-only:\t$count_R\t" . sprintf("%.3f%%", $count_R/$total*100) . "\n"
        . "Single_end:\t$count_S\t" . sprintf("%.3f%%", $count_S/$total*100) . "\n\n";

    print "Total aligned frags: $total\n\n";



    close $DEBUG_OFH if $DEBUG;

    exit(0);
    

}