Tuesday, November 29, 2011

perl Programming: a script to run background Java programs

One task I often find myself doing is running a java program on each member of a  dataset. If the java program is configured to run in the background, you will initiated enough java calls to crash your computer. I hacked together a solution using perl that waits for each call to java to finish before initiating the next.


 
#!/usr/bin/perl
use strict;
my (@fileA,@out);
#put java command into string
my $command =  "./java.sh";
#simulate multipul calls with foreach loop
foreach my $wait (1..20){#represents the iterations of inputs run via a java program
  #open a handle for the command, piping into $java
  open my $java, '-|',  $command or die "Could not run java ... - $!";
  #while handle is still be written to, print .
  print "running command ${wait}";
  while (<$java>) {
       print ".";
  #push the output of the command into file array
  @fileA = <$java>;
   }
   push(@out, @fileA);
  #close the pipe when the command is done
  close($java);
}

#print to some output file
my $file = "out.txt";
#open it, print to it the @out array, close it, print \n 
open my $f, '>', $file;
print $f @out;
close $f;
print "\n";

#java.sh
#############################################
#!/bin/bash
#java -classpath /home/user/pathto/jarFile.jar ducks.main &
############################################


 

Day 0

Hello and welcome to my blog. I intend to make this blog a collection of my ideas pertaining to all things bioinformatics. I am in the middle of applying to graduate school for bioinformatics, and will keep you updated on the status of my applications,good news only :).