view Length_Distibution.pl @ 44:0c4e11018934 draft

Uploaded
author big-tiandm
date Thu, 30 Oct 2014 21:29:19 -0400
parents
children
line wrap: on
line source

#!/usr/bin/perl -w
#==========================================================================================
# Date: 
# Title: 
# Comment: Program to plot gene structure
# Input: 1. input file of Gene region annotation which format like GenePred
#        2. input file of Transcripts region annotation which format like GenePred
#        3. input file of gene snp detail info
# Output: output file of gene structure graph by html or svg formt
# Test Usage: 
#========================================================================================
#use strict;
my $version=1.00;
use SVG;
use Getopt::Long;
my %opt;
GetOptions(\%opt,"i=s","o=s",,"h");
if (!(defined $opt{i} and defined $opt{o}) || defined $opt{h}) {
&usage;
}
#===============================Define Attribute==========================================
my %attribute=(
	canvas=>{
		'width'=>1500,
		'height'=>1800
	},
	text=>{
		'stroke'=>"#000000",
		'fill'=>"none",
		'stroke-width'=>0.5
		#'stroke-width2'=>1
	},
	line=>{
		'stroke'=>"black",
		'stroke-width'=>1
	},
	font=>{
		'fill'=>"#000000",
		'font-size'=>12,
		'font-size2'=>10,
		'font-weight'=>'bold',
		'font-family'=>"Arial"
		#'font-family2'=>"ArialNarrow-bold"
	},
	rect=>{
		'fill'=>"lightgreen",
		'stroke'=>"black",
		'stroke-width'=>0.5
	},
	readwidth=>0.5
);
#my $Xscale=600/$length;#定义X轴比例尺 1:1000 x轴的坐标长度都要按照此比例尺换算
#========================================data============================
open(IN,"$opt{i}")||die"cannot open the file $opt{i}";
my @R_length;
my @T_length;
my $R_number=0;
my $T_number=0;
my $R_max=0;
my $T_max=0;

my $title=<IN>;
chomp $title;
my @title=split/\t/,$title;
my @mark=split/\s+/,$title[1];
my $sample_number=@mark;
while (my $aline=<IN>) {
	if ($aline=~/^\s/) {
		my $T_title=<IN>;
		chomp $T_title;
		while (my $a_aline=<IN>) {
			chomp $a_aline;
			my @temp=split/\t/,$a_aline;
			my @number=split/\s+/,$temp[1];
			for (my $i=0;$i<@number ;$i++) {
				if ($R_max<$number[$i]) {
					$R_max=$number[$i];
				}
			}
			push @R_length,[$temp[0],@number];
			$R_number++;
		}
	}
	else {
		chomp $aline;
		my @temp=split/\t/,$aline;
		my @number=split/\s+/,$temp[1];
		for (my $i=0;$i<@number ;$i++) {
			if ($T_max<$number[$i]) {
				$T_max=$number[$i];
			}
		}
		push @T_length,[$temp[0],@number];
		$T_number++;
	}
}
close IN;
print "Tag max: $T_max\nRead max: $R_max\n";
my $kd_number=5;
##=======================Reads 纵坐标刻度==========================
my $r=1;
my $rr=1;
my $R=$R_max;
while ($R>10) {
	$R=$R/10;
	$r=$r*10;
	$rr++;
}
$R=int($R+0.5);
my $R_xg=$R/$kd_number*$r;#纵坐标一小格大小(一共10格)
my $R_kedu_scale_x=6*$rr;#纵坐标刻度文字
##=======================Tags 纵坐标刻度==========================
my $t=1;
my $tt=1;
my $T=$T_max;
while ($T>10) {
	$T=$T/10;
	$t=$t*10;
	$tt++;
}
$T=int($T+0.5);
my $T_xg=$T/$kd_number*$t;#纵坐标一小格大小(一共10格)
my $T_kedu_scale_x=6*$tt;#纵坐标刻度文字

#############################s#define start coordinate and scale
my $XOFFSET=50;
my $YOFFSET=60;
my $width=800;
my $heigth=800;
my $X_width=600;
#my $height=1600;
####    Starting    ####
#新建画布
my $svg=SVG->new(width=>$width,height=>$heigth);
####坐标轴
my $axisL=300;#read 纵坐标长度
my $x_margin = 50;
#=========Reads number setting==========================================
my $Y_R_title=30;#标题的纵向宽度
my $Y_R_0=$YOFFSET+$axisL+$Y_R_title;
my $X_R_0=$XOFFSET+$x_margin;
my $R_Yscale=$axisL/$R_xg/$kd_number;
my $R_Xscale=$X_width/$R_number/($sample_number+1);
#=====================================Reads Y axis======================
$svg->line('x1',$X_R_0,'y1',$Y_R_0,'x2',$X_R_0,'y2',$Y_R_0-$axisL,'stroke',$attribute{line}{'stroke'},'stroke-width',$attribute{line}{'stroke-width'});
for (my $i=1;$i<$kd_number ;$i++) {
	$svg->line('x1',$X_R_0-5,'y1',$Y_R_0-$i*$R_xg*$R_Yscale,'x2',$X_R_0,'y2',$Y_R_0-$i*$R_xg*$R_Yscale,'stroke',$attribute{line}{'stroke'},'stroke-width',$attribute{line}{'stroke-width'});
	$svg->text('x',$X_R_0-$R_kedu_scale_x,'y',$Y_R_0-$i*$R_xg*$R_Yscale+4,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',$attribute{text}{'stroke-width'},'font-size',6,'font-family',$attribute{font}{'font-family'},'-cdata',$i*$R_xg);
}
#=====================================Reads X axis======================
$svg->line('x1',$X_R_0,'y1',$Y_R_0,'x2',$X_R_0+$X_width,'y2',$Y_R_0,'stroke',$attribute{line}{'stroke'},'stroke-width',$attribute{line}{'stroke-width'});

#print "$R_number\t$sample_number\n";
for ($i=0;$i<$R_number ;$i++) {
	for (my $j=1;$j<$sample_number+1 ;$j++) {
		my $red=$j/$sample_number*255;
		$svg->rect('x',$X_R_0+($j+$i*($sample_number+1))*$R_Xscale,'y',$Y_R_0-$R_length[$i][$j]*$R_Yscale,'width',$R_Xscale,'height',$R_length[$i][$j]*$R_Yscale,'stroke',"black",'stroke-width',"0.5",'fill',"rgb($red,125,0)");
	}
	$svg->text('x',$X_R_0+(1+$sample_number/2+$i*($sample_number+1))*$R_Xscale,'y',$Y_R_0+15,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',$attribute{text}{'stroke-width'},'font-size',6,'font-family',$attribute{font}{'font-family'},'-cdata',$R_length[$i][0]);
}
#===Reads number title
$svg->text('x',$XOFFSET+400,'y',$YOFFSET+$Y_R_title,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',"1",'font-size',15,'font-family',$attribute{font}{'font-family'},'-cdata',"Reads Length Distribution");
#===Reads 
for (my $i=0;$i<$sample_number ;$i++) {
	my $red=($i+1)/$sample_number*255;
	$svg->rect('x',$X_R_0+550,'y',$YOFFSET+$Y_R_title+20*$i,'width',15,'height',10,'stroke',"black",'stroke-width',"0.5",'fill',"rgb($red,125,0)");
	$svg->text('x',$X_R_0+550+30,'y',$YOFFSET+$Y_R_title+20*$i+10,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',$attribute{text}{'stroke-width'},'font-size',10,'font-family',$attribute{font}{'font-family'},'-cdata',$mark[$i]);
}
####==================================================================================
#=========================================Tag s
my $Y_T_title=30;#标题的纵向宽度
my $Y_T_0=$Y_R_0+$axisL+$Y_R_title+50;#length size
my $X_T_0=$XOFFSET+$x_margin;
my $T_Yscale=$axisL/$T_xg/$kd_number;
my $T_Xscale=$X_width/$T_number/($sample_number+1);
#=====================================Tags Y axis======================
$svg->line('x1',$X_T_0,'y1',$Y_T_0,'x2',$X_T_0,'y2',$Y_T_0-$axisL,'stroke',$attribute{line}{'stroke'},'stroke-width',$attribute{line}{'stroke-width'});
for (my $i=1;$i<$kd_number ;$i++) {
	$svg->line('x1',$X_T_0-5,'y1',$Y_T_0-$i*$T_xg*$T_Yscale,'x2',$X_T_0,'y2',$Y_T_0-$i*$T_xg*$T_Yscale,'stroke',$attribute{line}{'stroke'},'stroke-width',$attribute{line}{'stroke-width'});
	$svg->text('x',$X_T_0-$T_kedu_scale_x,'y',$Y_T_0-$i*$T_xg*$T_Yscale+4,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',$attribute{text}{'stroke-width'},'font-size',6,'font-family',$attribute{font}{'font-family'},'-cdata',$i*$T_xg);
}
#=====================================Tags X axis======================
$svg->line('x1',$X_T_0,'y1',$Y_T_0,'x2',$X_T_0+$X_width,'y2',$Y_T_0,'stroke',$attribute{line}{'stroke'},'stroke-width',$attribute{line}{'stroke-width'});

#print "$R_number\t$sample_number\n";
for ($i=0;$i<$T_number ;$i++) {
	for (my $j=1;$j<$sample_number+1 ;$j++) {
		my $red=$j/$sample_number*255;
		$svg->rect('x',$X_T_0+($j+$i*($sample_number+1))*$T_Xscale,'y',$Y_T_0-$T_length[$i][$j]*$T_Yscale,'width',$T_Xscale,'height',$T_length[$i][$j]*$T_Yscale,'stroke',"black",'stroke-width',"0.5",'fill',"rgb($red,125,0)");
	}
	$svg->text('x',$X_T_0+(1+$sample_number/2+$i*($sample_number+1))*$T_Xscale,'y',$Y_T_0+15,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',$attribute{text}{'stroke-width'},'font-size',6,'font-family',$attribute{font}{'font-family'},'-cdata',$T_length[$i][0]);
}
#===Reads number title
$svg->text('x',$XOFFSET+400,'y',$Y_R_0+30+$Y_T_title,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',"1",'font-size',15,'font-family',$attribute{font}{'font-family'},'-cdata',"Tags Length Distribution");
#===Reads 
for (my $i=0;$i<$sample_number ;$i++) {
	my $red=($i+1)/$sample_number*255;
	$svg->rect('x',$X_T_0+550,'y',$Y_R_0+30+$Y_T_title+20*$i,'width',15,'height',10,'stroke',"black",'stroke-width',"0.5",'fill',"rgb($red,125,0)");
	$svg->text('x',$X_T_0+550+30,'y',$Y_R_0+30+$Y_T_title+20*$i+10,'style','fill:black;text-anchor:middle','stroke',$attribute{text}{'stroke'},'stroke-width',$attribute{text}{'stroke-width'},'font-size',10,'font-family',$attribute{font}{'font-family'},'-cdata',$mark[$i]);
}




open (OUT,">$opt{o}");
print OUT $svg->xmlify();

sub usage{
print <<"USAGE";
Version $version
Usage:
$0 
options:
-i 
-o svg output
-h help
USAGE
exit(1);
}