File: SAM_nameSorted_to_uniq_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 (275 lines) | stat: -rwxr-xr-x 7,681 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env perl

use strict;
use warnings;

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

my $usage = "\n\nusage: name_sorted_paired_reads.sam [debug]\n\n";

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

if ($sam_file =~ /coord/i) {
    die "Your filename contains the term 'coord' which makes me nervous.  This needs to be a name-sorted SAM file. Please rename the file or use the correct version here\n.";
}


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


=notes

Gathers all reads having the same core read name (both left and right reads of PE frags).

Separates the left and right reads into corresponding lists.

Examines pairwise comparisons between left and right reads, check for same scaffold and that the right reads mate aligned coordinate matches up with that of the left reads aligned coordinate.

If not properly paired:
    have both left and right reads mapped anywhere:  improper pair
Otherwise, left-only or right-only counts.


=cut


main: {


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

    my @reads;

    my %counts = ( 'UPP' => 0, # unique proper pairs
                   'MPP' => 0, # multi-mapped proper pairs
                   
                   'IP' => 0, # improper pairs
                   
                   'UL' => 0, # left unique reads
                   'ML' => 0, # left multi-mapped reads
                   
                   'UR' => 0, # right unique reads
                   'MR' => 0, # right multi-mapped reads

                   'US' => 0, # single unique-reads
                   'MS' => 0, # single multi-mapped reads
        );
         

    my $line_counter = 0;

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

        my $read = $sam_reader->get_next();
        
        if ($read->is_query_unmapped()) { next; }


        my $scaff_name = $read->get_scaffold_name();
        my $core_read_name = $read->get_core_read_name();
        
        if ($core_read_name ne $prev_read_name) {

            if (@reads) {
                &process_pairs(\@reads, \%counts);
                @reads = ();
            }
        }
        
        push (@reads, $read);
        

        $prev_read_name = $core_read_name;
        $prev_scaff_name = $scaff_name;


        if ($line_counter % 100000 == 0) {
            my $count_print = $line_counter;
            $count_print=~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g;
            print STDERR "\r[$count_print]  lines read ";
        }
            
        
    }

    print STDERR "\n\n";
        
    &process_pairs(\@reads, \%counts) if @reads;
    

    my $sum_reads = 0;
    foreach my $count (values %counts) {
        $sum_reads += $count;
    }

=try_mirror_bowtie2_summary

30575 reads; of these:
  30575 (100.00%) were paired; of these:
    2577 (8.43%) aligned concordantly 0 times
    5766 (18.86%) aligned concordantly exactly 1 time
    22232 (72.71%) aligned concordantly >1 times
    ----
    2577 pairs aligned concordantly 0 times; of these:
      133 (5.16%) aligned discordantly 1 time
    ----
    2444 pairs aligned 0 times concordantly or discordantly; of these:
      4888 mates make up the pairs; of these:
        3588 (73.40%) aligned 0 times
        322 (6.59%) aligned exactly 1 time
        978 (20.01%) aligned >1 times
94.13% overall alignment rate

=cut

    print "Stats for aligned rna-seq fragments (note, not counting those frags where neither left/right read aligned)\n";
    print "\n\n$sum_reads aligned fragments; of these:\n";
    print "  " . ($sum_reads - $counts{US} - $counts{MS}) . " were paired; of these:\n";
    print "    " . ($counts{UL} + $counts{ML} + $counts{UR} + $counts{MR} + $counts{IP}) . " aligned concordantly 0 times\n";
    print "    " . $counts{UPP} . " aligned concordantly exactly 1 time\n";
    print "    " . $counts{MPP} . " aligned concordantly >1 times\n";
    print "    ----\n";
    print "    " . ($counts{UL} + $counts{ML} + $counts{UR} + $counts{MR} + $counts{IP}) . " pairs aligned concordantly 0 times; of these:\n";
    print "    " . $counts{IP} . " aligned as improper pairs\n";
    print "    " . ($counts{UL} + $counts{ML} + $counts{UR} + $counts{MR}) . " pairs had only one fragment end align to one or more contigs; of these:\n";
    print "       " . ($counts{UL} + $counts{ML}) . " fragments had only the left /1 read aligned; of these:\n";
    print "            " . $counts{UL} . " left reads mapped uniquely\n";
    print "            " . $counts{ML} . " left reads mapped >1 times\n";
    print "       " . ($counts{UR} + $counts{MR}) . " fragments had only the right /2 read aligned; of these:\n";
    print "            " . $counts{UR} . " right reads mapped uniquely\n";
    print "            " . $counts{MR} . " right reads mapped >1 times\n";
    print "Overall,  " . sprintf("%.2f", ($counts{UPP} + $counts{MPP})/$sum_reads * 100) . "% of aligned fragments aligned as proper pairs\n\n\n";
    
    if ($counts{US} + $counts{MS}) {
        print "  " . ($counts{US} + $counts{MS}) . " were single-end reads; of these:\n";
        print "      " . $counts{US} . " single-end reads mapped uniquely\n";
        print "      " . $counts{MS} . " single-end reads mapped >1 times\n";
    }
        
    close $DEBUG_OFH if $DEBUG;
    
    exit(0);

}



####
sub process_pairs {
    my ($reads_aref, $counts_href) = @_;
    
    my @reads = @$reads_aref;
    
    my @left_reads;
    my @right_reads;


    foreach my $read (@reads) {
        if ($read->is_first_in_pair()) {
            push (@left_reads, $read);
        }
        elsif ($read->is_second_in_pair()) {
            push (@right_reads, $read);
        }
    }

    my $got_left_read = (@left_reads) ? 1 : 0;
    my $got_right_read = (@right_reads) ? 1: 0;
    

    ## check to see if we have proper pairs:
    my @proper_pairs;
    
    
  pair_search:
    foreach my $left_read (@left_reads) {

        unless ($left_read->is_proper_pair()) { next; }
        
        
        my $aligned_pos = $left_read->get_aligned_position();
                
        foreach my $right_read (@right_reads) {

            unless ($right_read->is_proper_pair()) { next; }
            
            if ($left_read->get_scaffold_name() eq $right_read->get_scaffold_name()
                &&
                $right_read->get_mate_scaffold_position() == $aligned_pos) {
                
                push (@proper_pairs, [$left_read, $right_read]);
                
                last;
            }
        }
    }
    

    my $class = "";
    
    if (@proper_pairs) {

        if (scalar(@proper_pairs) == 1) {
            $class = "UPP";
        }
        else {
            # multi ampped proper pairs
            $class = "MPP";
        }
    }
    elsif ($got_left_read && $got_right_read) {
        $class = "IP";
    }
    elsif ($got_left_read) {

        if (scalar(@left_reads) == 1) {
            $class = "UL";
        }
        else {
            $class = "ML";
        }
    }
    elsif ($got_right_read) {
        
        if (scalar(@right_reads) == 1) {
            $class = "UR";
        }
        else {
            $class = "MR";
        }
    }
    else {
        if (scalar(@reads) == 1) {
            $class = "US";
        }
        else {
            $class = "MS";
        }
    }

    $counts_href->{$class}++;
    
    if ($DEBUG) {
        my $core_read_name = $reads[0]->get_core_read_name();
        
        print $DEBUG_OFH join("\t", $core_read_name, $class) . "\n";
    }

    return;
}