function FileExists(FileName: String): Boolean;
{ Boolean function that returns True if the file exists;otherwise,
 it returns False. Closes the file if it exists. }
var
 F: file;
begin
 {$I-}
 Assign(F, FileName);
 FileMode := 0;  {Set file access to read only }
 Reset(F);
 Close(F);
 {$I+}
 FileExists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

procedure readWLC; {reads in force/extension data for 2 polymers}
var s:string;
begin
reset(forceFile); i:=1;
while not (eof(forceFile) or (i>200)) do
  begin
  readln(forceFile,dataLine);
  s:= copy (dataLine,1,30);
  val(s,dsDNA[i],j);         {extension of 1 basepair dsDNA}
  s:= copy (dataLine,31,30);
  val(s,ssDNA[i],j);         {extension of 2 bases of ssDNA}
  s:= copy (dataLine,61,30);
  val(s,molForce[i],j);      {force that aligns bases}
  s:= copy (dataLine,91,30);
  val(s,fjcStretch[i],j);    {entropic energy to align bases}
  inc(i);
  end;
end;

procedure findFile(var filename: pathstr; var fileopen:boolean;prompt:string);
{open an existing file for input}
begin
     writeln;
     writeln(prompt);
     readln(reply);
     message:='genome\'+reply+'.txt';
     filename:=fExpand(message);
     fSplit(fileName,dird,name,fileExt);
     s:=fSearch(filename,dird);
     if s<>'' then fileopen:=true
     else
       begin
       writeln('Unfortunately, file name '+message+' does not exist..q=quit');
       fileopen:=false;
       end;
end;

procedure createFile(var filename:pathStr;var fileOpen:boolean; prompt:string);
{create a new file for output}
begin
setColor(cyan);
outTextXY(100, yMid-60,prompt);
readln(reply);
message:= 'output\'+reply +'.txt';
filename:=fExpand(message);
fSplit(fileName,dird,name,fileExt);
s:=fSearch(filename,dird);
if s='' then  fileopen:=false
        else begin
        fileopen:=true; setColor(red);
        outTextXY(100,yMid-30,'File '+message+' already exists');
        outTextXY(100,yMid-15,'Do you want to write over it? y/n');
        readln(ch);
        if (ch='y') or (ch='Y') then fileOpen:=false;
        end;
plotAxes;
end;

procedure promptSequence;
var maxSize:longInt;
begin
clrscr;
repeat findFile(filename,fileopen,'Which genome do you want to unzip?')
until fileopen or (reply='q');
if reply='q' then halt;
seqName:=reply;
assign(seqdata,filename);
reset(seqdata);
readln(seqData,genomeSize);  {gets total number of bases in genome}
writeln;
writeln('Starting where in genome?');
readln(startIndex);
writeln;
maxSize:=genomeSize - startIndex;
if maxSize>7000 then maxSize := 7000;
writeln('How many basepair in hairpin? (max = ',maxSize,')');
readln(pinLength);
if pinLength > maxSize then pinLength := maxsize;
end;

procedure getViscosity;
begin
viscosity := (1.7383 -0.048948*T +0.00069668*T*T -0.0000038714*T*T*T) *1e-3;
          {viscosity of water in Newton*sec/meter^2}
dragCoeff:=6*Pi*viscosity*r; {Stokes law}
meanThermForce:=sqrt(2*kB*(T+273.15)*dragCoeff/dt); {Fick's law}
absT := T + 273.15;
end;

procedure promptTemperatures;
begin
outTextXY(100,100,'Lowest temperature in degrees C ?');
readln(Tmin); {degrees Centigrade}
outTextXY(100,120,'Highest temperature ?');
readln(Tmax); {degrees Centigrade}
outTextXY(100,140,'Temperature increment ?');
readln(Tstep); {degrees Centigrade}
T := Tmin;
loadBaseEnergies;  {using latest temperature}
getViscosity;
end;

procedure startOutputFile;
begin
writeln;
repeat createfile(filename,fileopen,'Name of new output file? (max 8 char, no .ext')
until not fileopen;
assign(FXfile,filename);
rewrite(FXfile);
FXfileOpen := true;
pullNum:=0;
write(FXfile,seqName,' =', genomeSize:6);
writeln(FXfile,' starting at ',startIndex,' and ending at ',startIndex+pinLength-1);
write(FXfile,'Temp.= ',T:1:1,' degC',' viscosity = ',viscosity*1000:1:4,' cp');
writeln(FXfile,' pullRate= ', pullRate:1:0,' nm/sec');
writeZipComments; {thermo parameters used in zipper.pas}
write(FXfile,'BeadRadius = ',r*1e6:2:2,'um');
writeln(FXfile,'  kTrap = ',kTrap*1e6:0:0,' pN/um');
writeln(FXfile, 'pullNum        Force(pN)     TrapPos(um)     forkPos');
outTextXY(10,20,filename);
end;

procedure incrementFile;
var indexStr:string;
begin
close(FXfile);
plotAxes; {erase old graph}
pullNum:=1;
inc(fileIndex);
if fileIndex>99 then halt; {prevent runaway disc-write we hope}
str(fileIndex,indexStr);
filename:='output\' +reply +indexStr +'.txt';
outTextXY(10,20,filename);
assign(FXfile,filename);
rewrite(FXfile);
write(FXfile,seqName,' =', genomeSize:6);
writeln(FXfile,' starting at ',startIndex,' and ending at ',startIndex+pinLength-1);
write(FXfile,'Temp.= ',T:1:1,' degC',' viscosity = ',viscosity*1000:1:4,' cp');
writeln(FXfile,' pullRate= ', pullRate:1:0,' nm/sec');
writeZipComments; {thermo parameters used in zipper.pas}
write(FXfile,'BeadRadius = ',r*1e6:2:2,'um');
writeln(FXfile,'  kTrap = ',kTrap*1e6:0:0,' pN/um');writeln(FXfile, 'pullNum        Force(pN)     TrapPos(um)     forkPos');
end;

procedure consultMenu;
begin
  ch:= readkey;
  case ch of
    'm':showMenu;
    'q': halt;
    'e':plotAxes;
    'c':changeColor;
    'b':begin plotAxes;showingBeads:=not showingBeads; end;
    'f':begin showingForce:=true; forcePix:=forcePix/2; plotAxes; end;
    'F':begin showingForce:=true; forcePix:=forcePix*2; plotAxes; end;
    'd':begin dsNum:=dsNum+1;ssNum:=ssNum-1;changeColor;end;
    'D':begin ssNum:=ssNum+1;dsNum:=dsNum-1;changeColor;end;
    'l':begin pixScale:=pixScale*2; plotAxes; end;
    'L':begin pixScale:=pixScale/2; plotAxes; end;
    'h': weight:=weight-(1e-13);
    'H': weight:=weight+(1e-13);
    'w': begin   holdingConstForce:= not holdingConstForce;
         weight := fltLightForce; end;
    'r','R':begin  showingReadings:=not ShowingReadings;
            if showingReadings then
                 begin resetTime:=time;
                 plotTime:=time+plotPeriod;
                 end;
            end;
    chr(0): begin ch:=readkey;
         case ord(ch) of
   {F4}     62:rampingForce:=not rampingForce;
   {F6}     64:begin initLightLever:=avLightLever;
               initForce:=avLightForce;
               end;
   {F7}     65: begin rampingForce:= not rampingForce;
                holdingConstForce:=false;
                if rampingForce then
                   begin promptTemperatures; promptForces; startOutputFile;  end
                else
                    begin close(FXfile); FXfileOpen :=false; end;
                end;
   {F8}     66: movingForward:= not movingForward;
{L Arrow}   75: piezoPosX:=piezoPosX-1e-9;
{R Arrow}   77: piezoPosX:=piezoPosX+1e-9;
         end;{inner case}
         end;{second readkey}
end; {outer case}
pixR:=round(r/pixScale); pixC:=round(1e-8/pixScale);
end;