view blat_wrapper.pl @ 0:3cec538aab33 draft

Uploaded
author joachim-jacob
date Thu, 30 May 2013 07:13:55 -0400
parents
children da4426cac227
line wrap: on
line source

#!/usr/bin/perl
# blat_wrapper.pl
# Joachim Jacob - joachim.jacob@gmail.com - 2013

use strict;
use File::Temp 'tempdir';
use File::Copy qw(move);
use File::Basename;
use File::Spec qw(join);
use Log::Log4perl qw(:easy);

# ---------------------- Prepping Logging -----------------------------#
########################################################################
# Log levels: 			$DEBUG	$INFO	$WARN	$ERROR	$FATAL
# ConversionPattern:	%d %-5p %F{1} [%M] (line %L): %m%n%n
my $log_conf = q/ 
    log4perl.category = ERROR, Screen 
    log4perl.appender.Screen.stderr=1
    log4perl.appender.Screen.layout=Log::Log4perl::Layout::PatternLayout
    log4perl.appender.Screen.layout.ConversionPattern = %d %-5p %m%n
    log4perl.appender.Screen        = Log::Log4perl::Appender::Screen 
/;

Log::Log4perl::init( \$log_conf );
my $logger = get_logger();

# ----------------- Getting parameters file ---------------------------#
########################################################################
my ($configfile) = @ARGV;
my (%para);
open(CONFIG,"<$configfile");
while (<CONFIG>) {
	if (/(\S+)==(.+)$/){ $para{ $1 } = $2 ; }
}
close(CONFIG);

=Excerpt Config parameters
			## first we pass some galaxy environment variables
			galtemp==${__new_file_path__}
			
			## first we pass some galaxy environment variables
			galtemp==${__new_file_path__}
			
			#if $refGenomeSource.genomeSource == "indexed"
				referencepath==${refGenomeSource.index.fields.path}
				range=$refGenomeSource.range
			#else
				referencepath==${refGenomeSource.ownFile}
			#end if

			input==$input
			output==$output
			q==$q
			t==$t

			advanced_params.use==$advanced_params.use
			#if $advanced_params.use=="yes"
				tileSize==$advanced_params.tileSize
				stepSize==$advanced_params.stepSize
				oneOff==$advanced_params.oneOff
				minMatch==$advanced_params.minMatch
				minScore==$advanced_params.minScore
				maxGap==$advanced_params.maxGap
				mask==$advanced_params.mask
				qMask==$advanced_params.qMask
				repeats==$advanced_params.repeats
				trimT==$advanced_params.trimT
				noTrimA==$advanced_params.noTrimA
				fine==$advanced_params.fine
				maxIntron==$advanced_params.maxIntron
				extendThroughN==$advanced_params.extendThroughN
			#end if
=cut

for my $para (keys %para){
	INFO "$para\tset to\t$para{$para}";
}


# ---------------------- Prepping temp dir ----------------------------#
########################################################################
# within the temporary directory of Galaxy, we create a temporary dir

my $galtemp = $para{'galtemp'};
delete($para{'galtemp'});
DEBUG "\nReceived Galaxy temporary directory:\n$galtemp";


my $tempdir = File::Temp->tempdir('tmpXXXXX', DIR => $galtemp, CLEANUP => 1); 
mkdir "$tempdir", 077 unless -d "$tempdir";
INFO "\nTemporary directory:\n$tempdir";


# -------------------- Assembling command  ----------------------------#                                                             
########################################################################                                                             
my $command = "blat ";   # this will ultimately be executed
$command .= " $para{'referencepath'}";     
if ( $para{'range'} ) { 
	## format checking of the provided range
	if ($para{'range'} !~ //){
		ERROR "Range has be wrongly formatted: $para{'range'}";
	} else {
		$command .= ":$para{'range'}";
	}
} 
                              
$command .=  " $para{'input'}";
delete($para{'referencepath'});
delete($para{'range'});
delete($para{'input'});

$command .= " -out=blast9 -q=$para{'q'}  -t=$para{'t'}";
delete($para{'q'});
delete($para{'t'});

my $output = " $para{'output'}";
delete($para{'output'});

if ( $para{'advanced_params.use'} eq "yes" ){
	delete($para{'advanced_params.use'});
	$command .= " 	-minScore=$para{'minScore'} -maxGap=$para{'maxGap'} -mask=$para{'mask'} -qMask=$para{'qMask'} -oneOff=$para{'oneOff'}  -minMatch=$para{'minMatch'} -tileSize=$para{'tileSize'} 	-stepSize=$para{'stepSize'} -maxIntron=$para{'maxIntron'} ";
	if($para{'repeats'} eq 'yes' ){
		$command .= " -repeats=$para{'qMask'}";
	}
	if($para{'extendThroughN'} eq 'yes' ){
		$command .= " -extendThroughN";
	}
	if($para{'fine'} eq 'yes' ){
		$command .= " -fine";
	}
	if($para{'trimT'} eq 'yes' ){
		$command .= " -trimT";
	}
	if($para{'noTrimA'} eq 'yes' ){
		$command .= " -noTrimA";
	}
}

$command .= " $output";                                                                                                                                


# --------------------- Executing command  ----------------------------#                                                             
########################################################################                                                             
run_process($command, "BLAT alignment", $tempdir);

# --------------------------- Exiting  --------------------------------#                                                             
########################################################################                                                             
exit 0;


### 					      Subroutines 						     ###
########################################################################
sub run_process {
	my ($command, $name, $tempdir)= @_;
	my $logger = get_logger();
	INFO "\nProcess to launch:\n $command\n";
	system("cd $tempdir; $command 2>/dev/null") == 0 or die "$name failed\nExit status $?\nCommand: $command";
	if ($? == -1) {
		print "failed to execute: $!\n";
	} elsif ($? & 127) {
		printf "child died with signal %d, %s coredump\n", ($? & 127), ($? & 128) ? 'with' : 'without';
	} else {
		printf "$name executed successfully\n", $? >> 8;
	}	
}