View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 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  // CompactFASTAIndex.java
20  // Since: 2010/03/11
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.format.fasta;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.io.InputStreamReader;
30  import java.io.Reader;
31  import java.net.URL;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import org.utgenome.gwt.utgb.client.bio.CytoBand;
36  import org.xerial.core.XerialException;
37  import org.xerial.lens.SilkLens;
38  import org.xerial.util.ObjectHandler;
39  
40  /**
41   * A sequence index to packed FASTA files
42   * 
43   * @author leo
44   * 
45   */
46  public class CompactFASTAIndex {
47  
48  	/**
49  	 * sequence name (e.g., chr1, scaffold2)
50  	 */
51  	public String name;
52  	/**
53  	 * Full description written in FASTA file ( > chr1 ... ), without '>' indicator
54  	 */
55  	public String description;
56  
57  	/**
58  	 * Offset from the beginning of the packed file
59  	 */
60  	public long offset;
61  	/**
62  	 * sequence length
63  	 */
64  	public int length;
65  
66  	private static class IndexHolder implements ObjectHandler<CompactFASTAIndex> {
67  
68  		ArrayList<CompactFASTAIndex> index = new ArrayList<CompactFASTAIndex>();
69  
70  		public void init() throws Exception {
71  
72  		}
73  
74  		public void handle(CompactFASTAIndex input) throws Exception {
75  			index.add(input);
76  		}
77  
78  		public void finish() throws Exception {
79  
80  		}
81  
82  	}
83  
84  	public static List<CompactFASTAIndex> load(URL indexFile) throws XerialException, IOException {
85  		Reader in = new BufferedReader(new InputStreamReader(indexFile.openStream()));
86  		return load(in);
87  	}
88  
89  	public static List<CompactFASTAIndex> load(Reader indexFile) throws XerialException, IOException {
90  		IndexHolder holder = new IndexHolder();
91  		SilkLens.findFromSilk(indexFile, "sequence", CompactFASTAIndex.class, holder);
92  
93  		return holder.index;
94  	}
95  
96  	/**
97  	 * Convert to cyto band
98  	 * 
99  	 * @return
100 	 */
101 	public CytoBand toCytoBand() {
102 		return new CytoBand(name, 1, length, "", null);
103 	}
104 
105 }