view methylation_analysis_bismark/methylation_analysis/methylation_extractor_wrapper.pl @ 9:5b208d4d89e5 draft

Uploaded
author fcaramia
date Tue, 04 Dec 2012 20:15:26 -0500
parents d15b4a2e3bdc
children
line wrap: on
line source

use strict;
use warnings;
use File::Basename; 
use Cwd;
use File::Path qw(make_path remove_tree);
die qq(
Bad numbr of inputs

) if(!@ARGV);

my @bam_list_entries;

my $player_options = "";
my $bam_file;
my $ending;
my $genome;
my $summary ;
my $output;

foreach my $input (@ARGV) 
{
	my @tmp = split "::", $input;
	if($tmp[0] eq "GENOME") 
	{
		$genome=$tmp[1];	
	} 
	elsif($tmp[0] eq "OPTION") 
	{
		$player_options = "$player_options ${tmp[1]}";
	} 
	elsif($tmp[0] eq "ENDING") 
	{
		$ending = $tmp[1];
	} 
        elsif($tmp[0] eq "BAMFILE") 
	{
		$bam_file = $tmp[1];
	}
	elsif($tmp[0] eq "SUMMARY") 
	{
		$summary = $tmp[1];
	} 
	elsif($tmp[0] eq "OUTPUT") 
	{
		$output = $tmp[1];
	}
        else 
	{
		die("Unknown Input: $input\n");
	}
}


my $working_dir = cwd();

#convert bam to sam
my $sam_file = "$working_dir/coverted.sam";
system("samtools view -h $bam_file > $sam_file");

#run bismark 

system ("bismark_methylation_extractor $ending $player_options --genome_folder $genome $sam_file > $summary 2>&1");

move_files($working_dir);

sub move_files
{
	my $name;
	my $suffix;
	my $path;
	my $local_dir = $_[0];
	opendir(DIR, $local_dir);
	#print ("Openning: $local_dir\n");
	my @FILES= readdir(DIR); 
	closedir(DIR);
	foreach my $file (@FILES) 
	{
		if ($file eq "." || $file eq "..")
		{
			#print ("./ or ../ skipped\n");
		}
		elsif (-d "$local_dir/$file")
		{
			#print ("moving to: $local_dir/$file\n");
			move_files("$local_dir/$file");
		}
		elsif (-f "$local_dir/$file")
		{
			($name,$path,$suffix) = fileparse($file,qr"\.[^.]*$");
			if ($suffix eq ".bedGraph")
			{
				system ("mv $local_dir/$file $output");
			}
		}
		else
		{
			die("Unrecognized file generated: $file\n");
		}
		
	}
}