#!/usr/bin/perl -w
use strict;
my$tline="";
my@tlist=();
my$tempi=0;
my$tempj=0;
my$tempk=0;
my$pline="";
my@plist=();
my$tid=0;
my%tidx=();
@ARGV==3 or die""."Error in syntax!\n"."   ./postProcess.pl <input.MITE.dat> <output.MITE.dat> <DirTemp>\n";
my$egFileDat=$ARGV[0];
my$egFileOut=$ARGV[1];
my$egDirTemp=$ARGV[2];
my$egDirOut=$egDirTemp;
my$egFileTemp="$egDirTemp/postProcess.fasta";
my$egFileTOut="$egDirTemp/postProcess.blastn.dat";
my$egFileDB="IS_Finder.SEQ.fasta";
my%egGID2ID=();
my%egID2ID=();
my@egLine=();
my@egID=();
my@egTerMatch=();
my@egEValue=();
my@egStatus=();
my$egNum=0;
my$egHeadLine="";
print"Saving sequences ... ";
open(efIn,"$egFileDat")or die"Error when reading!\n";
open(efOut,">$egFileTemp")or die"Error when writing!\n";
while(<efIn>){$tline=$_;$tline=~s/[\r\n]//g;
@tlist=split(/\t/,$tline);
if($tline=~/^#/){$egHeadLine=$tline;}elsif(@tlist>=23){print efOut">$tlist[0]|$tlist[1]\n$tlist[21]\n";
$egID2ID{"$tlist[0]|$tlist[1]"}=$egNum;
$egGID2ID{"$tlist[0]"}=$egNum;
$egLine[$egNum]=$tline;
$egID[$egNum]=$tlist[1];
$egTerMatch[$egNum]="";
$egEValue[$egNum]=100;
$egStatus[$egNum]=0;
$egNum++;}}close(efOut);
close(efIn);
print" [$egFileDat] [done:$egNum]\n";
my$egCmdLine="";
$egCmdLine="./MUST/filterSSR.pl 0.80 \"$egFileTemp\" \"$egFileTemp.no_ssr_valid_id.txt\"";
print"Filtering the SSR MITEs ... ";
if(system("$egCmdLine 2>/dev/null 1>/dev/null")){die"-Error when filtering!\n";}print" [RUN]";
open(efI,"$egFileTemp.no_ssr_valid_id.txt")or die"Error when reading!\n";
while(<efI>){$tline=$_;$tline=~s/[\r\n]//g;
$tid=$egID2ID{"$tline"};
if(defined$tid){$egStatus[$tid]=1;}}close(efI);
print" [done]\n";
my$egTermini=50;
print"Mapping ... ";
$egCmdLine="./MUST/blast_profile.pl -e 100 -dn \"$egFileDB\" -qn \"$egFileTemp\" -o \"$egFileTOut\" -dirtemp \"$egDirTemp\" -diroutput \"$egDirOut\" -blast blastn -displaynumber 500";
if(system("$egCmdLine")){die"-Error when mapping!\n";}print" [done]\n";
print"Scanning ... ";
open(efIn,"$egFileTOut")or die"Error when loading data!\n";
while(<efIn>){$tline=$_;$tline=~s/[\r\n]//g;
@tlist=split(/\t/,$tline);
if($tline=~/^#/){}elsif(@tlist>=20){$tid=$egID2ID{"$tlist[0]"};
if(defined$tid){if((&efIsTermini($tlist[1],$tlist[10],$tlist[11])==1)and(&efIsTermini($tlist[8],$tlist[14],$tlist[15])==1)){if($tlist[3]<$egEValue[$tid]){$tlist[7]=~s/;IS_SEQ$//g;
$egTerMatch[$tid]="$tlist[7]";
$egEValue[$tid]=$tlist[3];}}}}}close(efIn);
print" [done]\n";
print"Saving ... ";
open(efOut,">$egFileOut")or die"Error when saving data!\n";
print efOut"$egHeadLine\tTerMatch\tEValue\n";
for($tempi=0;$tempi<$egNum;$tempi++){if($egTerMatch[$tempi]eq""){$egEValue[$tempi]="N/A";}if($egStatus[$tempi]==1){print efOut"$egLine[$tempi]\t$egTerMatch[$tempi]\t$egEValue[$tempi]\n";}}close(efOut);
print" [done:$egNum:$tempi]\n";
sub efIsTermini{my($tLength,$tS,$tE)=@_;
if(($tS<=$egTermini)or($tE>=$tLength-$egTermini)){return 1;}else{return 0;}}sub efIsOverlap{my($s1,$e1,$s2,$e2)=@_;
my$t=0;
if($s1>$e1){$t=$s1;$s1=$e1;$e1=$t;}if($s2>$e2){$t=$s2;$s2=$e2;$e2=$t;}if((($s2>=$s1)and($s2<=$e1))or(($s1>=$s2)and($s1<=$e2))){return 1;}else{return 0;}}
