Source code

Download source archive ↓

upstream-extended/devteam/tools/draw_stacked_barplots/draw_stacked_barplots.pl

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