File: scaffold_iworm_contigs.pl

package info (click to toggle)
trinityrnaseq 2.15.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 468,004 kB
  • sloc: perl: 49,905; cpp: 17,993; java: 12,489; python: 3,282; sh: 1,989; ansic: 985; makefile: 717; xml: 62
file content (286 lines) | stat: -rwxr-xr-x 8,193 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
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env perl

use strict;
use warnings;

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

my $usage = "\n\nusage: $0 nameSorted.sam iworm_fasta_file\n\n";

my $name_sorted_sam_file = $ARGV[0] or die $usage;
my $iworm_fasta = $ARGV[1] or die $usage;


my $SAM_OFH;


main: {
        
    open ($SAM_OFH, ">scaffolding_entries.sam") or die $!;
    
    my %iworm_acc_to_fasta_index;
    {
        
        my $counter = 0;
        open (my $fh, $iworm_fasta) or die "Error, cannot open file $iworm_fasta";
        while (<$fh>) {
            if (/^>(\S+)/) {
                my $acc = $1;
                $iworm_acc_to_fasta_index{$acc} = $counter; # starts at zero
                $counter++;
            }
        }
        close $fh;
    }
    

    my %paired_iworm_contigs;

    my $prev_core_acc = "";
    my %end_to_iworm;
    
    my $num_warnings = 0;

    my $num_unrecognized_iworm_contig_names = 0;

    if ($name_sorted_sam_file =~ /\.bam$/) {
        $name_sorted_sam_file = "samtools view $name_sorted_sam_file | ";
    }
    
    open (my $fh, $name_sorted_sam_file) or die "Error, cannot open file $name_sorted_sam_file";
    while (<$fh>) {
        my $line = $_;
        chomp;
        my @x = split(/\t/);
        my $read_acc = $x[0];
        my $iworm_acc = $x[2];

        unless (defined $iworm_acc) { next; }
        
        if ($iworm_acc eq '*') { 
            # unmapped read
            next;
        }
        
        unless ($iworm_acc =~ /^a\d+;\d+/) {
            $num_unrecognized_iworm_contig_names++;
            if ($num_unrecognized_iworm_contig_names <= 10) {
                print STDERR "warning, inchworm contig ($iworm_acc) isn't recognized. Ignoring entry: [[$line]]\n";
            }
            if ($num_unrecognized_iworm_contig_names == 11) {
                print STDERR "warning, too many unrecognized inchworm contig names.  Will report summary of counts later.\n";
            }
            next;
        }


        my $core_acc;
        my $frag_end;
        

        if ($read_acc =~ /^(\S+)\/([12])$/) { 
            
            $core_acc = $1;
            $frag_end = $2;
        }
        
        # capture long read mappings
        elsif ($read_acc =~ /^(LR\$\|\S+)/) {
            $core_acc = $1;
            $frag_end = "LR";
        }
        
        else {
            # must have mixed in a single read with the pairs...
            if ($num_warnings <= 10) {
                print STDERR "warning, ignoring read: $read_acc since cannot decipher if /1 or /2 of a pair.\n";
            }
            elsif ($num_warnings == 11) {
                print STDERR "number of read warnings exceeded 10.  Turning off warning messages from here out.\n";
            }
            $num_warnings++;
            
            next;
        }
        
        if ($core_acc ne $prev_core_acc) {
            
            &examine_frags(\%end_to_iworm, \%paired_iworm_contigs);
            %end_to_iworm = ();
            
        }

        $end_to_iworm{$frag_end}->{$iworm_acc} = $line;
        
        $prev_core_acc = $core_acc;

    }
    close $fh;

    ## get last one
    &examine_frags(\%end_to_iworm, \%paired_iworm_contigs);
    
    if ($num_warnings) {
        print STDERR "WARNING: note there were $num_warnings reads that could not be deciphered as being /1 or /2 of a PE fragment.  Hopefully, these were SE reads that should have been ignored. Otherwise, please research this further.\n\n";
    }
    if ($num_unrecognized_iworm_contig_names) {
        print STDERR "WARNING: note, there were $num_unrecognized_iworm_contig_names inchworm contig names in the SAM file that were ignored due to the inchworm contig accession not being recognized.\n";
    }
    
    foreach my $pairing (reverse sort {$paired_iworm_contigs{$a}<=>$paired_iworm_contigs{$b}} keys %paired_iworm_contigs) {

        my ($iworm_acc_A, $iworm_acc_B) = split(/\t/, $pairing);
      
        my $index_A = $iworm_acc_to_fasta_index{$iworm_acc_A};
        unless (defined $index_A) {
            print STDERR "WARNING, no index for iworm acc: $iworm_acc_A\n";
            next;
        }
        my $index_B = $iworm_acc_to_fasta_index{$iworm_acc_B};
        unless (defined $index_B) {
            print STDERR "WARNING, no index for iworm acc: $iworm_acc_B\n";
            next;
        }
        
        print join("\t", $iworm_acc_A, $index_A, $iworm_acc_B, $index_B, $paired_iworm_contigs{$pairing}) . "\n";
    
    }
    

    close $SAM_OFH;
    
    exit(0);
}

####
sub examine_frags {
    my ($end_to_iworm_href, $paired_iworm_contigs_href) = @_;

    if (exists ($end_to_iworm_href->{LR})) {
        my @LR_read_names = keys %{$end_to_iworm_href->{LR}};
        
        for (my $i = 0; $i < $#LR_read_names; $i++) {
            for (my $j = $i + 1; $j <= $#LR_read_names; $j++) {
                
                my $pair  = join("\t", sort ($LR_read_names[$i], $LR_read_names[$j]) );
                $paired_iworm_contigs_href->{$pair}++;

                #print STDERR "-got LR pair: $pair\n";
            }
        }
        
        return;
    }
    

    ## handle the paired-end reads
    
    my @ends = keys %$end_to_iworm_href;

    unless (scalar @ends == 2) {
        ## no pairs
        return;
    }
    
    
    my @iworm_left = keys %{$end_to_iworm_href->{1}};
    my @iworm_right = keys %{$end_to_iworm_href->{2}};

    
    if (scalar(@iworm_left) == 1 && scalar(@iworm_right) == 1
        &&
        $iworm_left[0] ne $iworm_right[0]) {
        
        ## simplest case, pairs each aligning to different contigs.
    
        my $i_left = $iworm_left[0];
        my $i_right = $iworm_right[0];

        my $pair = join("\t", sort ($i_left, $i_right));
        #print STDERR "Got pairing: $pair\n";
        $paired_iworm_contigs_href->{$pair}++;
    
    }
    else {
        # examine split reads
        
        &check_for_split_reads($end_to_iworm_href->{1}, $paired_iworm_contigs_href);
        &check_for_split_reads($end_to_iworm_href->{2}, $paired_iworm_contigs_href);
        
        
    }
        
    ## output all alignments for these reads.
    foreach my $sam_line (values %{$end_to_iworm_href->{1}}, values %{$end_to_iworm_href->{2}}) {
        print $SAM_OFH $sam_line;
    }
        
    
    return;
}

        
####
sub check_for_split_reads {
    my ($read_mappings_href, $paired_iworm_contigs_href) = @_;

    my @iworm_names = keys %$read_mappings_href;

    if (scalar(@iworm_names) < 2) {
        # nothing to do here.
        return;
    }
    
    ## see if we have a read where different parts of the read are aligning to different iworm contigs.
    for (my $i = 0; $i < $#iworm_names; $i++) {
        my $iworm_name_i = $iworm_names[$i];
        my $align_i = new SAM_entry($read_mappings_href->{$iworm_name_i});
        
        for (my $j = $i + 1; $j <= $#iworm_names; $j++) {
            my $iworm_name_j = $iworm_names[$j];
            my $align_j = new SAM_entry($read_mappings_href->{$iworm_name_j});

            if (&overlap_not_involving_containment($align_i, $align_j)) {
                ## store pair
                
                my $pair = join("\t", sort($iworm_name_i, $iworm_name_j));
                $paired_iworm_contigs_href->{$pair}++;
                
            }
        }
    }

    return;
}
    
####
sub overlap_not_involving_containment {
    my ($align_i, $align_j) = @_;
        
    my ($read_i_lend, $read_i_rend) = $align_i->get_read_span();
            
    my ($read_j_lend, $read_j_rend) = $align_j->get_read_span();
    
    #print join("\t", "$read_i_lend-$read_i_rend", "$read_j_lend-$read_j_rend") . "\n";
    
    if (  ($read_i_lend <= $read_j_lend && $read_i_rend >= $read_j_rend) # i encapsulates j
          ||
          ($read_j_lend <= $read_i_lend && $read_j_rend >= $read_i_rend)  # j encapsulates i
        ) {
        ## containment
        #print "\t* containment\n";
        return(0);
    }
    elsif ( $read_i_lend < $read_j_rend && $read_i_rend > $read_j_lend) {
        #print "\t OVERLAP\n";
        return(1);
    }
    else {
        #print "\tok\n";
        
        return(1);
    }
        
}