File: filter_low_expr_transcripts.pl

package info (click to toggle)
trinityrnaseq 2.15.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (296 lines) | stat: -rwxr-xr-x 9,567 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
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env perl

use strict;
use warnings;
use Carp;
use Getopt::Long qw(:config posix_default no_ignore_case bundling pass_through);
use lib ("/usr/lib/trinityrnaseq/PerlLib");
use Fasta_reader;

my $help_flag;


my $usage = <<__EOUSAGE__;

##########################################################################################
#
#  --matrix|m <string>            expression matrix (TPM or FPKM, *not* raw counts)
#
#  --transcripts|t <string>       transcripts fasta file (eg. Trinity.fasta)
#
#
#  # expression level filter:
#
#     --min_expr_any <float>      minimum expression level required across any sample (default: 0)
#
#  # Isoform-level filtering
#
#     --min_pct_dom_iso <int>         minimum percent of dominant isoform expression (default: 0)
#          or
#     --highest_iso_only          only retain the most highly expressed isoform per gene (default: off)
#                                 (mutually exclusive with --min_pct_dom_iso param)
#
#     # requires gene-to-transcript mappings
#
#     --trinity_mode              targets are Trinity-assembled transcripts
#         or
#     --gene_to_trans_map <string>   file containing gene-to-transcript mappings
#                                    (format is:   gene(tab)transcript )
#
#########################################################################################


__EOUSAGE__

    ;


my $matrix_file;
my $transcripts_file;
my $min_expr_any = 0;
my $min_pct_dom_iso = 0;
my $highest_iso_only_flag = 0;
my $trinity_mode_flag = 0;
my $gene_to_trans_map_file;


&GetOptions ( 'help|h' => \$help_flag,

              'matrix|m=s' => \$matrix_file,
              'transcripts|t=s' => \$transcripts_file,
              
              'min_expr_any=f' => \$min_expr_any,
              'min_pct_dom_iso=i' => \$min_pct_dom_iso,
              'highest_iso_only' => \$highest_iso_only_flag,

              'trinity_mode' => \$trinity_mode_flag,
              'gene_to_trans_map=s' => \$gene_to_trans_map_file,
              

    );


if ($help_flag) {
    die $usage;
}


unless ($matrix_file && $transcripts_file &&
        ($min_expr_any || $min_pct_dom_iso || $highest_iso_only_flag) ) {

    die $usage;
}

if ( ($min_pct_dom_iso || $highest_iso_only_flag) && ! ($trinity_mode_flag || $gene_to_trans_map_file) ) {
    die "Error, if --min_pct_dom_iso or --highest_iso_only, must also specify either --trinity_mode or --gene_to_trans_map";
}

if ($min_pct_dom_iso && $highest_iso_only_flag) {
    die "Error, --min_pct_dom_iso and --highest_iso_only are mutually exclusive parameters. ";
}


main: {

    my %expr_vals = &parse_expr_matrix($matrix_file);
    
    if ($min_pct_dom_iso || $highest_iso_only_flag) {
        
        my %gene_to_iso_map = ($trinity_mode_flag) 
            ? &parse_Trinity_gene_mapping($transcripts_file)
            : &parse_gene_trans_map_file($gene_to_trans_map_file);
        
        &add_pct_iso_stats(\%expr_vals, \%gene_to_iso_map);
    }

    my $total_records = 0;
    my $retained_records = 0;
    
    my $fasta_reader = new Fasta_reader($transcripts_file);
    while (my $seq_obj = $fasta_reader->next()) {

        $total_records++;
        
        my $acc = $seq_obj->get_accession();

        my $keep_flag = 1;

        my $info_struct = $expr_vals{$acc} or die "Error, no expression record stored for acc: [$acc].  Be sure to provide the transcript expression matrix and all transcripts in the $transcripts_file must have records in the transcript expression matrix file.";
        
        if ($min_expr_any && $info_struct->{max_expr} < $min_expr_any) {
            $keep_flag = 0;
            print STDERR "-excluding $acc, max_expr: $info_struct->{max_expr} < $min_expr_any\n";
        }
        if ($min_pct_dom_iso && (! $info_struct->{top_iso}) && $info_struct->{pct_dom_iso_expr} < $min_pct_dom_iso) {
            # notice we'll still keep the dominant isoform for the gene even if it's pct iso < $min_pct_dom_iso.
            ## dont want to be silly and throw out the gene altogther...  :)
            print STDERR "-excluding $acc, pct_dom_iso_expr $info_struct->{pct_dom_iso_expr} < $min_pct_dom_iso\n";
            
            $keep_flag = 0;
        }
        
        if ($highest_iso_only_flag && ! $info_struct->{top_iso}) {
            print STDERR "-excluding $acc, not top_iso\n";
            $keep_flag = 0;
        }

        if ($keep_flag) {
            $retained_records++;
            my $fasta_record = $seq_obj->get_FASTA_format();
            chomp $fasta_record;
            my ($header_line, @seq_lines) = split(/\n/, $fasta_record);
            # tack on the pct expr info onto the header
            my $top_iso_flag = $info_struct->{top_iso};
            my $pct_iso_expr = (defined $info_struct->{pct_iso_expr}) ? $info_struct->{pct_iso_expr} : "NA";
            
            my $pct_dom_iso_expr = (defined $info_struct->{pct_dom_iso_expr}) ? $info_struct->{pct_dom_iso_expr} : "NA";
            
            $header_line .= " top_iso:$top_iso_flag pct_iso_expr=$pct_iso_expr pct_dom_iso_expr=$pct_dom_iso_expr max_expr_any=$info_struct->{max_expr}";
            
            print join("\n", $header_line, @seq_lines) . "\n";
            
        }
    }
    
    my $pct_records_retained = sprintf("%.2f", $retained_records / $total_records * 100);
    print STDERR "\n\n\tRetained $retained_records / $total_records = $pct_records_retained\% of total transcripts.\n\n\n";
    
    
    exit(0);
    
    
}

####
sub add_pct_iso_stats {
    my ($expr_vals_href, $gene_to_iso_map_href) = @_;
    
    foreach my $gene (keys %$gene_to_iso_map_href) {
        
        my @isoforms = keys %{$gene_to_iso_map_href->{$gene}};
        
        if (scalar @isoforms == 1) {
            # only one isoform, so must be 100% of that gene.
            $expr_vals_href->{ $isoforms[0] }->{pct_iso_expr} = 100;
            $expr_vals_href->{ $isoforms[0] }->{pct_dom_iso_expr} = 100;
            $expr_vals_href->{ $isoforms[0] }->{top_iso} = 1;
            
        }
        else {
            # determine fraction of total gene expr
            # first, get sum of gene expr across isoforms
            my $gene_sum_expr = 0;
            my $dominant_iso_expr = 0;
            foreach my $iso (@isoforms) {
                
                my $expr = $expr_vals_href->{$iso}->{sum_expr};
                if (!defined($expr)) {
                    use Data::Dumper;
                    print STDERR "ISO: $iso\t" . Dumper($expr_vals_href->{$iso});
                }
                if ($expr > $dominant_iso_expr) {
                    $dominant_iso_expr = $expr;
                }

                
                $gene_sum_expr += $expr;
            }
            # now compute pct iso
            foreach my $iso (@isoforms) {
                my $expr = $expr_vals_href->{$iso}->{sum_expr};
                my $pct_iso = 0;
                if ($gene_sum_expr > 0) {
                    $pct_iso = sprintf("%.2f", $expr / $gene_sum_expr * 100);
                }
                $expr_vals_href->{$iso}->{pct_iso_expr} = $pct_iso;

                my $pct_dom_iso_expr = 0;
                if ($dominant_iso_expr > 0) {
                    $pct_dom_iso_expr = sprintf("%.2f", $expr / $dominant_iso_expr * 100);
                }
                $expr_vals_href->{$iso}->{pct_dom_iso_expr} = $pct_dom_iso_expr;
            }
            # set top iso
            @isoforms = sort { $expr_vals_href->{$a}->{pct_iso_expr} <=> $expr_vals_href->{$b}->{pct_iso_expr} } @isoforms;
            
            my $top_isoform = pop @isoforms;  # note, if there's no gene expression for some reason, choice isn't informative.
            $expr_vals_href->{$top_isoform}->{top_iso} = 1;
        }
    }
}


####
sub parse_expr_matrix {
    my ($matrix_file) = @_;
    
    my %expr_vals;
    
    open (my $fh, $matrix_file) or die "Error, cannot open file $matrix_file";
    my $header = <$fh>;
    while (<$fh>) {
        chomp;
        my @expr = split(/\t/);
        my $acc = shift @expr;

        my $max_val = 0;
        my $sum = 0;
        
        foreach my $expr_val (@expr) {
            $sum += $expr_val;
            if ($expr_val > $max_val) {
                $max_val = $expr_val;
            }
        }

        $expr_vals{$acc}->{max_expr} = $max_val;
        $expr_vals{$acc}->{sum_expr} = $sum;
        $expr_vals{$acc}->{pct_iso_expr} = undef; # set later
        $expr_vals{$acc}->{pct_dom_iso_expr} = undef;
        $expr_vals{$acc}->{top_iso} = 0; # set later to the isoform with highest expression for that gene.
        
    }
    close $fh;
    
    return(%expr_vals);
}

####
sub parse_Trinity_gene_mapping {
    my ($transcripts_file) = @_;

    my %gene_to_iso_map;
    
    open (my $fh, $transcripts_file) or die "Error, cannot open file $transcripts_file";
    while (<$fh>) {
        if (/^>(\S+)/) {
            my $acc = $1;
            $acc =~ /^(\S+)(_i\d+)$/ or die "Error, cannot parse Trinity accession: $acc";
            my $gene_id = $1;
            
            $gene_to_iso_map{$gene_id}->{$acc} = 1;
        }
    }
    close $fh;

    return(%gene_to_iso_map);
}

####
sub parse_gene_trans_map_file {
    my ($gene_to_trans_map_file) = @_;

    my %gene_to_iso_map;
    
    open (my $fh, $gene_to_trans_map_file) or die "Error, cannot open file $gene_to_trans_map_file";
    while (<$fh>) {
        chomp;
        my ($gene, $trans) = split(/\t/);
        
        $gene_to_iso_map{$gene}->{$trans} = 1;
    }
    close $fh;

    return (%gene_to_iso_map);
}