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  // Database.java
20  // Since: May 25, 2010
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.bio;
26  
27  import java.io.Serializable;
28  
29  /**
30   * Database locator.
31   * 
32   * @author leo
33   * 
34   */
35  public class GenomeDB implements Serializable {
36  	/**
37  	 * 
38  	 */
39  	private static final long serialVersionUID = 1L;
40  
41  	public static enum DBType {
42  		AUTO, BED, SAM, BAM, FASTA, DAS, CytoBand;
43  
44  		private DBType() {
45  		}
46  
47  		public static String[] getDBTypeList() {
48  			DBType[] values = DBType.values();
49  			String[] dbTypes = new String[values.length];
50  			for (int i = 0; i < values.length; ++i) {
51  				dbTypes[i] = values[i].name();
52  			}
53  			return dbTypes;
54  		}
55  
56  	}
57  
58  	/**
59  	 * database path.
60  	 * 
61  	 * When the path starts with "local:", it searches for locally stored database files.
62  	 * 
63  	 * 
64  	 */
65  	public String path;
66  
67  	/**
68  	 * reference sequence name to which this genome DB is associated
69  	 */
70  	public String ref;
71  
72  	/**
73  	 * database type
74  	 */
75  	public DBType type = DBType.AUTO;
76  
77  	public GenomeDB() {
78  	}
79  
80  	protected GenomeDB(GenomeDB db) {
81  		this(db.type, db.path, db.ref);
82  	}
83  
84  	public GenomeDB(String path, String refSeq) {
85  		this(DBType.AUTO, path, refSeq);
86  	}
87  
88  	public GenomeDB(DBType type, String path, String ref) {
89  		if (type == null)
90  			type = DBType.AUTO;
91  		if (path == null)
92  			throw new NullPointerException("Database ID is null");
93  		if (ref == null)
94  			throw new NullPointerException("Reference is null");
95  
96  		this.type = type;
97  		this.path = path;
98  		this.ref = ref;
99  	}
100 
101 	@Override
102 	public String toString() {
103 		return "db(path:" + path + ", type:" + type + ", ref:" + ref + ")";
104 	}
105 
106 	public DBType resolveDBType() {
107 		return resolveDBType(this);
108 	}
109 
110 	/**
111 	 * Resolve the db type when the db type is AUTO.
112 	 * 
113 	 * @param db
114 	 * @return the resolved data type, or null if no appropriate db type is found
115 	 */
116 	public static DBType resolveDBType(GenomeDB db) {
117 		if (db.type == DBType.AUTO) {
118 			if (db.path.endsWith(".sam"))
119 				return DBType.SAM;
120 			else if (db.path.endsWith(".bam"))
121 				return DBType.BAM;
122 			else if (db.path.endsWith(".bed"))
123 				return DBType.BED;
124 			else if (db.path.endsWith(".fa"))
125 				return DBType.FASTA;
126 			else if (db.path.endsWith(".cytoband"))
127 				return DBType.CytoBand;
128 
129 			return null;
130 		}
131 		else
132 			return db.type;
133 	}
134 
135 	public static DBType resolveDBType(String path) {
136 		if (path.endsWith(".sam"))
137 			return DBType.SAM;
138 		else if (path.endsWith(".bam"))
139 			return DBType.BAM;
140 		else if (path.endsWith(".bed"))
141 			return DBType.BED;
142 		else if (path.endsWith(".fa"))
143 			return DBType.FASTA;
144 		else if (path.endsWith(".cytoband"))
145 			return DBType.CytoBand;
146 		return null;
147 	}
148 
149 }