annotate tools/regVariation/draw_stacked_barplots.pl @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/perl -w
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # This program draws, in a pdf file, a stacked bars plot for different categories of data and for
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 # different criteria. For each criterion a stacked bar is drawn, such that the height of each stacked
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 # sub-bar represents the number of elements in each category satisfying that criterion.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 # The input consists of a TABULAR format file, where the left column represents the names of categories
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 # and the other columns are headed by the names of criteria, such that each data value in the file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 # represents the number of elements in a certain category satisfying a certain criterion.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 # The output is a PDF file containing a stacked bars plot representing the number of elements in each
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 # category satisfying each criterion. The drawing is done using R code.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 use strict;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 use warnings;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 my $criterion;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 my @criteriaArray = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 my $criteriaNumber = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 my $lineCounter = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 #variable to store the names of R script file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 my $r_script;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 # check to make sure having correct files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 my $usage = "usage: draw_stacked_bar_plot.pl [TABULAR.in] [PDF.out] \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 die $usage unless @ARGV == 2;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 my $categoriesInputFile = $ARGV[0];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 my $categories_criteria_bars_plot_outputFile = $ARGV[1];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 #open the input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 open (INPUT, "<", $categoriesInputFile) || die("Could not open file $categoriesInputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 open (OUTPUT, ">", $categories_criteria_bars_plot_outputFile) || die("Could not open file $categories_criteria_bars_plot_outputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 # R script to implement the drawing of a stacked bar plot representing thes significant motifs in each category of motifs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 #construct an R script file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 $r_script = "motif_significance_bar_plot.r";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 open(Rcmd,">", $r_script) or die "Cannot open $r_script \n\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 print Rcmd "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 #store the table content of the first file into a matrix
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 categoriesTable <- read.table(\"$categoriesInputFile\", header = TRUE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 categoriesMatrix <- as.matrix(categoriesTable);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 #compute the sum of elements in the column with the maximum sum in each matrix
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 columnSumsVector <- colSums(categoriesMatrix);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 maxColumn <- max (columnSumsVector);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 if (maxColumn %% 10 != 0){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 maxColumn <- maxColumn + 10;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 plotHeight = maxColumn/8;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 criteriaVector <- names(categoriesTable);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 pdf(file = \"$categories_criteria_bars_plot_outputFile\", width = length(criteriaVector), height = plotHeight, family = \"Times\", pointsize = 12, onefile = TRUE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 #draw the first barplot
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 barplot(categoriesMatrix, ylab = \"No. of elements in each category\", xlab = \"Criteria\", ylim = range(0, maxColumn), col = \"black\", density = c(10, 20, 30, 40, 50, 60, 70, 80), angle = c(45, 90, 135), names.arg = criteriaVector);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 #draw the legend
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 legendX = 0.2;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 legendY = maxColumn;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 legend (legendX, legendY, legend = rownames(categoriesMatrix), density = c(10, 20, 30, 40, 50, 60, 70, 80), angle = c(45, 90, 135));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 dev.off();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 #eof\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 close Rcmd;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 #close the input files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 close(OUTPUT);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 close(INPUT);