Okay, Here's the first working version of my progress monitor. Yes, it's very, very un-Mac-like.
// read a text file and match certain items on separate lines
import java.io.*;
class Keyboard
{
InputStreamReader inStream;
BufferedReader stdInStream;
// override implicit constructor to open the input stream and attach it
// to a stream reader
Keyboard()
{
inStream = new InputStreamReader(System.in);
stdInStream = new BufferedReader(inStream);
}
// get one character
char read()
{
char aChar = ' ';
try
{
aChar = (char)stdInStream.read();
}
// empty exception handling
catch (Exception e)
{
}
return aChar;
} // end of read()
// get a whole line and return it as a String
String readln()
{
String aString = " ";
try
{
aString = (String)stdInStream.readLine();
}
// empty exception handling
catch (Exception e)
{
}
return aString;
} // end of readln()
} // end of Keyboard class
class ReadAndParse
{
private static String fileName;
private static String line;
private static String proteinName = "";
private static float completed = -1;
private static float remaining = -1;
private static float totalNbrOfFrames = -1;
private static float currentFrame = -1;
private static int progressPercentage = -1;
private static String junk;
private static boolean proteinFound = false;
private static boolean totalsFound = false;
private static boolean framesFound = false;
public static String match(String toSearch, String toMatch, String start, String end)
{
int posFrom = -1, posTo = -1;
String matchFound = "";
posFrom = toSearch.indexOf(toMatch);
if(posFrom >= 0)
{
posFrom = toSearch.indexOf(start, posFrom); // character after search word
posFrom++;
posTo = toSearch.indexOf(end , posFrom); // character after parameter
if(posTo >= 0)
{
// get the token that follows
matchFound = toSearch.substring(posFrom, posTo);
}
else
{
// extra space not found before end of line
// get the token that follows
matchFound = toSearch.substring(posFrom);
}
}
return matchFound;
}
public static void main(String []args)
{
Keyboard kb = new Keyboard();
System.out.println("Please enter the file name to be searched");
fileName = new String(kb.readln());
try
{
BufferedReader inputFile = new BufferedReader(new FileReader(fileName));
// file should be open, read it
proteinFound = false;
line = inputFile.readLine();
while(line != null && proteinFound == false)
{
// find Protein name
proteinName = match(line, "Protein:", " ", " ");
if(proteinName != "") // non-blank
{
proteinFound = true;
break;
}
line = inputFile.readLine();
} // end while ! proteinFound
framesFound = false;
line = inputFile.readLine();
while(line != null && totalsFound == false)
{
// find total number of frames
junk = match(line, "Completed:", " ", ",");
if(junk != "")
{
completed = Float.parseFloat(junk);
}
junk = match(line, "Remaining:", " ", " ");
if(junk != "")
{
remaining = Float.parseFloat(junk);
totalNbrOfFrames = completed + remaining;
totalsFound = true;
}
line = inputFile.readLine();
} // while ! totalsFound
// find highest frame number
framesFound = false;
line = inputFile.readLine();
while(line != null && framesFound == false)
{
junk = match(line, "Finished a frame", "(", ")");
if(junk != "")
{
currentFrame = Float.parseFloat(junk);
framesFound = true;
}
line = inputFile.readLine();
} // end while ! framesFound
// got at least one, check for the highest
line = inputFile.readLine();
while(line != null && framesFound == true)
{
junk = match(line, "Finished a frame", "(", ")");
if(junk != "")
{
currentFrame = Float.parseFloat(junk);
}
else
{
framesFound = false;
}
line = inputFile.readLine();
} // end while framesFound
// do the math
progressPercentage = (int) ( 100 * (currentFrame / totalNbrOfFrames));
System.out.println("Protein is " + proteinName);
System.out.println("Latest frame is " + currentFrame);
System.out.println("Total number of frames are " + totalNbrOfFrames);
System.out.println("Progress is currently at: " + progressPercentage + "%");
}
catch(IOException e)
{
System.out.println("The file was not found. Please check file name and/or path");
}
} // end main()
} // end class
You may compile it by putting it in a folder and typing javac ReadAndParse.java.
This will produce two class files:
Keyboard.class
ReadAndParse.class
You may then run it within the Terminal app. by typing java ReadAndParse.
When it requests, please provide it the file name (and full path if it's in a different folder) where the logfile exists.
If I put it in the same folder with the logfile, it asks for the file name and I type logfile_01.txt.
If I put in in a different folder, I must type something like ~/FAH2/work/logfile_01.txt or if I can see the file in the Finder, I can drag it to the Terminal and the name will magically be pasted there.
Anyway, it's a start. I should have a working GUI version sometime in the coming week.
// read a text file and match certain items on separate lines
import java.io.*;
class Keyboard
{
InputStreamReader inStream;
BufferedReader stdInStream;
// override implicit constructor to open the input stream and attach it
// to a stream reader
Keyboard()
{
inStream = new InputStreamReader(System.in);
stdInStream = new BufferedReader(inStream);
}
// get one character
char read()
{
char aChar = ' ';
try
{
aChar = (char)stdInStream.read();
}
// empty exception handling
catch (Exception e)
{
}
return aChar;
} // end of read()
// get a whole line and return it as a String
String readln()
{
String aString = " ";
try
{
aString = (String)stdInStream.readLine();
}
// empty exception handling
catch (Exception e)
{
}
return aString;
} // end of readln()
} // end of Keyboard class
class ReadAndParse
{
private static String fileName;
private static String line;
private static String proteinName = "";
private static float completed = -1;
private static float remaining = -1;
private static float totalNbrOfFrames = -1;
private static float currentFrame = -1;
private static int progressPercentage = -1;
private static String junk;
private static boolean proteinFound = false;
private static boolean totalsFound = false;
private static boolean framesFound = false;
public static String match(String toSearch, String toMatch, String start, String end)
{
int posFrom = -1, posTo = -1;
String matchFound = "";
posFrom = toSearch.indexOf(toMatch);
if(posFrom >= 0)
{
posFrom = toSearch.indexOf(start, posFrom); // character after search word
posFrom++;
posTo = toSearch.indexOf(end , posFrom); // character after parameter
if(posTo >= 0)
{
// get the token that follows
matchFound = toSearch.substring(posFrom, posTo);
}
else
{
// extra space not found before end of line
// get the token that follows
matchFound = toSearch.substring(posFrom);
}
}
return matchFound;
}
public static void main(String []args)
{
Keyboard kb = new Keyboard();
System.out.println("Please enter the file name to be searched");
fileName = new String(kb.readln());
try
{
BufferedReader inputFile = new BufferedReader(new FileReader(fileName));
// file should be open, read it
proteinFound = false;
line = inputFile.readLine();
while(line != null && proteinFound == false)
{
// find Protein name
proteinName = match(line, "Protein:", " ", " ");
if(proteinName != "") // non-blank
{
proteinFound = true;
break;
}
line = inputFile.readLine();
} // end while ! proteinFound
framesFound = false;
line = inputFile.readLine();
while(line != null && totalsFound == false)
{
// find total number of frames
junk = match(line, "Completed:", " ", ",");
if(junk != "")
{
completed = Float.parseFloat(junk);
}
junk = match(line, "Remaining:", " ", " ");
if(junk != "")
{
remaining = Float.parseFloat(junk);
totalNbrOfFrames = completed + remaining;
totalsFound = true;
}
line = inputFile.readLine();
} // while ! totalsFound
// find highest frame number
framesFound = false;
line = inputFile.readLine();
while(line != null && framesFound == false)
{
junk = match(line, "Finished a frame", "(", ")");
if(junk != "")
{
currentFrame = Float.parseFloat(junk);
framesFound = true;
}
line = inputFile.readLine();
} // end while ! framesFound
// got at least one, check for the highest
line = inputFile.readLine();
while(line != null && framesFound == true)
{
junk = match(line, "Finished a frame", "(", ")");
if(junk != "")
{
currentFrame = Float.parseFloat(junk);
}
else
{
framesFound = false;
}
line = inputFile.readLine();
} // end while framesFound
// do the math
progressPercentage = (int) ( 100 * (currentFrame / totalNbrOfFrames));
System.out.println("Protein is " + proteinName);
System.out.println("Latest frame is " + currentFrame);
System.out.println("Total number of frames are " + totalNbrOfFrames);
System.out.println("Progress is currently at: " + progressPercentage + "%");
}
catch(IOException e)
{
System.out.println("The file was not found. Please check file name and/or path");
}
} // end main()
} // end class
You may compile it by putting it in a folder and typing javac ReadAndParse.java.
This will produce two class files:
Keyboard.class
ReadAndParse.class
You may then run it within the Terminal app. by typing java ReadAndParse.
When it requests, please provide it the file name (and full path if it's in a different folder) where the logfile exists.
If I put it in the same folder with the logfile, it asks for the file name and I type logfile_01.txt.
If I put in in a different folder, I must type something like ~/FAH2/work/logfile_01.txt or if I can see the file in the Finder, I can drag it to the Terminal and the name will magically be pasted there.
Anyway, it's a start. I should have a working GUI version sometime in the coming week.