use strict;
use warnings;

my $type;

system("dos2unix reactionsin.txt"); #convert windows EOL to unix in case the script is running on a linux webserver
open(LISTIN, "reactionsin.txt");

#determine whether the list in the reactionsin file is of ecnumbers of kegg reaction IDs
my @importedreactions = <LISTIN>;
close LISTIN;
# if ($importedreactions[1] =~ m/R\d\d\d\d\d/)  {
	# $type = "reaction";
# }
# else  {
	# $type = "ecnumber";
# }

$type = "reaction";
foreach (@importedreactions)  {
	if ($_ =~ m/\.*\./)  { #reactions should never EVER have a full-stop let alone two seperated by something
		$type = "ecnumber";
	}
}

print "Imported ".scalar(@importedreactions)." ".$type."s \n";

open(FIRSTIN, "KGMLmin.svg");
my @KGML = <FIRSTIN>;
close FIRSTIN;

if ($type eq "reaction")  {
	foreach my $reaction(@importedreactions)  {
		chomp $reaction;
		for(my$i=0; $i<scalar(@KGML); $i++)  {
			my $line = $KGML[$i];
			if ($line =~ m/$reaction/)  {		
				while  ($line =~ m/			/)  {  #3 tabs for KGMLmin. Would need to be 4 for KGML normal. I know delimiting on this is bad, but I tried three times not to. This works.
					$line =~ s/stroke\-width="(.+?)"/stroke\-width="20"/;
					$KGML[$i] = $line;
					$i++;
					$line = $KGML[$i];
				}
			next;	
			}
		}
	#print "." ;
	}
}

#take ec numbers as input option
else   {
	foreach my $reaction(@importedreactions)  {
		chomp $reaction;
		for(my$i=0; $i<scalar(@KGML); $i++)  {
			my $line = $KGML[$i];
			if ($line =~ m/:$reaction"/)  {		
				while  ($line =~ m/			/)  { #3 tabs for KGMLmin. Would need to be 4 for KGML normal. I know delimiting on this is bad, but I tried three times not to. This works.
					$line =~ s/stroke\-width="(.+?)"/stroke\-width="20"/;
					$KGML[$i] = $line;
					$i++;
					$line = $KGML[$i];
				}
			next;	
			}
		}
	#print "." ;
	}
}

open(OUTPUT, ">output.svg");
foreach my $line(@KGML)  {
	print OUTPUT "$line";
}

close OUTPUT;