#!/usr/bin/perl
use strict;
use warnings;


my $pdbdir="/local/CASP13/TARGETS/"; 
my $resultdir="/local/CASP13/RAND_MODELS/";

opendir(PDBDIR,$pdbdir);
my @files=readdir(PDBDIR);
closedir(PDBDIR);

if (defined($ARGV[0])){
   my $target = $ARGV[0];
   if (-e "$pdbdir/$target.pdb"){
	@files = ();
	push @files, "$target.pdb";
   } else {
	print "File $pdbdir/$target.pdb doesn't exist\n";
	exit;
   }
  
}
#print scalar(@files)."\n";
#exit;


for(my $i=0;$i<scalar(@files);$i++){
	if($files[$i]=~/(^T0.*?)\.pdb/){
		my $name=$1;
		my $outdir=$resultdir."/".$name."/";
		system("mkdir -p $outdir");
		my $nowfile=$pdbdir.$files[$i];
		unless(open(INPUT,"$nowfile")){
			die("Can not open file $nowfile\n");
  		exit;
		}
		my %indexhash;
		my @indexarray;
		while(my $line=<INPUT>){
			chomp($line);
			if($line=~/^ATOM/ or $line=~/^HETATM/){
			 	my $indexinfo=substr($line,22,5);
				if($indexinfo=~/(-?\w+)/){
	  			$indexinfo=$1;
	  		}
	  		if(defined $indexhash{$indexinfo}){
	  			my $prearrayref=$indexhash{$indexinfo};
	  			my @prearray=@$prearrayref;
	  			push(@prearray,$line);
	  			$indexhash{$indexinfo}=\@prearray;
	  		}else{
	  			push(@indexarray,$indexinfo);
	  			my @tmparray=();
	  			push(@tmparray,$line);
	  			$indexhash{$indexinfo}=\@tmparray;
	  		}		
	  	}	
	  }
	  my $outindex=0;
	  for(my $ti=4;$ti<@indexarray;$ti=$ti+5){
	  	$outindex++;
	  	my $nowoutfile=$outdir.$name.".".$outindex;
	  	if(-e $nowoutfile){
	  		print "$nowoutfile exist\n";
	  		next;
	  	}	
	  	printfile($nowoutfile,$ti,\%indexhash,\@indexarray,'t');
	  }	
	  my @reversearray= reverse(@indexarray);
	  for(my $ri=0;$ri<@reversearray;$ri=$ri+5){
			$outindex++;
			my $nowoutfile=$outdir.$name.".".$outindex;
			if(-e $nowoutfile){
	  		print "$nowoutfile exist\n";
	  		next;
	  	}	
			printfile($nowoutfile,$ri,\%indexhash,\@indexarray,'s');
	  }	
	}		
}	
sub printfile{
	my $nowoutfile=shift(@_);
	my $startpos=shift(@_);
	my $nowindexhashref=shift(@_);
	my %nowindexhash=%$nowindexhashref;
	my $nowindexarrayref=shift(@_);
	my @nowindexarray=@$nowindexarrayref;
	my $type=shift(@_);
	my @checkarray=@nowindexarray;
	if($type eq 't'){
		
	}else{
		@checkarray=reverse(@checkarray);
	}		
	my $size=@checkarray;
	my @doublearray=@checkarray;
	unless(open(RESULT,">>$nowoutfile")){
		die("Can not open file $nowoutfile\n");
  	exit;
	}
	for(my $i=0;$i<@checkarray;$i++){
		push(@doublearray,$checkarray[$i]);
	}	
	my $endpos=$size+$startpos;
	my $count=0;
	my $allcount=0;
	for(my $j=$startpos;$j<$endpos;$j++){
		my $outindex=$nowindexarray[$count];
		$count++;
		my $thisinforef=$nowindexhash{$doublearray[$j]};
		my @thisinfoarray=@$thisinforef;
		for(my $t=0;$t<@thisinfoarray;$t++){
				my $outnumbercount=sprintf("%7s",$allcount);
				my $aainfo=substr($thisinfoarray[$t],11,9);
				my $outpos=sprintf("%4s",$outindex);
				my $xpos=substr($thisinfoarray[$t],30,8);
				my $ypos=substr($thisinfoarray[$t],38,8);
				my $zpos=substr($thisinfoarray[$t],46,8);
				print RESULT "ATOM"."$outnumbercount".$aainfo."  ".$outpos."    ".$xpos.$ypos.$zpos."\n";
		}	
	}
	close(RESULT);	
}
