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  // BEDGene.java
20  // Since: 2010/04/29
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.format.bed;
26  
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  import java.util.ArrayList;
30  import java.util.StringTokenizer;
31  
32  import org.utgenome.gwt.utgb.client.bio.BEDGene;
33  import org.utgenome.gwt.utgb.client.bio.CDS;
34  import org.utgenome.gwt.utgb.client.bio.Exon;
35  import org.utgenome.gwt.utgb.client.bio.Gene;
36  import org.utgenome.gwt.utgb.client.bio.OnGenome;
37  
38  /**
39   * Representing each gene line of BED format
40   * 
41   * @author leo
42   * 
43   */
44  public class BEDEntry extends BEDGene {
45  
46  	private static final long serialVersionUID = 1L;
47  
48  	public static BEDEntry createFromResultSet(String chr, ResultSet rs) throws SQLException {
49  
50  		BEDEntry gene = new BEDEntry();
51  		gene.coordinate = chr;
52  		gene.setStart(rs.getInt(1));
53  		gene.setEnd(rs.getInt(2));
54  
55  		gene.setName(rs.getString(3));
56  		gene.score = rs.getInt(4);
57  		gene.setStrand(rs.getString(5));
58  
59  		ArrayList<int[]> regionList = readRegions(rs.getString(6));
60  		for (int[] region : regionList) {
61  			CDS cds = new CDS(region[0], region[1]);
62  			gene.addCDS(cds);
63  		}
64  
65  		regionList = readRegions(rs.getString(7));
66  		for (int[] region : regionList) {
67  			Exon exon = new Exon(region[0], region[1]);
68  			gene.addExon(exon);
69  		}
70  
71  		gene.setColor(rs.getString(8));
72  
73  		return gene;
74  	}
75  
76  	private static ArrayList<int[]> readRegions(String string) {
77  		ArrayList<int[]> res = new ArrayList<int[]>();
78  
79  		StringTokenizer st = new StringTokenizer(string, "[] ,");
80  		while (st.hasMoreTokens()) {
81  			String str = st.nextToken();
82  
83  			// get start of region
84  			if (str.startsWith("(")) {
85  				int[] region = new int[2];
86  				region[0] = Integer.valueOf(str.substring(1)).intValue();
87  
88  				// get end of region
89  				while (st.hasMoreTokens()) {
90  					str = st.nextToken();
91  					if (str.endsWith(")")) {
92  						region[1] = Integer.valueOf(str.substring(0, str.length() - 1)).intValue();
93  						res.add(region);
94  						break;
95  					}
96  				}
97  			}
98  		}
99  		return res;
100 	}
101 
102 	@Override
103 	public String toString() {
104 		return String.format("%s: %s:%d-%d\t%s\t%s\t%s\t%s", getName(), coordinate, getStart(), getEnd(), getStrand(), getCDS(), getExon(), getColor());
105 
106 	}
107 
108 }