File: extract_GO_enriched_genes.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 (165 lines) | stat: -rwxr-xr-x 3,887 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Carp;

use Getopt::Long qw(:config no_ignore_case bundling pass_through);


my $help_flag;

my $usage = <<__EOUSAGE__;


####################################################################
#
#  Required:
#
#  --GO_annots <string>            GO assignments to genes.
#
#  --gene_factors_file <string>    gene factors file
#
#  --factor <string>               the gene factor defining the genes of interest.
#
#  --GO_id <string>                GO ID to examine.
#
#  --fpkm_matrix <string>          FPKM matrix file.
#
#  Optional:
#
#  --output_prefix <string>        output prefix  (default: 'factor,GO_id')
#
#  --samples <string>              samples description file (for heatmaps)
#
####################################################################

__EOUSAGE__

    ;

my $GO_annots_file;
my $gene_factors_file;
my $factor;
my $GO_id;
my $fpkm_matrix;
my $output_prefix;
my $samples_file;

&GetOptions ( 'h' => \$help_flag,
              'GO_annots=s' => \$GO_annots_file,
              'gene_factors_file=s' => \$gene_factors_file,
              'factor=s' => \$factor,
              'GO_id=s' => \$GO_id,
              'fpkm_matrix=s' => \$fpkm_matrix,
              'output_prefix=s' => \$output_prefix,
              'samples=s' => \$samples_file,
    );


if ($help_flag) { 
    die $usage;
}

unless ($GO_annots_file && $gene_factors_file && $factor && $GO_id && $fpkm_matrix) {
    die $usage;
}

unless ($output_prefix) {
    $output_prefix = join(",", $factor, $GO_id);
}

main: {

    my %genes = &get_genes_assigned_to_factor($gene_factors_file, $factor);
    
    my %genes_with_GO = &get_subset_of_genes_with_GO_id($GO_annots_file, $GO_id, \%genes);

    my $fpkm_outfile = "$output_prefix.fpkm";
    open (my $ofh, ">$fpkm_outfile") or die "Error, cannot write to $fpkm_outfile";
    open (my $fh, $fpkm_matrix) or die "Error, cannot open file $fpkm_matrix";
    my $header = <$fh>;
    print $ofh $header;
    
    my %genes_want = %genes_with_GO;
    while (<$fh>) {
        my $line = $_;
        my @x = split(/\t/);
        my $acc = $x[0];
        if ($genes_with_GO{$acc}) {
            print $ofh $line;
            delete $genes_want{$acc};
        }
    }
    close $ofh;

    if (%genes_want) {
        use Data::Dumper;
        die "Error, didn't find fpkm matrix rows for genes: " . Dumper(\%genes_want);
    }
    
    ## generate a heatmap
    my $cmd = "/usr/lib/trinityrnaseq/Analysis/DifferentialExpression/PtR -m $fpkm_outfile --log2 --heatmap --gene_dist euclidean --sample_dist euclidean";
    if ($samples_file) {
        $cmd .= " -s $samples_file ";
    }
    
    print STDERR "CMD: $cmd\n";
    my $ret = system($cmd);
    if ($ret) {
        die "Error, CMD: $cmd died with ret $ret";
    }
    
    exit(0);
    
}

####
sub get_genes_assigned_to_factor {
    my ($gene_factors_file, $factor_want) = @_;

    my %genes;

    open (my $fh, $gene_factors_file) or die $!;
    while (<$fh>) {
        chomp;
        my ($gene, $factor) = split(/\t/);
        if ($factor eq $factor_want) {
            
            $genes{$gene} = 1;
        }
    }
    close $fh;

    unless (%genes) {
        confess "Error, no genes found assigned to factor [$factor_want] in file $gene_factors_file";
    }

    return(%genes);
}


####
sub get_subset_of_genes_with_GO_id {
    my ($GO_annots_file, $GO_id, $genes_href) = @_;

    my %genes_with_GO;

    open (my $fh, $GO_annots_file) or die $!;
    while (<$fh>) {
        chomp;
        my ($gene, $go_annots) = split(/\t/);
        if ($genes_href->{$gene} && $go_annots =~ /$GO_id/) {
            $genes_with_GO{$gene} = 1;
        }
    }
    close $fh;

    unless (%genes_with_GO) {
        confess "Error, no genes found with GO: [$GO_id] in file: $GO_annots_file for genes: " . Dumper($genes_href);
    }

    return(%genes_with_GO);
}