File: prune_isoforms_fasta.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 (294 lines) | stat: -rwxr-xr-x 8,659 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
#!/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;
use Data::Dumper;


my $help_flag;


my $usage = <<__EOUSAGE__;

####################################################################################
#
# required:
#
#  --transcripts_fasta <string>   fasta file containing transcript sequences.      
#
#  --gene_trans_map <string>      gene-trans mapping file (format: gene_id(tab)transcript_id)
#
#  --expr_matrix <string>         expression matrix (fpkm or tpm, ideally TMM-normalized)
#
#  --samples_file <string>        samples file
#
#  --min_pct_iso <float>          minimum percent of total isoform expression
#
# optional:
#
#  --out_prefix <string>          output filename prefix ("pruned.\${min_pct_iso}"
#
####################################################################################

__EOUSAGE__

    ;



my $gene_trans_map_file;
my $expr_matrix;
my $samples_file;
my $min_pct_iso;
my $out_prefix;
my $transcripts_fasta_file;

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

              'gene_trans_map=s' => \$gene_trans_map_file,
              'expr_matrix=s' => \$expr_matrix,
              'transcripts_fasta=s' => \$transcripts_fasta_file,

              'samples_file=s' => \$samples_file,
              'min_pct_iso=s' => \$min_pct_iso,
              'out_prefix=s' => \$out_prefix,
              
    );

unless ($gene_trans_map_file && $expr_matrix && $samples_file && $min_pct_iso && $transcripts_fasta_file) {
    die $usage;
}

unless ($out_prefix) {
    $out_prefix = "pruned.$min_pct_iso";
}

main: {

    print STDERR "-parsing samples file: $samples_file\n";
    my %sample_to_replicates = &parse_samples_file($samples_file);

    print STDERR "-parsing gene-trans mapping file: $gene_trans_map_file\n";
    my %gene_to_trans = &parse_gene_trans_relationships($gene_trans_map_file);
    
    print STDERR "-parsing expr matrix: $expr_matrix\n";
    my %trans_to_avg_expr = &parse_expr_matrix($expr_matrix, \%sample_to_replicates);
    
    my $out_log_filename = "$out_prefix.log";
    my $out_fasta_filename = "$out_prefix.fasta";

    open(my $out_log_fh, ">$out_log_filename") or die "Error, cannot write to $out_log_filename";
    open(my $out_fasta_fh, ">$out_fasta_filename") or die "Error, cannot write to $out_fasta_filename";

    print STDERR "-pruning isoforms\n";

    ## decide on which transcripts to keep
    my %trans_retain;
    foreach my $gene (keys %gene_to_trans) {
        my @trans_ids = keys %{$gene_to_trans{$gene}};
        if (scalar(@trans_ids) == 1) {
            # no alt splicing, just keep it.
            $trans_retain{ $trans_ids[0] } = 1;
            next;
        }

        my %trans_to_sum_expr_all_conditions;
        my @trans_to_sample_expr;
        my %sample_to_sum_expr;
        foreach my $sample_name (keys %sample_to_replicates) {
            my $sum_expr = 0;
            my @trans_to_expr;
            foreach my $trans_id (@trans_ids) {
                my $expr = $trans_to_avg_expr{$trans_id}->{$sample_name};
                unless (defined $expr) {
                    die "Error, no expression set for trans_id: [$trans_id] and sample type: [$sample_name] ";
                }
                $trans_to_sum_expr_all_conditions{$trans_id} += $expr;
                push (@trans_to_sample_expr, [$trans_id, $sample_name, $expr]);
                $sample_to_sum_expr{$sample_name} += $expr;
            }
        }

        # prioritize transcript by order across experiments
        @trans_to_sample_expr = reverse sort {$trans_to_sum_expr_all_conditions{$a->[0]} <=> $trans_to_sum_expr_all_conditions{$b->[0]}} @trans_to_sample_expr;
        
        my %seen_sample;
        foreach my $trans_info_aref (@trans_to_sample_expr) {
            # always retain top isoform in any sample type.
            my ($trans_id, $sample_name, $expr) = @$trans_info_aref;
            
            $expr = sprintf("%.3f", $expr);
            
            my $retain_flag = 0;
            my $selected_top_entry = 0;
            if (! $seen_sample{$sample_name}) {
                # top expr entry for that sample
                $seen_sample{$sample_name} = 1;
                $selected_top_entry = 1;
                $retain_flag = 1;
            }
            
            my $sum_sample_expr = $sample_to_sum_expr{$sample_name};
            my $pct_iso = 0;
            if ($sum_sample_expr > 0) {
                $pct_iso = sprintf("%.2f", $expr / $sum_sample_expr * 100);
                if ($pct_iso >= $min_pct_iso) {
                    $retain_flag = 1;
                }
            }
            
            if ($retain_flag) {
                $trans_retain{$trans_id} = 1;
            }
            
            ## output log entry
            my $out_text = join("\t", $gene, $trans_id, $sample_name, $expr, $pct_iso);
            if ($retain_flag) {
                $out_text .= "\t[RETAINED]";
            }
            if ($selected_top_entry) {
                $out_text .= "\t[TOP_EXPR]";
            }
            print $out_log_fh "$out_text\n";
        }
        
        print $out_log_fh "\n"; # spacer between genes.
    }
    

    close $out_log_fh;
    
    my $fasta_reader = new Fasta_reader($transcripts_fasta_file) or die $!;
    my %to_retrieve = %trans_retain;

    print STDERR "-outputting filtered transcripts as: $out_fasta_filename\n";
    while (my $seq_obj = $fasta_reader->next()) {
        my $acc = $seq_obj->get_accession();
        if ($trans_retain{$acc}) {
            my $fasta_entry = $seq_obj->get_FASTA_format();
            chomp $fasta_entry;
            print $out_fasta_fh "$fasta_entry\n";
            delete($to_retrieve{$acc});
        }
    }
    
    if (%to_retrieve) {
        die "Failed to retrieve fasta records for transcripts: " . Dumper(\%to_retrieve);
    }
    
    close $out_fasta_fh;
    
    print STDERR "-done.  See outputs: $out_prefix.\*\n\n";
    
    exit(0);
}

####
sub parse_gene_trans_relationships {
    my ($gene_trans_map_file) = @_;

    my %gene_to_trans;

    open(my $fh, $gene_trans_map_file) or die "Error, cannot open file $gene_trans_map_file";
    while (<$fh>) {
        unless (/\w/) { next; }
        if (/^\#/) { next; }
        chomp;
        
        my ($gene_id, $trans_id) = split(/\t/);
        $gene_to_trans{$gene_id}->{$trans_id} = 1;
    }
    close $fh;
    
    return(%gene_to_trans);

}


####
sub parse_samples_file {
    my ($filename) = @_;

    my %sample_to_reps;
    
    open (my $fh, $filename) or die "Error, cannot open file $filename";
    while (<$fh>) {
        chomp;
        my ($condition, $replicate_name, @rest) = split(/\t/);
        
        $sample_to_reps{$condition}->{$replicate_name} = 1;
    }

    return(%sample_to_reps);
}


####
sub parse_expr_matrix {
    my ($expr_matrix, $sample_to_replicates_href) = @_;

    open(my $fh, $expr_matrix) or die "Error, cannot open file $expr_matrix";
    
    my $header = <$fh>;
    chomp $header;
    my @header_fields = split(/\t/, $header);
    
    my %replicate_to_col_number;

    my %trans_expr;

    my $first_data_row = 1;
    while(<$fh>) {
        chomp;
        my @x = split(/\t/);

        # process column info as needed.
        if ($first_data_row) {
            if (scalar(@header_fields) == scalar(@x) - 1) {
                # adjust header
                unshift(@header_fields, '');
            }
            # assign header column numbers
            for (my $i = 1; $i <= $#header_fields; $i++) {
                my $rep_name = $header_fields[$i];
                $replicate_to_col_number{$rep_name} = $i;
            }
            $first_data_row = 0;
        }
        
        my $trans_id = $x[0];
        
        ## process data, compute averages
        foreach my $sample_name (keys %$sample_to_replicates_href) {
            my @expr_vals;
            my @rep_names = keys %{$sample_to_replicates_href->{$sample_name}};
            foreach my $rep_name (@rep_names) {
                my $col_id = $replicate_to_col_number{$rep_name} || die "Error, no column identified for replicate name: [$rep_name] ";
                my $expr_val = $x[$col_id];
                push (@expr_vals, $expr_val);
            }
            my $avg_expr = &sum(@expr_vals) / scalar(@expr_vals);

            $trans_expr{$trans_id}->{$sample_name} = $avg_expr;
        }
    }

    return(%trans_expr);
}

####
sub sum {
    my @vals = @_;

    my $sum_val = 0;
    foreach my $v (@vals) {
        $sum_val += $v;
    }
    
    return($sum_val);
}