#!/usr/bin/perl

$fname          = $ARGV[0];
$ofname         = $ARGV[1];
$patfile        = $ARGV[2];


# check syntax from commandline
if( @ARGV  ne 3 )
  {
    print "Wrong number of arguments!\n";
    print "Syntax: ./out2orfplot.pl out.ffn toOrfPlot.Scer list_cand.txt\n";
    exit( 1 );
  }


open( MYFILE, $fname ) or die "Cannot open file: $fname : $!";
open( OUTFILE, ">$ofname" ) or die "Cannot write to file: $ofname:  $!";


# Load all strings to match that will be used for pattern-matching against genenames
open( CANDFILE, $patfile ) or die "Cannot open file: $patfile : $!";
%isInCandidates;
while ( <CANDFILE> )
{
  $_ =~ s/\n//;
  $isInCandidates{ $_ } = 1;
}



while ( <MYFILE> )
  {
    my $a = $_;
  LABEL:

    if( $a =~ /\>/ )
      {
	my @parts = split " ", $a;
	$parts[0] =~ s/\>//;
	$parts[1] =~ s/\[//;
	$parts[3] =~ s/\]//;
	@parts2 = split "_", $parts[0];
	if( $isInCandidates{ $parts2[0] } == 1 )
	  {
	    print OUTFILE "Scer_".$parts[0]." ".$parts2[0]." ".$parts[1]." ".$parts[3]." ".$parts[8]."\n";
	  }
      }
  }


close( OUTFILE );
