Add still-not-working code for archiving jobs
[proteocache.git] / engine / compbio / engine / archive / ArchiveManager.java
diff --git a/engine/compbio/engine/archive/ArchiveManager.java b/engine/compbio/engine/archive/ArchiveManager.java
new file mode 100644 (file)
index 0000000..dabaf3a
--- /dev/null
@@ -0,0 +1,133 @@
+package compbio.engine.archive;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.Scanner;
+
+//import compbio.util.Util;
+
+/**
+ * Manage files in ProteoCache Archive
+ * 
+ * @author Alexander Sherstnev
+ * @version 1.0 November 2013
+ * 
+ */
+public class ArchiveManager implements Iterator<ArchivedJob> {
+       Archive archive;
+       //private final Scanner input;
+       /**
+        * Delimiter for the scanner
+        */
+       //private final String DELIM = ">";
+
+       /**
+        * Header data can contain non-ASCII symbols and read in UTF8
+        * 
+        * @param mainPath
+        *            the absolute path to the ProteoCache job archive
+        * @throws FileNotFoundException
+        *             if the input file is not found
+        * @throws IllegalStateException
+        *             if the close method was called on this instance
+        * 
+        */
+       public ArchiveManager(final String mainPath) throws FileNotFoundException {
+               /*
+               input = new Scanner(new File(mainPath), "UTF8");
+               input.useDelimiter(DELIM);
+               Runtime.getRuntime().addShutdownHook(new Thread() {
+
+                       @Override
+                       public void run() {
+                               if (input != null) {
+                                       input.close();
+                               }
+                       }
+               });
+               */
+       }
+       
+       public ArchiveManager(Archive ar) {
+               archive = ar;
+       }
+
+
+       /**
+        * {@inheritDoc}
+        * 
+        * @throws IllegalStateException
+        *             if the close method was called on this instance
+        */
+       @Override
+       public boolean hasNext() {
+               //return input.hasNext();
+               return true;
+       }
+
+       /**
+        * Reads the next FastaSequence from the input
+        * 
+        * @throws AssertionError
+        *             if the header or the sequence is missing
+        * @throws IllegalStateException
+        *             if the close method was called on this instance
+        * @throws MismatchException
+        *             - if there were no more FastaSequence's.
+        */
+       @Override
+       public ArchivedJob next() {
+               String path = "bla-bla-bla";
+               /*
+               String path = input.next();
+               while (fastaHeader.indexOf("\n") < 0 && input.hasNext()) {
+                       path = fastaHeader.concat(">");
+                       path = fastaHeader.concat(input.next());
+               }
+               */
+               return new ArchivedJob(path);
+       }
+
+       /**
+        * Not implemented
+        */
+       @Override
+       public void remove() {
+               throw new UnsupportedOperationException();
+       }
+
+       /**
+        * Call this method to close the connection to the input file if you want to
+        * free up the resources. The connection will be closed on the JVM shutdown
+        * if this method was not called explicitly. No further reading on this
+        * instance of the FastaReader will be possible after calling this method.
+        */
+       public void close() {
+               //input.close();
+       }
+
+       private static ArchivedJob toFastaSequence(final String singleFastaEntry) {
+
+               // assert !Util.isEmpty(singleFastaEntry) :
+               // "Empty String where FASTA sequence is expected!";
+
+               int nlineidx = singleFastaEntry.indexOf("\n");
+               if (nlineidx < 0) {
+                       throw new AssertionError(
+                                       "The FASTA sequence must contain the header information"
+                                                       + " separated by the new line from the sequence. Given sequence does not appear to "
+                                                       + "contain the header! Given data:\n "
+                                                       + singleFastaEntry);
+               }
+               String header = singleFastaEntry.substring(0, nlineidx);
+
+               /*
+                * if (Util.isEmpty(sequence)) { throw new AssertionError(
+                * "Empty sequences are not allowed! Please make sure the " +
+                * " data is in the FASTA format! Given data:\n " + singleFastaEntry); }
+                */
+               return new ArchivedJob(header);
+       }
+}