| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # This script will run a FDR FET analysis. | ||
| 4 | # It requires two files, a list of genes with P values, see t/HIV-loci-pval.inp | ||
| 5 | # for an example, and a list of pathways, see t/example_pathways.txt for | ||
| 6 | # an example. The results go into the outdir directory. The other options | ||
| 7 | # are described in the FdrFet.pm documentation. | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use Bio::FdrFet; | ||
| 11 | use Getopt::Long; | ||
| 12 | |||
| 13 | my $fdrcutoff = 35; | ||
| 14 | my ($genefile, $pathwayfile, $outdir); | ||
| 15 | my $help = 0; | ||
| 16 | my $verbose = 0; | ||
| 17 | my $universe = "genes"; | ||
| 18 | my $genecount = undef; | ||
| 19 | my $arrays = 0; | ||
| 20 | &GetOptions("fdr=i" => \$fdrcutoff, | ||
| 21 | "genelist=s" => \$genefile, | ||
| 22 | "pathway=s" => \$pathwayfile, | ||
| 23 | "outdir=s" => \$outdir, | ||
| 24 | "help!" => \$help, | ||
| 25 | "verbose!" => \$verbose, | ||
| 26 | "universe=s" => \$universe, | ||
| 27 | "genecount=i" => \$genecount, | ||
| 28 | "arrays!" => \$arrays | ||
| 29 | ); | ||
| 30 | |||
| 31 | my $usage = <<EOF; | ||
| 32 | fdr-fet.pl -fdr=integer | ||
| 33 | -genelist=file | ||
| 34 | -pathway=file | ||
| 35 | -outdir=output_directory | ||
| 36 | -universe=universe_setting | ||
| 37 | -genecount=integer | ||
| 38 | -[no]help | ||
| 39 | -[no]verbose | ||
| 40 | -[no]arrays | ||
| 41 | EOF | ||
| 42 | |||
| 43 | if (not $outdir or | ||
| 44 | not $genefile or | ||
| 45 | not $pathwayfile or | ||
| 46 | $help) { | ||
| 47 | print STDERR $usage; | ||
| 48 | exit(1); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $obj = new Bio::FdrFet($fdrcutoff); | ||
| 52 | $obj->verbose($verbose); | ||
| 53 | $obj->universe($universe); | ||
| 54 | #read in gene set/pathway data | ||
| 55 | |||
| 56 | open (IN, $pathwayfile) || die "can not open pathway annotation file $pathwayfile: $!\n"; | ||
| 57 | while (<IN>) { | ||
| 58 | chomp; | ||
| 59 | my ($gene, $dbacc, $desc, $rest) = split (/\t/, $_, 4); | ||
| 60 | $obj->add_to_pathway("gene" => $gene, | ||
| 61 | "dbacc" => $dbacc, | ||
| 62 | "desc" => $desc); | ||
| 63 | } | ||
| 64 | close IN; | ||
| 65 | |||
| 66 | #read in genes and associated p values | ||
| 67 | my (%genes, @fdrs); | ||
| 68 | open (IN, $genefile) || die "can not open gene file $genefile: $!\n"; | ||
| 69 | my $ref_size = 0; | ||
| 70 | while (<IN>) { | ||
| 71 | my ($gene, $pval) = split (/\t/, $_); | ||
| 72 | $obj->add_to_genes("gene" => $gene, | ||
| 73 | "pval" => $pval); | ||
| 74 | $ref_size++; | ||
| 75 | } | ||
| 76 | close IN; | ||
| 77 | if ($universe eq 'user') { | ||
| 78 | $obj->gene_count($genecount); | ||
| 79 | } | ||
| 80 | $obj->calculate; | ||
| 81 | |||
| 82 | if (not -d $outdir) { | ||
| 83 | system("mkdir -p $outdir"); | ||
| 84 | } | ||
| 85 | open (PATH, ">$outdir/$pathwayfile.fdr$fdrcutoff.detail.out") || | ||
| 86 | die "can not open pathway analysis detail file: $!\n"; | ||
| 87 | |||
| 88 | foreach my $p ($obj->pathways) { | ||
| 89 | foreach my $locus (@{$obj->pathway_result($p, 'loci')}) { | ||
| 90 | my @components = map {$obj->pathway_result($p, $_)} ('odds', | ||
| 91 | 'q', | ||
| 92 | 'm', | ||
| 93 | 'n', | ||
| 94 | 'k'); | ||
| 95 | $components[0] = sprintf("%.2f", $components[0]); | ||
| 96 | my $rest = join(':', @components); | ||
| 97 | if ($components[0] == 0) { | ||
| 98 | $rest = '0.00:'; | ||
| 99 | } | ||
| 100 | printf PATH "%s\t%s\t%.2f\t$rest\t$locus\n", | ||
| 101 | $p, | ||
| 102 | $obj->pathway_desc($p), | ||
| 103 | $obj->pathway_result($p, 'logpval'); | ||
| 104 | } | ||
| 105 | if ($arrays) { | ||
| 106 | print "Log Pval for pathway $p:\n"; | ||
| 107 | my @logpval = $obj->pathway_result($p, 'logpval', 'all'); | ||
| 108 | for (my $i = 0; $i < $fdrcutoff; $i++) { | ||
| 109 | printf "%d\t%.2f\n", $i+1, $logpval[$i]; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | close PATH; | ||
| 114 | |||
| 115 | #output pathways and their P values from GSEA | ||
| 116 | open (OUT, ">$outdir/$pathwayfile.out") || die "can not open pathway analysis output file: $!\n"; | ||
| 117 | foreach my $p ($obj->pathways) { | ||
| 118 | printf OUT "$p\t%s\t%.2f\n", $obj->pathway_desc($p), $obj->pathway_result($p, 'logpval'); | ||
| 119 | } | ||
| 120 | close OUT; |