File: run_GOseq.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 (249 lines) | stat: -rwxr-xr-x 9,784 bytes parent folder | download
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
#!/usr/bin/env perl

use strict;
use warnings;

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

my $usage = <<__EOUSAGE__;

###############################################################################################
#
#  --factor_labeling <string>       tab delimited file with format:  factor<tab>feature_id
#   or
#  --genes_single_factor <string>   list of genes to test (can be a matrix, only the first column is used for gene IDs)
#
#  --GO_assignments <string>        extracted GO assignments with format: feature_id <tab> GO:000001,GO:00002,...
#
#  --lengths <string>               feature lengths file with format:  feature_id <tab> length
#
#  --background <string>            gene ids file that defines the full population of genes to consider in testing.
#                                   Ideally, these represent the genes that are expressed and relevant to this test
#                                   as opposed to using all genes in the genome.
#
###############################################################################################



__EOUSAGE__

    ;


my ($factor_labeling, $GO_file, $help_flag, $lengths_file, $genes_single_factor_file);
my $background_file;

&GetOptions("factor_labeling=s" => \$factor_labeling,
            "GO_assignments=s" => \$GO_file,
            "lengths=s" => \$lengths_file,
            
            "genes_single_factor=s" => \$genes_single_factor_file,
            "background=s" => \$background_file,
            "help|h" => \$help_flag,

    );


if ($help_flag) {
    die $usage;
}


unless (($factor_labeling || $genes_single_factor_file) && $GO_file && $lengths_file && $background_file) {
    die $usage;
}


main: {

    my $Rscript = "__runGOseq.R";
    open (my $ofh, ">$Rscript") or die $!;
    
    print $ofh "library(goseq)\n";
    print $ofh "library(GO.db)\n";
    print $ofh "library(qvalue)\n";
    

    print $ofh "# capture list of genes for functional enrichment testing\n";
    if ($genes_single_factor_file) {
        print $ofh "factor_labeling = read.table(\"$genes_single_factor_file\", row.names=1)\n";
        print $ofh "factor_labeling[,1] = rep('custom_list', dim(factor_labeling)[1])\n";
        print $ofh "factor_labeling = factor_labeling[,1,drop=F]\n";
    }
    else {
        print $ofh "factor_labeling = read.table(\"$factor_labeling\", row.names=2, header=F)\n";
    }
    
    print $ofh "colnames(factor_labeling) = c('type')\n";
    print $ofh "factor_list = unique(factor_labeling[,1])\n";
    print $ofh "DE_genes = rownames(factor_labeling)\n";
    

    print $ofh "\n\n# get gene lengths\n";
    print $ofh "gene_lengths = read.table(\"$lengths_file\", header=T, row.names=1, com='')\n";
    print $ofh "gene_lengths = as.matrix(gene_lengths[,1,drop=F])\n";

    print $ofh "\n\n# get background gene list\n";
    print $ofh "background = read.table(\"$background_file\", header=T, row.names=1)\n";
    print $ofh "background.gene_ids = rownames(background)\n";
    print $ofh "background.gene_ids = unique(c(background.gene_ids, DE_genes))\n"; 
    print $ofh "sample_set_gene_ids = background.gene_ids\n";
    
    print $ofh "\n\n# parse GO assignments\n";
    print $ofh "GO_info = read.table(\"$GO_file\", header=F, row.names=1,stringsAsFactors=F)\n";
    
    print $ofh "GO_info_listed = apply(GO_info, 1, function(x) unlist(strsplit(x,',')))\n";
    print $ofh "names(GO_info_listed) = rownames(GO_info)\n";

    print $ofh "get_GO_term_descr =  function(x) {\n";
    print $ofh "    d = 'none';\n"
             . "    go_info = GOTERM[[x]];\n"
             . "    if (length(go_info) >0) { d = paste(Ontology(go_info), Term(go_info), sep=' ');}\n"
             . "    return(d);\n"
             . "}\n";


    
    print $ofh "\n\n#organize go_id -> list of genes\n";  
    print $ofh "GO_to_gene_list = list()\n";
    print $ofh "for (gene_id in intersect(names(GO_info_listed), sample_set_gene_ids)) {\n";
    print $ofh "    go_list = GO_info_listed[[gene_id]]\n";
    print $ofh "    for (go_id in go_list) {\n";
    print $ofh "        GO_to_gene_list[[go_id]] = c(GO_to_gene_list[[go_id]], gene_id)\n";
    print $ofh "    }\n";
    print $ofh "}\n";
    
    
    print $ofh "\n\n# GO-Seq protocol: build pwf based on ALL DE features\n";
        
    print $ofh "missing_gene_lengths = sample_set_gene_ids[! sample_set_gene_ids %in% rownames(gene_lengths)]\n";
    print $ofh "if (length(missing_gene_lengths) > 0) {\n";
    print $ofh "     stop(\"Error, missing gene lengths for features: \", paste(missing_gene_lengths, collapse=', '))\n";
    print $ofh "}\n";
    print $ofh "sample_set_gene_lengths = gene_lengths[sample_set_gene_ids,]\n";
    print $ofh "GO_info_listed = GO_info_listed[ names(GO_info_listed) %in% sample_set_gene_ids ]\n";
    
    print $ofh "cat_genes_vec = as.integer(sample_set_gene_ids %in% rownames(factor_labeling))\n";
    
    print $ofh "pwf=nullp(cat_genes_vec, bias.data=sample_set_gene_lengths)\n";
    print $ofh "rownames(pwf) = sample_set_gene_ids\n";

    
    print $ofh "\n\n# perform functional enrichment testing for each category.\n";
    print $ofh "for (feature_cat in factor_list) {\n";
    print $ofh "    message('Processing category: ', feature_cat)\n";
    print $ofh "    gene_ids_in_feature_cat = rownames(factor_labeling)[factor_labeling\$type == feature_cat]\n";
    print $ofh "    cat_genes_vec = as.integer(sample_set_gene_ids %in% gene_ids_in_feature_cat)\n";
    print $ofh "    pwf\$DEgenes = cat_genes_vec\n";
    print $ofh "    res = goseq(pwf,gene2cat=GO_info_listed, use_genes_without_cat=TRUE)\n";
    
    ## Process the over-represented    
    print $ofh "    ## over-represented categories:\n";
    print $ofh "     pvals = res\$over_represented_pvalue\n";
    print $ofh "     pvals[pvals > 1 - 1e-10] = 1 - 1e-10\n";
    print $ofh "     q = qvalue(pvals)\n";
    print $ofh "     res\$over_represented_FDR = q\$qvalues\n";
    
    if ($genes_single_factor_file) {
        print $ofh "go_enrich_filename = paste(\"$genes_single_factor_file\", '.GOseq.enriched', sep='')\n";
    }
    else {
        print $ofh "    go_enrich_filename = paste(feature_cat,'.GOseq.enriched', sep='')\n";
    }
    print $ofh "    result_table = res[res\$over_represented_pvalue<=0.05,]\n";

    print $ofh "    descr = unlist(lapply(result_table\$category, get_GO_term_descr))\n";
    print $ofh "    result_table\$go_term = descr;\n";

    print $ofh "    result_table\$gene_ids = do.call(rbind, lapply(result_table\$category, function(x) { \n" .
               "            gene_list = GO_to_gene_list[[x]]\n" .
               "            gene_list = gene_list[gene_list %in% gene_ids_in_feature_cat]\n" .
               "            paste(gene_list, collapse=', ');\n" .
               "     }) )\n";
    
    print $ofh "    write.table(result_table[order(result_table\$over_represented_pvalue),], file=go_enrich_filename, sep='\t', quote=F, row.names=F)\n";
    

    ## Process the under-represented    
    print $ofh "    ## under-represented categories:\n";
    
    print $ofh "     pvals = res\$under_represented_pvalue\n";
    print $ofh "     pvals[pvals>1-1e-10] = 1 - 1e-10\n";
    print $ofh "     q = qvalue(pvals)\n";
    print $ofh "     res\$under_represented_FDR = q\$qvalues\n";
    
    if ($genes_single_factor_file) {
        print $ofh "    go_depleted_filename = paste(\"$genes_single_factor_file\", '.GOseq.depleted', sep='')\n";
    }
    else {
        print $ofh "    go_depleted_filename = paste(feature_cat,'.GOseq.depleted', sep='')\n";
    }
    
    print $ofh "    result_table = res[res\$under_represented_pvalue<=0.05,]\n";
    
    print $ofh "    descr = unlist(lapply(result_table\$category, get_GO_term_descr))\n";
    print $ofh "    result_table\$go_term = descr;\n";
    print $ofh "    write.table(result_table[order(result_table\$under_represented_pvalue),], file=go_depleted_filename, sep='\t', quote=F, row.names=F)\n";
    
            
    print $ofh "}\n";
    
    close $ofh;

    #my $cmd = "R --no-save --no-restore --no-site-file --no-init-file --quiet < $Rscript";
    my $cmd = "Rscript $Rscript";
    my $ret = system($cmd);
    if ($ret) {
        die "Error, cmd: $cmd died with ret $ret";
    }
    else {
        print STDERR "\n\nDone.\n\n";
    }
    

    exit(0);
}

__END__

  

Notes:

1. Get the transcript GO annotation by running Trinotate, getting a trinotate.xls report file, and then running:

     trinotate-code/util/extract_GO_assignments_from_Trinotate_xls.pl --Trinotate_xls trinotate.xls -G --include_ancestral_terms > go_annotations.txt

    # use -T instead of -G in above to get transcript instead of gene-level annotations.


2.  Run GO-Seq like so, using this script 'run_GOseq.pl' included in Trinity:

      TRINITY_HOME/Analysis/DifferentialExpression/run_GOseq.pl --factor_labeling  factor_labeling.txt  --GO_assignments go_annotations.txt --lengths gene.lengths.txt

       The 'factor_labeling.txt' file should be of format:

              gene_id (tab) factor

       where factor is a string describing that subset of genes.

       For example:

              my_gene_A (tab) diff_expressed_cond_X_Y
              my_gene_B (tab) diff_expressed_cond_X_Y
              ...
              my_gene_M (tab) diff_cond_W_Z
              my_gene_N (tab) diff_cond_W_Z
              ...

      You can come up with whatever gene subset you want and call it whatever you want.  The enrichment tests will be performed separately for 
      each factor defined.

      The gene.lengths.txt file has the format

       gene (tab) length

        and you can use the same file you used earlier as part of doing the TMM normalization step and generating your FPKM matrix.