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 * @(#)GloDBUtils.java 021 */ 022 023 package edu.upenn.gloDB; 024 025 import java.awt.Toolkit; 026 import javax.swing.JTextArea; 027 import java.util.zip.Deflater; 028 import java.util.zip.Inflater; 029 import java.io.ByteArrayOutputStream; 030 031 /** 032 * GloDBUtils. Miscellaneous functions. 033 * 034 * @author Stephen Fisher 035 * @version $Id: GloDBUtils.java,v 1.1.2.9 2007/03/01 21:17:32 fisher Exp $ 036 */ 037 038 public class GloDBUtils { 039 040 public final static int TRACK = 1; 041 public final static int SEQUENCE = 2; 042 public final static int FEATURE = 3; 043 044 public final static int WARNING = 1; 045 public final static int ERROR = 2; 046 public final static int FEEDBACK = 3; 047 048 /** 049 * Amount of detailed feedback: 2 = lots, 1 = no warnings, 0 = no 050 * feedback. 051 */ 052 private static int VERBOSE = GloDBMain.userDefaults.getInt("VERBOSE", 1); 053 054 /** 055 * This flag is used in FeatureUtils.compareFeatures() to compare 056 * two Features. If 'true', then only the Feature source and 057 * start/stop positions will be used. If 'false', then the 058 * hashCode of the attributes HashMap will also be used. This 059 * allows for Features identically placed but with different 060 * attributes to coexist in a Track. Thus if two Features exist 061 * at the exact same location on a sequence, this flag will 062 * determine whether the attributes field will be used when 063 * deciding if these are in fact the same Feature. 064 */ 065 private static boolean IGNORE_ATTRIBUTES = GloDBMain.userDefaults.getBoolean("IGNORE_ATTRIBUTES", 066 false); 067 068 /** 069 * If not null, all error and warning messages will be sent here. 070 * This messages panel can be disabled by setting the value to null. 071 * When disabled, all messages printed will be sent to stderr, 072 * which will effectively display them in the console. 073 */ 074 public static JTextArea guiMessages = null; 075 076 //-------------------------------------------------------------------------- 077 // Getters and Setters 078 079 /** Set the VERBOSE flag. */ 080 public static void setVerbose(int verbose) { 081 VERBOSE = verbose; 082 GloDBMain.userDefaults.putInt("VERBOSE", verbose); 083 } 084 085 /** Get the VERBOSE flag. */ 086 public static int getVerbose() { return VERBOSE; } 087 088 /** Set the IGNORE_ATTRIBUTES flag. */ 089 public static void setIgnoreAttributes(boolean ignoreAttributes) { 090 IGNORE_ATTRIBUTES = ignoreAttributes; 091 GloDBMain.userDefaults.putBoolean("IGNORE_ATTRIBUTES", ignoreAttributes); 092 String msg = "Changing IGNORE_ATTIBUTES will not affect the features already loaded \n"; 093 msg += "into tracks but will affect any new features added."; 094 printMsg(msg, WARNING); 095 } 096 097 /** Get the IGNORE_ATTRIBUTES flag. */ 098 public static boolean ignoreAttributes() { return IGNORE_ATTRIBUTES; } 099 100 //-------------------------------------------------------------------------- 101 // Miscellaneous Methods 102 103 /** Returns true if 'str' is empty (ignores spaces) or null. */ 104 public static boolean isEmpty(String str) { 105 if ((str == null) || (str.trim().length() == 0)) return true; 106 else return false; 107 } 108 109 /** RE-set GloDB user defaults. */ 110 public static void resetGloDBDefaults() { 111 setVerbose(1); 112 setIgnoreAttributes(false); 113 } 114 115 /** 116 * Convert from the integer constant value to a string equivalent. 117 */ 118 public static String convertConstant(int val) { 119 switch (val) { 120 case TRACK: return "Track"; 121 case SEQUENCE: return "Sequence"; 122 case FEATURE: return "Feature"; 123 } 124 return ""; 125 } 126 127 /** Get the class name without any package info. */ 128 public static String getClassName(Object o) { 129 String classString = o.getClass().getName(); 130 int dotIndex = classString.lastIndexOf("."); 131 return classString.substring(dotIndex+1); 132 } 133 134 public static byte[] compressString(String val) { 135 byte[] bVal = val.getBytes(); 136 137 // Create the compressor with highest level of compression 138 Deflater compressor = new Deflater(); 139 compressor.setLevel(Deflater.BEST_SPEED); 140 141 // Give the compressor the data to compress 142 compressor.setInput(bVal); 143 compressor.finish(); 144 145 // Create an expandable byte array to hold the compressed data. 146 // You cannot use an array that's the same size as the orginal because 147 // there is no guarantee that the compressed data will be smaller than 148 // the uncompressed data. 149 ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length()); 150 151 // Compress the data 152 byte[] buf = new byte[2048]; 153 while (!compressor.finished()) { 154 int count = compressor.deflate(buf); 155 bos.write(buf, 0, count); 156 } 157 try { 158 bos.close(); 159 } catch (Exception e) { System.err.println(e); } 160 161 return bos.toByteArray(); 162 } 163 164 public static String uncompressString(byte[] compressedData) { 165 // Create the decompressor and give it the data to compress 166 Inflater decompressor = new Inflater(); 167 decompressor.setInput(compressedData); 168 169 // Create an expandable byte array to hold the decompressed data 170 ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length); 171 172 // Decompress the data 173 byte[] buf = new byte[1024]; 174 while (!decompressor.finished()) { 175 try { 176 int count = decompressor.inflate(buf); 177 bos.write(buf, 0, count); 178 } catch (Exception e) { System.err.println(e); } 179 } 180 try { 181 bos.close(); 182 } catch (Exception e) { System.err.println(e); } 183 184 return bos.toString(); 185 } 186 187 /** Display the message to either the gui or stderr. */ 188 public static void printMsg(String msg) { printMsg(msg, 0, true); } 189 190 /** Display the message to either the gui or stderr. */ 191 public static void printMsg(String msg, int type) { printMsg(msg, type, true); } 192 193 /** Display the message to either the gui or stderr. */ 194 public static void printMsg(String msg, boolean newline) { printMsg(msg, 0, newline); } 195 196 /** Display the error message to either the gui or stderr. */ 197 public static void printError(String msg) { printMsg(msg, ERROR, true); } 198 199 /** Display the warning message to either the gui or stderr. */ 200 public static void printWarning(String msg) { printMsg(msg, WARNING, true); } 201 202 /** 203 * Display the message to either the gui or stderr. The message 204 * type can be set which will add the appropriate label to the 205 * message (1 = warning, 2 = error). 206 */ 207 public static void printMsg(String msg, int type, boolean newline) { 208 // if VERBOSE = 0, then don't print any messages 209 if (VERBOSE == 0) return; 210 211 // add appropriate message label 212 switch (type) { 213 case FEEDBACK: 214 if (VERBOSE < 2) return; 215 break; 216 case WARNING: 217 if (VERBOSE < 2) return; 218 msg = " * WARNING: " + msg; 219 break; 220 case ERROR: 221 if (VERBOSE < 1) return; 222 msg = " ** ERROR: " + msg; 223 Toolkit.getDefaultToolkit().beep(); 224 break; 225 // default: msg = " " + msg; 226 } 227 228 if (newline) msg += "\n"; 229 230 if (guiMessages == null) System.err.print(msg); 231 else guiMessages.append(msg); 232 } 233 234 } // GloDBUtils.java