Apply formatting
[proteocache.git] / engine / compbio / engine / archive / ArchiveManager.java
1 package compbio.engine.archive;
2
3 import java.io.FileNotFoundException;
4
5 import java.util.Iterator;
6
7 //import compbio.util.Util;
8
9 /**
10  * Manage files in ProteoCache Archive
11  * 
12  * @author Alexander Sherstnev
13  * @version 1.0 November 2013
14  * 
15  */
16 public class ArchiveManager implements Iterator<ArchivedJob> {
17         Archive archive;
18
19         // private final Scanner input;
20         /**
21          * Delimiter for the scanner
22          */
23         // private final String DELIM = ">";
24
25         /**
26          * Header data can contain non-ASCII symbols and read in UTF8
27          * 
28          * @param mainPath
29          *            the absolute path to the ProteoCache job archive
30          * @throws FileNotFoundException
31          *             if the input file is not found
32          * @throws IllegalStateException
33          *             if the close method was called on this instance
34          * 
35          */
36         public ArchiveManager(final String mainPath) throws FileNotFoundException {
37                 /*
38                  * input = new Scanner(new File(mainPath), "UTF8");
39                  * input.useDelimiter(DELIM); Runtime.getRuntime().addShutdownHook(new
40                  * Thread() {
41                  * 
42                  * @Override public void run() { if (input != null) { input.close(); } }
43                  * });
44                  */
45         }
46
47         public ArchiveManager(Archive ar) {
48                 archive = ar;
49         }
50
51         /**
52          * {@inheritDoc}
53          * 
54          * @throws IllegalStateException
55          *             if the close method was called on this instance
56          */
57         @Override
58         public boolean hasNext() {
59                 // return input.hasNext();
60                 return true;
61         }
62
63         /**
64          * Reads the next FastaSequence from the input
65          * 
66          * @throws AssertionError
67          *             if the header or the sequence is missing
68          * @throws IllegalStateException
69          *             if the close method was called on this instance
70          * @throws MismatchException
71          *             - if there were no more FastaSequence's.
72          */
73         @Override
74         public ArchivedJob next() {
75                 String path = "bla-bla-bla";
76                 /*
77                  * String path = input.next(); while (fastaHeader.indexOf("\n") < 0 &&
78                  * input.hasNext()) { path = fastaHeader.concat(">"); path =
79                  * fastaHeader.concat(input.next()); }
80                  */
81                 return new ArchivedJob(path);
82         }
83
84         /**
85          * Not implemented
86          */
87         @Override
88         public void remove() {
89                 throw new UnsupportedOperationException();
90         }
91
92         /**
93          * Call this method to close the connection to the input file if you want to
94          * free up the resources. The connection will be closed on the JVM shutdown
95          * if this method was not called explicitly. No further reading on this
96          * instance of the FastaReader will be possible after calling this method.
97          */
98         public void close() {
99                 // input.close();
100         }
101
102         private static ArchivedJob toFastaSequence(final String singleFastaEntry) {
103
104                 // assert !Util.isEmpty(singleFastaEntry) :
105                 // "Empty String where FASTA sequence is expected!";
106
107                 int nlineidx = singleFastaEntry.indexOf("\n");
108                 if (nlineidx < 0) {
109                         throw new AssertionError("The FASTA sequence must contain the header information"
110                                         + " separated by the new line from the sequence. Given sequence does not appear to "
111                                         + "contain the header! Given data:\n " + singleFastaEntry);
112                 }
113                 String header = singleFastaEntry.substring(0, nlineidx);
114
115                 /*
116                  * if (Util.isEmpty(sequence)) { throw new AssertionError(
117                  * "Empty sequences are not allowed! Please make sure the " +
118                  * " data is in the FASTA format! Given data:\n " + singleFastaEntry); }
119                  */
120                 return new ArchivedJob(header);
121         }
122 }