X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FForesterUtil.java;h=1c914c800799ee453b4130476fd4c7ce042a4e62;hb=6721c2fefe276eef3e17773812b05474d609ebc4;hp=78b01ead9ccb7095806c2ed0e48f56b80fee386c;hpb=038c34792757a86f24296de5683e722fab3f9307;p=jalview.git diff --git a/forester/java/src/org/forester/util/ForesterUtil.java b/forester/java/src/org/forester/util/ForesterUtil.java index 78b01ea..1c914c8 100644 --- a/forester/java/src/org/forester/util/ForesterUtil.java +++ b/forester/java/src/org/forester/util/ForesterUtil.java @@ -38,6 +38,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; +import java.io.Writer; import java.math.BigDecimal; import java.net.URL; import java.text.DateFormat; @@ -47,10 +48,12 @@ import java.text.NumberFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; @@ -92,6 +95,21 @@ public final class ForesterUtil { sb.append( separator ); } } + + final public static String getForesterLibraryInformation() { + return "forester " + ForesterConstants.FORESTER_VERSION + " (" + ForesterConstants.FORESTER_DATE + ")"; + } + + public static boolean seqIsLikelyToBeAa( final String s ) { + final String seq = s.toLowerCase(); + if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 ) + || ( seq.indexOf( 'q' ) > -1 ) || ( seq.indexOf( 'h' ) > -1 ) || ( seq.indexOf( 'k' ) > -1 ) + || ( seq.indexOf( 'w' ) > -1 ) || ( seq.indexOf( 's' ) > -1 ) || ( seq.indexOf( 'm' ) > -1 ) + || ( seq.indexOf( 'p' ) > -1 ) || ( seq.indexOf( 'v' ) > -1 ) ) { + return true; + } + return false; + } /** * This calculates a color. If value is equal to min the returned color is @@ -220,6 +238,27 @@ public final class ForesterUtil { return s.replaceAll( "[\\s]+", " " ); } + final public static void collection2file( final File file, final Collection data, final String separator ) + throws IOException { + final Writer writer = new BufferedWriter( new FileWriter( file ) ); + collection2writer( writer, data, separator ); + writer.close(); + } + + final public static void collection2writer( final Writer writer, final Collection data, final String separator ) + throws IOException { + boolean first = true; + for( final Object object : data ) { + if ( !first ) { + writer.write( separator ); + } + else { + first = false; + } + writer.write( object.toString() ); + } + } + final public static String colorToHex( final Color color ) { final String rgb = Integer.toHexString( color.getRGB() ); return rgb.substring( 2, rgb.length() ); @@ -536,6 +575,33 @@ public final class ForesterUtil { return map; } + final public static void map2file( final File file, + final Map data, + final String entry_separator, + final String data_separator ) throws IOException { + final Writer writer = new BufferedWriter( new FileWriter( file ) ); + map2writer( writer, data, entry_separator, data_separator ); + writer.close(); + } + + final public static void map2writer( final Writer writer, + final Map data, + final String entry_separator, + final String data_separator ) throws IOException { + boolean first = true; + for( final Entry entry : data.entrySet() ) { + if ( !first ) { + writer.write( data_separator ); + } + else { + first = false; + } + writer.write( entry.getKey().toString() ); + writer.write( entry_separator ); + writer.write( entry.getValue().toString() ); + } + } + final public static StringBuffer mapToStringBuffer( final Map map, final String key_value_separator ) { final StringBuffer sb = new StringBuffer(); for( final Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { @@ -669,20 +735,29 @@ public final class ForesterUtil { } final public static void printProgramInformation( final String prg_name, + final String desc, final String prg_version, final String date, final String email, - final String www ) { - final int l = prg_name.length() + prg_version.length() + date.length() + 4; + final String www, + final String based_on ) { + String my_prg_name = new String( prg_name ); + if ( !ForesterUtil.isEmpty( desc ) ) { + my_prg_name += ( " - " + desc ); + } + final int l = my_prg_name.length() + prg_version.length() + date.length() + 4; System.out.println(); - System.out.println( prg_name + " " + prg_version + " (" + date + ")" ); + System.out.println( my_prg_name + " " + prg_version + " (" + date + ")" ); for( int i = 0; i < l; ++i ) { System.out.print( "_" ); } System.out.println(); System.out.println(); - System.out.println( "WWW : " + www ); - System.out.println( "Contact: " + email ); + System.out.println( "WWW : " + www ); + System.out.println( "Contact : " + email ); + if ( !ForesterUtil.isEmpty( based_on ) ) { + System.out.println( "Based on: " + based_on ); + } if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) { System.out.println(); System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" ); @@ -690,6 +765,14 @@ public final class ForesterUtil { System.out.println(); } + final public static void printProgramInformation( final String prg_name, + final String prg_version, + final String date, + final String email, + final String www ) { + printProgramInformation( prg_name, null, prg_version, date, email, www, null ); + } + final public static void printWarningMessage( final String prg_name, final String message ) { System.out.println( "[" + prg_name + "] > warning: " + message ); } @@ -772,6 +855,10 @@ public final class ForesterUtil { return str.split( regex ); } + final public static String stringArrayToString( final String[] a ) { + return stringArrayToString( a, ", " ); + } + final public static String stringArrayToString( final String[] a, final String separator ) { final StringBuilder sb = new StringBuilder(); if ( ( a != null ) && ( a.length > 0 ) ) { @@ -783,6 +870,18 @@ public final class ForesterUtil { return sb.toString(); } + final public static String[] stringListToArray( final List list ) { + if ( list != null ) { + final String[] str = new String[ list.size() ]; + int i = 0; + for( final String l : list ) { + str[ i++ ] = l; + } + return str; + } + return null; + } + final public static String stringListToString( final List l, final String separator ) { final StringBuilder sb = new StringBuilder(); if ( ( l != null ) && ( l.size() > 0 ) ) { @@ -794,10 +893,6 @@ public final class ForesterUtil { return sb.toString(); } - final public static String stringArrayToString( final String[] a ) { - return stringArrayToString( a, ", " ); - } - final public static String[] stringSetToArray( final Set strings ) { final String[] str_array = new String[ strings.size() ]; int i = 0; @@ -807,18 +902,6 @@ public final class ForesterUtil { return str_array; } - final public static String[] stringListToArray( final List list ) { - if ( list != null ) { - final String[] str = new String[ list.size() ]; - int i = 0; - for( final String l : list ) { - str[ i++ ] = l; - } - return str; - } - return null; - } - final public static void unexpectedFatalError( final String prg_name, final Exception e ) { System.err.println(); System.err.println( "[" + prg_name