View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2010 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // FASTA.java
20  // Since: 2010/10/21
21  //
22  //--------------------------------------
23  package org.utgenome.format.fasta;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.utgenome.UTGBException;
31  
32  public class FASTA {
33  
34  	Map<String, RawStringWrapper> chrToSequence = new HashMap<String, RawStringWrapper>();
35  
36  	public FASTA(File fastaFile) throws IOException, UTGBException {
37  		loadFASTA(fastaFile);
38  	}
39  
40  	public static class RawStringWrapper implements GenomeSequence {
41  
42  		public final String seq;
43  
44  		public RawStringWrapper(String seq) {
45  			this.seq = seq;
46  		}
47  
48  		public int length() {
49  			return seq.length();
50  		}
51  
52  		public char charAt(int index) {
53  			return seq.charAt(index);
54  		}
55  
56  	}
57  
58  	public String getRawSequence(String chr) {
59  		return getSequence(chr).seq;
60  	}
61  
62  	public RawStringWrapper getSequence(String chr) {
63  		return chrToSequence.get(chr);
64  	}
65  
66  	void loadFASTA(File fastaFile) throws IOException, UTGBException {
67  		FASTAPullParser fastaPullParser = new FASTAPullParser(fastaFile);
68  		for (FASTASequence seq; (seq = fastaPullParser.nextSequence()) != null;) {
69  			chrToSequence.put(seq.getSequenceName(), new RawStringWrapper(seq.getSequence()));
70  		}
71  		fastaPullParser.close();
72  	}
73  
74  }