#!c:/perl/bin/perl


use XML::DOM;
use Getopt::Std;

getopt("irdo");
#-i:input file
#-r:reviser,defaults is "antisenseml";
#-d:revise date: format is yyyy-mm-dd,defaut is current system date.
#-o:output file

$inall='';
open(in,$opt_i) or die $!;
print "reading...\n";
while (<in>){
	next if $_=~/^\s*$/;
	$inall.=$_;
	}
	close in;
	
	"parsing...\n";
	@in=split ('>',$inall);
	print $#in,"\n";
	
	$i=1;
	@target=();
	@antiseq=();
	
	@antiassays=();
	@efficacys=();
	@controls=();
	@figures=();
	@reference=();
	@comments=();
	

	my @tagin=();
	
	foreach (@in){
         
          if ($_=~/^<target/m){ @target=split ("\n",$_);}
           elsif ($_=~/^<reference/m){ @reference=split("\n",$_);}
           	elsif ($_=~/^<antiseq/m){ @antiseq=split("\n",$_);}
           	   elsif($_=~s/^<antiassay:(.*)$//m){
           	   	#@antiassays[$assayid]=$1;
           	   
           	   push @antiassays, [split(':',$1)];

             	       @allassays=split('}',$_);
           	       foreach(@allassays){
           	       	if ($_=~/^\{efficacy/m) { 
           	       		push @efficacys,[split("\n",$_)];
           	       		}
           	           elsif($_=~/^\{control/m) {  
           	           	push @controls,[split("\n",$_)];
           	           	}
           	       	     elsif($_=~/^\{figure/m) { 
           	       	     	push @figures,[split("\n",$_)];
           	       	     	}
           	       	         elsif($_=~/^\{comments/m) { 
           	       	         	push @comments,[split("\n",$_)];
           	       	         	}
           	       	
           	       	}

           	       
           	   	}
     
	
		}
		


        	open (temp,"+>template.xml") or die $!;
        	print temp "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
                print temp "<AntisenseML xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"antisenseML.xsd\" version=\"1.00\">\n";
                print temp "</AntisenseML>\n";        	
        	close temp;	
        			
	        my $parser=new XML::DOM::Parser;
	

        	my $doc = $parser->parsefile ("template.xml");
        	
        my $antisenseml=$doc->getDocumentElement;
         
####hash to represent list is good.
foreach(@target){
	next if $_=~/^<target.*/;
	next if $_=~/^\s*$/;
	@tarref=split("\t",$_);
	print "\@tarref:@tarref\n";
$targetdf=$doc->createDocumentFragment;
$target=$doc->createElement("Target");
$targetdf->appendChild($target);
if ($tarref[1]!=""){
     $target->setAttribute("TargetType",$tarref[0]);
     $target->setAttribute("GenBankID",$tarref[1]);
     }
     else {
          $target->setAttribute("TargetType","miRNA");
          $target->setAttribute("miRBaseAccessionNumber",$tarref[2]);	
     	}
     	
 $description=$doc->createElement("Description");
 $target->appendChild($description);
 $description->addText($tarref[3]);

 $sequence=$doc->createElement("Sequence");
 $target->appendChild($sequence);
 $sequence->addText($tarref[4]);
 $sequence->setAttribute("StartIndex","1");
 $sequence->setAttribute("Length",length($tarref[4]) );
 

  
 $information=$doc->createElement("Information");
  $target->appendChild($information);
 $information->addText($tarref[5]);


}
$antisenseml->appendChild($targetdf);
##############antiagent#################################
##############=========#################################

my $antiagentdf=$doc->createDocumentFragment;
my $antiagent=$doc->createElement("AntiAgent");
$antiagentdf->appendChild($antiagent);

###loop antiseq#####################
foreach (@antiseq){
	next if $_=~/^\s*$/;
	next if $_=~/^<antiseq.*/;
	@seqtmp=split("\t",$_);
$antiseq=$doc->createElement("AntiSeq");

$antiagent->appendChild($antiseq);

$antiseq->setAttribute("AntiType",$seqtmp[1]);
$antiseq->setAttribute("AntiID",$seqtmp[0]);
$antiseq->setAttribute("SeqType",$seqtmp[2]);
$antiseq->setAttribute("RefIDs",$seqtmp[9]);

$seq=$doc->createElement("Seq");
$seq->addText($seqtmp[3]);
$antiseq->appendChild($seq);
$seq->setAttribute("length", length($seqtmp[3]));
$seq->setAttribute("strand",$seqtmp[4]);
$seq->setAttribute("Position",$seqtmp[6]);
$seq->setAttribute("Source",$seqtmp[5]);
$seq->setAttribute("shRNAseq",$seqtmp[7]);

##loop###
@mod=split(';',$seqtmp[8]);
@modpos=();
@modf=();
foreach (@mod){
	$_=~/^\(([^()]*)\)(.*)/;
	push @modpos,$1;
	push @modf,$2;
	
	
	}

foreach (0..$#modpos){

$modification=$doc->createElement("Modification");
$modification->addText($modf[$_]);
$modification->setAttribute("ModPosition",$modpos[$_]);
$antiseq->appendChild($modification);

}
}
##antiassay loop##############################

foreach (0..$#antiassays){
	@assaytmp=@{$antiassays[$_]};
		
	
$antiassay=$doc->createElement("AntiAssay");

$antiagent->appendChild($antiassay);

$antiassay->setAttribute("AssayType",$assaytmp[0]);
$antiassay->setAttribute("SeriesID",$assaytmp[1]);
$antiassay->setAttribute("RefIDs",$assaytmp[2]);
$antiassay->setAttribute("Transfection_Method",$assaytmp[3]);



#################efficacy######################
@efficacy=@{$efficacys[$_]};
	foreach(@efficacy){
	next if $_=~/^\s*$/;
	next if $_=~/^\{.*/;	
	@efftmp=split("\t",$_);
	print "@efftmp\n";	
$efficacy=$doc->createElement("Efficacy");

$antiassay->appendChild($efficacy);

$efficacy->addText($efftmp[7]);
$efficacy->setAttribute("AntiID",$efftmp[0]);
$efficacy->setAttribute("RefID",$efftmp[1]);
$efficacy->setAttribute("CtrlIDs",$efftmp[2]);
$efficacy->setAttribute("CellLine",$efftmp[3]);
$efficacy->setAttribute("AssayLevel",$efftmp[4]);
$efficacy->setAttribute("AssayMethod",$efftmp[5]);
$efficacy->setAttribute("Conc",$efftmp[6]);
$antiassay->appendChild($efficacy);

}
############################
@ctrl=@{$controls[$_]};
	foreach(@ctrl){
		next if $_=~/^\s*$/;
		next if $_=~/^\{.*/;
		@ctrltmp=split("\t",$_);
		
	print "@ctrltmp\n";	
$control=$doc->createElement("Control");

$antiassay->appendChild($control);

$control->setAttribute("CtrlID",$ctrltmp[0]);
$control->setAttribute("CtrlType",$ctrltmp[1]);
$control->setAttribute("CtrlSeq",$ctrltmp[2]);
$control->setAttribute("AssayLevel",$ctrltmp[3]);
$control->setAttribute("AssayMethod",$ctrltmp[4]);
$control->setAttribute("CtrlConc",$ctrltmp[5]);
$control->setAttribute("CtrlEff",$ctrltmp[6]);


}
############################################

@fig=@{$figures[$_]};
	foreach(@fig){
		next if $_=~/^\s*$/;
		
		@figtmp=split("\t",$fig[$_]);
		print "@figtmp\n";
$figure=$doc->createElement("Figure");

$antiassay->appendChild($figure);

$figure->setAttribute("FigID",$figtmp[0]);
$figure->setAttribute("FigName",$figtmp[1]);
$figure->setAttribute("FigURL",$figtmp[2]);
$figure->setAttribute("FigPath",$figtmp[3]);
$figure->setAttribute("AntiIDs",$figtmp[4]);
$figure->setAttribute("CtrlIDs",$figtmp[5]);

$title=$doc->createElement("Title");
$figure->appendChild($title);
$title->addText($figtmp[6]);
$annotation=$doc->createElement("Annotation");
$figure->appendChild($annotation);
$annotation->addText($figtmp[7]);
}
##############################################
@comtmp=@{$comments[$_]};
$com='';
	foreach(@comtmp){
		next if $_=~/^\s*$/;
		next if $_=~/^\{.*/;
	$com=join ('',$_);
	print "$com\n";}
$comments=$doc->createElement("Comments");
$comments->addText($com);
$antiassay->appendChild($comments);


}


$antisenseml->appendChild($antiagentdf);
###############Reference###################################
###############=========###################################

#########loop referenece##############################
foreach(@reference){
	next if $_=~/^\s*$/;
	next if $_=~/^<reference.*/;
	@reftmp=split("\t",$_);
	print "@reftmp\n";
$reference=$doc->createElement("Reference");

$antisenseml->appendChild($reference);
$reference->setAttribute("RefID",$reftmp[0]);
if ($reftmp[1]!=""){
$reference->setAttribute("PMID",$reftmp[1]);
}

$author=$doc->createElement("Author");
$author->addText($reftmp[2]);
$reference->appendChild($author);


$title=$doc->createElement("Title");
$title->addText($reftmp[3]);
$reference->appendChild($title);



if ($reftmp[5]!=""){
$book=$doc->createElement("Book-title");
$book->addText($reftmp[5]);
$reference->appendChild($book);
}
else {
$journal=$doc->createElement("Journal");
$journal->addText($reftmp[4]);
$reference->appendChild($journal);
	}
	
if ($reftmp[6]!=""){
$date=$doc->createElement("Date");
$date->addText($reftmp[6]);
$reference->appendChild($date);
}
if ($reftmp[7]!=""){
$volumn=$doc->createElement("Volumn");
$volumn->addText($reftmp[7]);
$reference->appendChild($volumn);
}
if ($reftmp[8]!=""){
$issue=$doc->createElement("Issue");
$issue->addText($reftmp[8]);
$reference->appendChild($issue);
}
if ($reftmp[9]!=""){
$pages=$doc->createElement("Pages");
$pages->addText($reftmp[9]);
$reference->appendChild($pages);
}
if ($reftmp[10]!=""){
$path=$doc->createElement("Path");
$path->addText($reftmp[10]);
$reference->appendChild($path);
}

}
#####################Revision
#####################========

my $date='';
if ($opt_r ==''){
	$reviser="antisenseml";
}
   else {
   	$reviser=$opt_r;
   	}
   	if($opt_d==''){
   		@ti=localtime();
   		$month=@ti[4]+1;
   		$day=@ti[3];
   		
   		if ($month<10){$month='0'.$month;}
   		if ($day<10){$day='0'.$day;}
   		$date=(@ti[5]+1900).'-'.$month.'-'.$day;
   		}
   		else {
   			$date=$opt_d;
   			}
$revisiondf=$doc->createDocumentFragment;
$revision=$doc->createElement("Revision");
$revisiondf->appendChild($revision);

$rdate=$doc->createElement("rDate");
$rdate->addText($date);
$rdate->setAttribute("Reviser",$reviser);
$revision->appendChild($rdate);		

$antisenseml->appendChild($revisiondf);

$doc->printToFile($opt_o);


$doc->dispose;
    
