view kmernator_wrapper.pl @ 0:d9da256384e1 default tip

Uploaded
author edward-kirton
date Thu, 14 Jul 2011 22:19:53 -0400
parents
children
line wrap: on
line source

#!/usr/bin/env perl

# FilterReads (AKA Kmernator) wrapper for Galaxy.
# Kmernator by Rob Egan (RSEgan@LBL.gov)
# Wrapper for Galaxy supported by Edward Kirton (ESKirton@LBL.gov)
# Produced under funding from the United States Department of Energy Office of Science
# Freely distributable under the same license as Galaxy itself.

use strict;
use warnings;
use File::Copy;

# SHIFT OUTFILE PATHS OFF ARGV
die unless @ARGV;
my $id=shift @ARGV;
my $results_dir=shift @ARGV;
my $working_dir=shift @ARGV;
my $mmap_infile=shift @ARGV;
my $kmer=shift @ARGV;
my $mmap_outfile=shift @ARGV;
my $gc_map_outfile=shift @ARGV;
my $partition_by_depth=shift @ARGV;
my $format=shift @ARGV;
my @infiles=();
my @history_ids=();
while (my $infile=shift @ARGV) {
    last if $infile eq 'END';
    push @infiles, $infile;
    my $hid=shift @ARGV;
    push @history_ids, $hid;
    die("Input file $hid does not exist ($infile)\n") unless -e $infile;
}

# PREPARE
mkdir($working_dir) unless -d $working_dir;
chdir($working_dir) or die($!);
for (my $i=0; $i<=$#infiles; $i++) {
    my $infile=$infiles[$i];
    symlink($infile,"$working_dir/$i.fastq");
    unshift @ARGV, "--input-file $working_dir/$i.fastq";
}
if ($mmap_infile ne 'None') {
    symlink("$mmap_infile", "$working_dir/out-mmap");
    unshift @ARGV, "--load-kmer-mmap 1";
}

# RUN
my $output=`FilterReads @ARGV --output-file $working_dir/out`;
die("Kmernator died while running command: @ARGV\nOUTPUT:\n$output\n") if $?;

# MOVE OUTFILES
if ($kmer < 1) {
    my $dest=join("_",'primary',$id,'FilteredReads','visible',$format); 
    move("out-in.fastq","$results_dir/$dest") or die($!);
} elsif ($partition_by_depth > 0) {
    opendir(DIR, $working_dir) or die($!);
    my @files = readdir(DIR);
    closedir(DIR);
    foreach my $file ( @files ) {
        next unless $file =~ /^out\-(MinDepth\d+\-PartitionDepth\d+)\.fastq$/ or $file =~ /^out\-(MinDepth\d+\-PartitionDepth\d+\-consensus\-\d+)\.fastq$/;
        #next unless $file =~ /^out\-(MinDepth\d+\-PartitionDepth\d+)\-dataset_\d+\.fastq$/ or $file =~ /^out\-(MinDepth\d+\-PartitionDepth\d+\-consensus\-\d+)\.fastq$/;
        my $dest=join("_",'primary',$id,$1,'visible',$format); 
        move($file,"$results_dir/$dest") or die($!);
    }
    move("out-GC.txt",$gc_map_outfile) or die($!);
    move("out-mmap",$mmap_outfile) or die($!);
} else {
    opendir(DIR, $working_dir) or die($!);
    my @files = readdir(DIR);
    closedir(DIR);
    foreach my $file ( @files ) {
        next unless $file =~ /^out\-(MinDepth\d+)\.fastq$/ or $file =~ /^out\-(MinDepth\d+\-consensus\-\d+)\.fastq$/;
        #next unless $file =~ /^out\-(MinDepth\d+)\-dataset_\d+\.fastq$/ or $file =~ /^out\-(MinDepth\d+\-consensus\-\d+)\.fastq$/;
        my $dest=join("_",'primary',$id,$1,'visible',$format); 
        move($file,"$results_dir/$dest") or die($!);
    }
    move("out-GC.txt",$gc_map_outfile) or die($!);
    move("out-mmap",$mmap_outfile) or die($!);
}
if ( -f "out-0-Artifact.fastq" ) {
    for (my $i=0; $i<=$#infiles; $i++) {
        my $hid=$history_ids[$i];
        my $dest=join("_",'primary',$id,"Dataset-$hid-Artifact",'visible',$format); 
        move("out-$i-Artifact.fastq", "$results_dir/$dest") or die($!);
    }
}
exit;