Advertisement
Guest User

Perl-R-Code

a guest
Oct 11th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.97 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use Getopt::Long;
  5. use Statistics::R;
  6.  
  7. my ($temp_dir,$file_name,$file_type,$pval_col,$chart_title,$output_format,$plot_name);
  8.  
  9. usage() if (@ARGV < 1 or !GetOptions('tempdir=s' => \$temp_dir, 'filename=s' => \$file_name, 'filetype=s' => \$file_type, 'pvalcol=s' => \$pval_col, 'charttitle:s' => \$chart_title, 'outputformat=s' => \$output_format, 'plotname=s' => \$plot_name));
  10.  
  11. chdir($temp_dir);
  12.  
  13. my $R = Statistics::R->new();
  14.  
  15. $R -> run(qq(name="$chart_title"));
  16.  
  17. $R -> run(qq(ylabel = "Observed P-Vlaues (-log10)"));
  18. $R -> run(qq(xlabel = "Theoretical P-Values (-log10)"));
  19.  
  20.  
  21. if ($file_type eq "text/csv") {
  22.         $R -> run(qq(traitfile <- read.csv("$file_name", header=TRUE, sep=",", as.is="$pval_col", na.strings="nan", stringsAsFactors=FALSE)));
  23. } elsif ($file_type eq "text/plain") {
  24.         $R -> run(qq(traitfile <- read.table("$file_name", header=TRUE, as.is="$pval_col", na.strings="nan", stringsAsFactors=FALSE)));
  25. }
  26.  
  27. $R -> run(qq(ylimit = -log10(min(traitfile\$$pval_col ,na.rm=TRUE))+1));
  28. $R -> run(qq(tlen <- length(traitfile\$$pval_col)));
  29. $R -> run(qq(degf <- length(traitfile\$$pval_col) - 2));
  30. $R -> run(qq(a <- rt(tlen, degf)));
  31. $R -> run(qq(b <- pt(a, degf)));
  32. $R -> run(qq(c <- -log10(traitfile\$$pval_col)));
  33. $R -> run(qq(d <- -log10(b)));
  34. $R -> run(qq(c[c > ylimit] <- ylimit));
  35. $R -> run(qq(traitfile.fit <- lm(c ~ d)));
  36.  
  37. if ($output_format eq "png") {
  38.         $R -> run(qq(png(filename="$plot_name", width=800, height=800, units="px", pointsize=12, bg="white", type="cairo")));
  39. } elsif ($output_format eq "jpeg") {
  40.         $R -> run(qq(jpeg(filename="$plot_name", width=800, height=800, units="px", pointsize=12, bg="white", type="cairo")));
  41. } elsif ($output_format eq "pdf") {
  42.         $R -> run(qq(pdf("$plot_name", width = 10, height = 10)));
  43. }
  44.  
  45. $R -> run(qq(qqplot(d, c, main=name, ylab = "Observed P-Values (-log10)", xlab ="Theoretical P-Values (-log10)", ylim=c(0,ylimit), xlim=c(0,ylimit), pch=21, cex =0.5)));
  46. $R -> run(qq(deno<-0.456));
  47. $R -> run(qq(w1<-1.0-traitfile\$$pval_col));
  48. $R -> run(qq(w2<-qchisq(w1,df=1)));
  49. $R -> run(qq(lam1<-median(w2)/deno));
  50. $R -> run(qq(text(0.1, ylimit, paste("GC Lambda= ", format(round(lam1,2), nsmall = 2)), pos=4, cex=1.5, col="darkgreen")));
  51. $R -> run(qq(abline(a=0, b=1, col="blue")));
  52. $R -> run(qq(dev.off()));
  53.  
  54. my $ret= $R -> read;
  55. $R -> stopR();
  56.  
  57. exit;
  58.  
  59. sub usage {
  60.         print "Unknown option: @_\n" if (@_);
  61.         print "Usage: test_qqPlot.pl [options]
  62.                      [--tempdir ----- full path of temp directory]
  63.                      [--filename ----- input file name. this file contains data for Q-Q plot]
  64.                      [--filetype ----- type of uploaded file]
  65.                      [--pvalcol ------ column name in which p-values are stored]
  66.                      [--charttitle ----- title for chart]
  67.                      [--outputformat ----- format for plot]
  68.                      [--plotname ----- name for plot]\n";
  69.         exit;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement