view sam2Bed_bowtie.pl @ 14:36ba71d63988 draft

Uploaded
author big-tiandm
date Wed, 29 Oct 2014 04:19:59 -0400
parents 07745c0958dd
children
line wrap: on
line source

#!/usr/bin/perl -w
#Filename:
#Author: Tian Dongmei
#Email: tiandm@big.ac.cn
#Date: 2011/11/7
#Modified:
#Description: sam2BED 
my $version=1.00;

use strict;
use Getopt::Long;

my %opts;
GetOptions(\%opts,"i=s","mark=s","o=s","h");
if (!(defined $opts{i} and defined $opts{o}) || defined $opts{h}) { #necessary arguments
&usage;
}

my $filein=$opts{'i'};
my $fileout=$opts{'o'};
my $mark=$opts{'mark'};
my @sample=split/\#/,$mark;
$mark=join"\t",@sample;
open OUT,">$fileout"; #output file  
print OUT "#chr\tstrand\tstart\tend\t$mark\n";

open IN,"<$filein"; #input file  
my $Tags_num=0;
my @read_num;
#print OUT "#chr\tstart\tend\tnum\t<=20\t21\t22\t23\t24\t>=25\n";
while (my $aline=<IN>) {
	chomp $aline;
	next if($aline=~/^\@/);
	my @tmp=split/\t/,$aline;
	my $strand=$tmp[1];
	my $start=$tmp[3]+1;
	my $length=length($tmp[4]);
	my $end=$start+$length-1;
	my $hit=$tmp[6]+1;
	#======express caculate weighted===================================
		my $exp;
		my @tempID=split/\:/,$tmp[0];
		my @exp=split/\_/,$tempID[1];
		pop @exp;
		for (my $j=0;$j<@exp ;$j++) {
			#my @tempID1=split/\=/,$tempID[$j];
			$exp[$j]=sprintf("%.2f",$exp[$j]/$hit);
			$read_num[$j]+=$exp[$j];
			#print OUT "\t$exp";
		}
		$exp=join "\t",@exp;
		print OUT $tmp[2],"\t",$strand,"\t",$start,"\t",$end,"\t",$exp,"\n";
		$Tags_num++;

}
print "Total Tags numer: $Tags_num\n";
my $read_number=join "\t",@read_num;
print "Each sample numer: $read_number\n";
close IN;
close OUT;
sub usage{
print <<"USAGE";
Version $version
Usage:
$0 -i -o
options:
-i input file
-mark sampleA	sampleB	sampleC.....
-o output file
-h help
USAGE
exit(1);
}