#!perl
# quadratic.pl

# code to get input file name
print "Enter filename of .xml file, e.g. 'input.xml' \n";
print "Please make sure .xml is lowercase \n";

chomp($line = <STDIN>);

eval {
    $infilename = $line;
    open INFILE, $infilename
	or die "Can't open file '$infilename' \n";
};

if ($@){
    print "Can't open file '$infilename', program halting. \n "; 
    die "File not found ";
} else {
	print "Okay, file named $infilename exists, continuing \n"; 
	print "\n";
}


print "Enter filename of the peaks.csv file, e.g. 'peaks-quadratic.csv' \n";

chomp($line = <STDIN>);

eval {
    $peakname = $line;
    open FILE, $peakname
	or die "Can't open file '$peakname' \n";
};

if ($@){
    print "Can't open file '$peakname', program halting. \n "; 
    die "File not found ";
} else {
	print "Okay, file named $peakname exists, continuing \n"; 
}

# give the form of the tags that correspond to
# a, b, t0, U (the voltage, also called V in some places within
# Ciphergen documentation), and a boolean indicator of whether the
# spot correction feature is enabled.  Spot correction can make
# a very tiny difference between masses (< .001%)fitted by the equation and
# those actually written out in a .csv file.  As Ciphergen's use
# of this parameter is unclear and I'm unable to duplicate it, 
# this program turns off the spot correction feature.
# The user may need to verify their .xml file uses
# these same tags -- if not these labels will need to be changed
$atag = "massCalibrationA";
$btag = "massCalibrationB";
$t0tag = "massCalibrationT0";
$Utag  = "massCalibrationU";
$spottag = "spotCorrectionEnabled";

# name of file holding original parameters that this program
# writes out for use in R
$Rinput = "Rinput.csv";

# After the R program processes Rinput.csv it writes out
# Routput.csv which is then used in this Perl program
$Routput = "Routput.csv";

# name of R program used to compute new parameters
$Rprogram = "quadratic-r-program.r";

# log file to see if any errors in R program
$Rprogramlog = "quadratic-r-program.log";


open INFILE, $infilename;

# get the values of the tags
while (<INFILE>) {
    if (/$atag.*$atag/) {
	s/<\/$atag>//;
	s/<$atag>//;
	$aval = 1*$_;
    }
    if (/$btag.*$btag/) {
	s/<\/$btag>//;
	s/<$btag>//;
	$bval = 1*$_;
    }
    if (/$t0tag.*$t0tag/) {
	s/<\/$t0tag>//;
	s/<$t0tag>//;
	$t0val = 1*$_;
    }
    if (/$Utag.*$Utag/) {
	s/<\/$Utag>//;
	s/<$Utag>//;
	$Uval = 1*$_;
    }
}
#print "$aval \n";
#print "$bval \n";
#print "$t0val \n";
#print "$Uval \n";

close INFILE;

# create name of new output .xml file to hold new parameters
$outfilename = $infilename ;
$outfilename =~ s/\.xml/-new\.xml/;

# write the Rinput.csv file with original parameters
open STDOUT, ">$Rinput";
  print "Avalue, Bvalue, t0value, Uvalue, peak.filename \n";
  print "$aval , $bval, $t0val, $Uval, \"$peakname\" \n";
close STDOUT;

# Call the R program to process Rinput.csv
# The R program produces Routput.csv
system "Rcmd BATCH  $Rprogram $Rprogramlog";

# read in the Routput.csv file with the new parameters
# and reassign aval, bval, t0val, and Uval to the new
# parameters (though Uval won't change)
open INFILE, $Routput;
while(<INFILE>){
    @fields = split /,/ ;
    $Uval = 1*pop(@fields);
    $t0val = 1*pop(@fields);
    $bval = 1*pop(@fields);
    $aval = 1*pop(@fields);
}
close INFILE;


open INFILE, $infilename;
open STDOUT, "> $outfilename";

while(<INFILE>){
    s/<$atag>.*<\/$atag>/<$atag>$aval<\/$atag>/;
    s/<$btag>.*<\/$btag>/<$btag>$bval<\/$btag>/;
    s/<$t0tag>.*<\/$t0tag>/<$t0tag>$t0val<\/$t0tag>/;
    s/<$spottag>.*<\/$spottag>/<$spottag>false<\/$spottag>/;
    print $_;
}
close INFILE;
close STDOUT;


