#!/usr/bin/perl -w

# emboss.pl
# Run emboss program on all seqs in a folder
# Can be easily modified to run any command on every sequence in a folder
# WI Bioinformatics course - Feb 2002 - Lecture 5
# WI Bioinformatics course - Modified Sep 2003

################  User-supplied variables  #############

$Home = "/home/yuan";   #Replace yuan with your login name
                        #flexible, if you need to work in another directory,
                        #you can only change this directory

# Directory of sequences

print "The directory of original sequences: ";
$InDir = <STDIN>; 
chomp($InDir);                #delete "\n";
$myDir = "$Home/$InDir";      #$myDir="/home/yuan/liverseq" if $InDir is liverseq

# Output directory 

print "Output Directory is: ";
$OutDir = <STDIN>;
chomp $OutDir;                # delete "\n";
$outputDir = "$Home/$OutDir"; # $outputDir="/home/yuan/Palindrome" if $OutDir is Palindrome

#make the output directory if it does not exist, this part is optional(not required by the homework)

if (! -e $outputDir) {        # if $outputDir doesn't exist
    mkdir $outputDir, 0755;         # make a new directory called $outputDir
}

#Output sequence file ending

print "Output File ends with: "; 
$Tail = <STDIN>; 
chomp ($Tail);                # delete "\n";

################   Main Program  #################

# Go to sequence directory and open it (i.e, read contents)
chdir($myDir) || die "Cannot change to $myDir: $!";		# Go to $myDir
opendir(DIR, $myDir) || die "Cannot open $myDir: $!";		# Open $myDir

foreach $seqFile (sort readdir(DIR))
{	
    if ($seqFile =~ /\.tfa$/)		                        # if file ends in .tfa
    {
	print "Processing $seqFile\n";
	$outFile = $seqFile;			                # Create $outFile name
	
	$outFile =~ s/\.tfa/$Tail/;                             # replace .tfa with .palindrome if $Tail is .palindrome
	
###############  Run Emboss  ####################

#uncomment the system call to run the program

#	`revseq  $seqFile  $outputDir/$outFile`;               #revseq program 
	`palindrome $seqFile -minpallen 10 -maxpallen 100 -gaplimit 100 -nummismatches 2  -overlap  $outputDir/$outFile`;
                                                               #palindrome program
#	`transeq -frame F $seqFile  $outputDir/$outFile`;      #transeq program

    }
} 
closedir(DIR);