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