#!/usr/bin/perl 


if($#ARGV != 0 ) { print "Script to convert ss to xrna.\nrun as :\n./ss2xrna.pl file.ss\noutput will be file.xrna\nfile name should have no space or non-alphanumeric characters\nwhite space bug fix 2014"; exit;}

$filein  = @ARGV[0];

if($filein =~ /[^a-z\d\.]/i){print "filename contains non-alphanumneric. Please simplify it.\n";exit;}

$fileout=$filein;
if($filein =~ /.ss$/){$fileout =~ s/ss/xrna/ ;}
$mol=$fileout;
$mol =~ s/\.xrna//;

open(IF,$filein);
$previd=0;
$helixlength=0;
$nuclist="";
$nn=0;
$avex=0;$avey=0;
$minx=0;$maxx=0;
$miny=0;$maxy=0;
$nxy=0;
while($a=<IF>){
chomp($a);
@b=split(/\s+/,$a);
#1 G 348.00 -183.00 -1 72
if(scalar(@b)==6 && $a =~ /^[0-9]/){

$id=$b[0];
$char=$b[1];
$char =~ s/a/A/;
$char =~ s/u/U/;
$char =~ s/c/C/;
$char =~ s/g/G/;
$x=$b[2];
$y=$b[3];
if($y<$miny){$miny=$y;}
if($y>$maxy){$maxy=$y;}
if($x<$minx){$minx=$x;}
if($x>$maxx){$maxx=$x;}
$avex+=$x;
$avey+=$y;
$nxy++;
$q=$b[4];
$pair=$b[5];
if($previd==0){$startid=$id; }
$nuclist .= "$char $x $y\n";
$nucarray[$nn] = "$char $x $y";
$nn++;

if($pair > 0 && $pair==$prevpair-1){
#helix continues
$key="$id\t$pair";
$hash{$key}++;
$rkey="$pair\t$id";
$hash{$rkey}++;
if($hash{$key}==1 && $hash{$rkey}==1){$helixlength++;}
}
elsif($pair != $prevpair-1 && $pair != 0 && $helixlength>0 ){
#print prevhelix, start new one

$helixout .= "<BasePairs nucID='$hstart' length='$helixlength' bpNucID='$hpstart' />\n";
#newhelix

$key="$id\t$pair";
$hash{$key}++;
$rkey="$pair\t$id";
$hash{$rkey}++;
if($hash{$key}==1 && $hash{$rkey}==1){$helixlength=1;$hstart=$id ; $hpstart=$pair;}
else{$helixlength=0;}

}
elsif($prevpair==0 && $pair>0){
# start helix
$key="$id\t$pair";
$hash{$key}++;
$rkey="$pair\t$id";
$hash{$rkey}++;
if($hash{$key}==1 && $hash{$rkey}==1){$helixlength=1;$hstart=$id ; $hpstart=$pair;}
else{$helixlength=0;}
}
elsif($pair==0 && $helixlength>0){
# end helix
$helixout .= "<BasePairs nucID='$hstart' length='$helixlength' bpNucID='$hpstart' />\n";
$helixlength=0;
}


$prevpair=$pair;
$previd=$id;
}}

if($helixlength>0){$helixout .= "<BasePairs nucID='$hstart' length='$helixlength' bpNucID='$hpstart' />\n"; }

$avex=int($avex/$nxy);
$avey=int($avey/$nxy);


print "$maxx $minx $maxy $miny \n";

open(OF,">$fileout");
print OF "\n<ComplexDocument Name='$mol'>
<SceneNodeGeom CenterX='0' CenterY='0' />
<Complex Name='$mol'>
<RNAMolecule Name='$mol'>\n";


#scale from -500 to 500

print OF "<NucListData StartNucID='$startid' DataType='NucChar.XPos.YPos'>\n";
#print OF $nuclist;
for($n=0;$n<$nn;$n++){
@line = split(/ /,$nucarray[$n]);
$x = -400 + (800 * ($line[1]-$minx)/($maxx-$minx)) ;
$y = -400 + (800 * ($line[2]-$miny)/($maxy-$miny)) ;
print OF "$line[0] $x $y\n";
}

print OF "</NucListData>\n";
print OF "<Nuc RefIDs='$startid-$id' Color='0' FontID='0' FontSize='14' />\n";
print OF "<Nuc RefIDs='$startid-$id' IsSchematic='false' SchematicColor='0' SchematicLineWidth='1.5' SchematicBPLineWidth='1.0' />\n";
print OF $helixout ;



print OF "</RNAMolecule>
</Complex>
</ComplexDocument>\n";

