File: DE_results_to_pairwise_summary.pl

package info (click to toggle)
trinityrnaseq 2.2.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 212,452 kB
  • ctags: 5,067
  • sloc: perl: 45,552; cpp: 19,678; java: 11,865; sh: 1,485; makefile: 613; ansic: 427; python: 313; xml: 83
file content (160 lines) | stat: -rwxr-xr-x 5,153 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

my $usage = "usage: $0 sample_avg_expr.matrix edgeR_directory/ [FDR=0.05]\n\n";

my $sample_expr_matrix = $ARGV[0] or die $usage;
my $DE_dir = $ARGV[1] or die $usage;
my $MAX_FDR = $ARGV[2];
unless (defined $MAX_FDR) {
    $MAX_FDR = 0.05;
}

my @DE_result_files = <$DE_dir/*.DE_results>;
unless (@DE_result_files) {
    die "Error, cannot find \*.DE_results files at $DE_dir ";
}

main: {
    

    my %gene_to_sample_expr_val = &parse_expression_matrix($sample_expr_matrix);

    print join("\t", "#feature", "sample_A", "sample_B", "log2(exprA)", "log2(exprB)", "logFC", "FDR") . "\n";
    
    foreach my $DE_result_file (@DE_result_files) {
        print STDERR "-processing DE file: $DE_result_file\n";
        
        $DE_result_file =~ /\.([^\.\/]+)_vs_([^\.\/]+).[^\.]+.DE_results/ or die "Error, cannot parse filename: $DE_result_file";
        my $sample_A = $1;
        my $sample_B = $2;
        
        open (my $fh, $DE_result_file) or die $!;
        my $header = <$fh>;
        chomp $header;
        my @header_fields = split(/\t/, $header);
        my $FDR_field = undef;

        my $line_counter = 0;
        while(<$fh>) {
            $line_counter++;
            chomp;
            my @x = split(/\t/);

            # match up header with data fields.
            if ($line_counter == 1) {
                if (scalar(@header_fields) == scalar(@x) -1) {
                    unshift(@header_fields, 'id');
                }
                elsif (scalar(@header_fields) != scalar(@x)) {
                    die "Error, disconnect between header line and data line, number of fields are unequal and header isn't one short:\n"
                        . "header: @header_fields\n"
                        . "line: @x\n";
                }
                for (my $i = 0; $i <= $#header_fields; $i++) {
                    if ($header_fields[$i] eq 'FDR') {
                        $FDR_field = $i;
                    }
                }
                unless ($FDR_field) {
                    die "Error, couldn't identify column corresponding to FDR";
                }
            }
            
            my $feature = $x[0];
            my $feature_expr = $gene_to_sample_expr_val{$feature};
            unless (defined $feature_expr) {
                die "Error, no expression values stored for [$feature] ";
            }
            

            my $FDR = $x[$FDR_field];
            if ($FDR <= $MAX_FDR) {
                my $expr_sample_A = $feature_expr->{$sample_A};
                my $expr_sample_B = $feature_expr->{$sample_B};
                
                unless (defined $expr_sample_A && defined $expr_sample_B) {
                    die "Error, no expr value for feature: $feature, $sample_A [$expr_sample_A] or $sample_B [$expr_sample_B] " . Dumper($gene_to_sample_expr_val{$feature});
                }
                my $log_expr_sample_A = log($expr_sample_A+1)/log(2);
                my $log_expr_sample_B = log($expr_sample_B+1)/log(2);
                
                my $log_FC = sprintf("%.2f", $log_expr_sample_A - $log_expr_sample_B);

                print join("\t", $feature, $sample_A, $sample_B, $log_expr_sample_A, $log_expr_sample_B, $log_FC, $FDR) . "\n";
            }
        }
    }

    print STDERR "\nDone\n\n";

    exit(0);

}


####
sub parse_expression_matrix {
    my ($expr_matrix_file) = @_;

    print STDERR "\nReading matrix: $expr_matrix_file ... ";

    my $cmd = "wc -l $expr_matrix_file ";
    
    my $num_lines = `$cmd`;
    if ($?) {
        die "Error, cmd; $cmd died with ret $?";
    }
    $num_lines =~ /(\d+)/;
    $num_lines = $1 or die "Error, cannot count number of lines from: $num_lines, cmd: $cmd";
    
    print STDERR " $num_lines rows of matrix detected.\n\n";
    
    my %gene_to_sample_expr_val;

    open (my $fh, $expr_matrix_file) or die "Error, cannot open file $expr_matrix_file";
    my $header = <$fh>;
    chomp $header;
    $header =~ s/^\s+//;
    my @sample_names = split(/\t/, $header);
    
    my $counter = 0;
    while (<$fh>) {
        chomp;
        my @x = split(/\t/);
        my $feature_name = shift @x;

        #unless ($feature_name eq "c1088792_g2_i5^sp|Q06441|TSP4_XENLA^COMP^sigP") { next; }
        
        unless (scalar @x == scalar @sample_names) {
            die "Error, number of samples: " . scalar (@sample_names) . " doesn't match number of values read: " . scalar(@x) . "  ";
        }
        
        for (my $i = 0; $i <= $#sample_names; $i++) {
            my $sample = $sample_names[$i];
            my $val = $x[$i];
            
            $gene_to_sample_expr_val{$feature_name}->{$sample} = $val;
        }
        

        $counter++;
        if ($counter % 10000 == 0) {
            my $pct_done = sprintf("%.2f", $counter/$num_lines * 100);
            print STDERR "\r[$pct_done %] matrix read.   ";
        }
        #if ($counter > 10) { last; } # debug
    }

    close $fh;

    print STDERR "\n\nDone reading matrix.\n";

    #print Dumper(\%gene_to_sample_expr_val);
    
    return(%gene_to_sample_expr_val);
}