001 /* 002 * Copyright 2007, 2012 Stephen Fisher and Junhyong Kim, University of 003 * Pennsylvania. 004 * 005 * This file is part of Glo-DB. 006 * 007 * Glo-DB is free software: you can redistribute it and/or modify it 008 * under the terms of the GNU General Public License as published by 009 * the Free Software Foundation, either version 3 of the License, or 010 * (at your option) any later version. 011 * 012 * Glo-DB is distributed in the hope that it will be useful, but 013 * WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with Glo-DB. If not, see <http://www.gnu.org/licenses/>. 019 * 020 * @(#)FileIO.java 021 */ 022 023 package edu.upenn.gloDB.io; 024 025 import edu.upenn.gloDB.GloDBUtils; 026 import java.util.HashSet; 027 import java.util.Iterator; 028 029 /** 030 * Import and export utilities. 031 * 032 * @author Stephen Fisher 033 * @version $Id: FileIO.java,v 1.16.2.8 2007/03/01 21:17:33 fisher Exp $ 034 */ 035 036 public class FileIO { 037 038 public final static int GLODB = 1; 039 public final static int FASTA = 2; 040 public final static int GFF = 3; 041 public final static int GENBANK = 4; 042 043 /** 044 * This HashSet is used to store references to TrackFile types. 045 */ 046 private static HashSet trackFileTypes = new HashSet(); 047 048 /** 049 * This HashSet is used to store references to SequenceFile types. 050 */ 051 private static HashSet sequenceFileTypes = new HashSet(); 052 053 054 //-------------------------------------------------------------------------- 055 // Setters and Getters 056 057 /** Returns the set of Track file types. */ 058 public static HashSet getTrackFileTypes() { 059 return trackFileTypes; 060 } 061 062 /** Returns the set of Sequence file types. */ 063 public static HashSet getSequenceFileTypes() { 064 return sequenceFileTypes; 065 } 066 067 //-------------------------------------------------------------------------- 068 // Miscellaneous Methods 069 070 /** 071 * Convert from the integer constant value to a string equivalent. 072 */ 073 public static String convertConstant(int val) { 074 switch (val) { 075 case 1: return "GLODB"; 076 case 2: return "GFF"; 077 case 3: return "FASTA"; 078 case 4: return "GenBank"; 079 } 080 return ""; 081 } 082 083 /** Returns the DataFiles based on the file type. */ 084 public static HashSet getDataTypes(int type) { 085 if (type == GloDBUtils.TRACK) return getTrackFileTypes(); 086 else if (type == GloDBUtils.SEQUENCE) return getSequenceFileTypes(); 087 else return null; 088 } 089 090 /** Returns the DataFile based on the file type and type description. */ 091 public static DataFile getDataType(int type, String desc) { 092 if (type == GloDBUtils.TRACK) return getTrackFileType(desc); 093 else if (type == GloDBUtils.SEQUENCE) return getSequenceFileType(desc); 094 else return null; 095 } 096 097 /** Adds the Track file type to the list of possible types. */ 098 public static void addTrackFileType(TrackFile trackFileType) { 099 trackFileTypes.add(trackFileType); 100 } 101 102 /** Returns the TrackFile based on the file type description. */ 103 public static TrackFile getTrackFileType(String desc) { 104 if (trackFileTypes != null) { 105 for (Iterator i = trackFileTypes.iterator(); i.hasNext();) { 106 TrackFile trackFile = (TrackFile) i.next(); 107 if (desc == trackFile.getDesc()) return trackFile; 108 } 109 } 110 111 return null; 112 } 113 114 /** Returns the TrackFile based on the file type ID. */ 115 public static TrackFile getTrackFileType(int id) { 116 if (trackFileTypes != null) { 117 for (Iterator i = trackFileTypes.iterator(); i.hasNext();) { 118 TrackFile trackFile = (TrackFile) i.next(); 119 if (id == trackFile.getID()) return trackFile; 120 } 121 } 122 123 return null; 124 } 125 126 /** Adds the Seqeunce file type to the list of possible types. */ 127 public static void addSequenceFileType(SequenceFile sequenceFileType) { 128 sequenceFileTypes.add(sequenceFileType); 129 } 130 131 /** Returns the SequenceFile based on the file type description. */ 132 public static SequenceFile getSequenceFileType(String desc) { 133 if (sequenceFileTypes != null) { 134 for (Iterator i = sequenceFileTypes.iterator(); i.hasNext();) { 135 SequenceFile sequenceFile = (SequenceFile) i.next(); 136 if (desc == sequenceFile.getDesc()) return sequenceFile; 137 } 138 } 139 140 return null; 141 } 142 143 /** Returns the SequenceFile based on the file type ID. */ 144 public static SequenceFile getSequenceFileType(int id) { 145 if (sequenceFileTypes != null) { 146 for (Iterator i = sequenceFileTypes.iterator(); i.hasNext();) { 147 SequenceFile sequenceFile = (SequenceFile) i.next(); 148 if (id == sequenceFile.getID()) return sequenceFile; 149 } 150 } 151 152 return null; 153 } 154 155 } // FileIO.java