X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FForesterUtil.java;h=336c88763124454613da3df163c1535e93f9810a;hb=bc282cbd857f4fb9decdc00b278640bcf146a645;hp=58dbf453bb55a9af5823952e689569056a8df3ea;hpb=3be5f9e46469980f068a9f8170e1d20849b5eaf8;p=jalview.git diff --git a/forester/java/src/org/forester/util/ForesterUtil.java b/forester/java/src/org/forester/util/ForesterUtil.java index 58dbf45..336c887 100644 --- a/forester/java/src/org/forester/util/ForesterUtil.java +++ b/forester/java/src/org/forester/util/ForesterUtil.java @@ -61,6 +61,11 @@ import java.util.TreeMap; import java.util.TreeSet; import java.util.regex.Pattern; +import org.forester.phylogeny.PhylogenyNode; +import org.forester.phylogeny.data.Distribution; +import org.forester.phylogeny.data.Sequence; +import org.forester.phylogeny.data.Taxonomy; + public final class ForesterUtil { public final static String FILE_SEPARATOR = System.getProperty( "file.separator" ); @@ -90,12 +95,44 @@ public final class ForesterUtil { private ForesterUtil() { } + public static void ensurePresenceOfTaxonomy( final PhylogenyNode node ) { + if ( !node.getNodeData().isHasTaxonomy() ) { + node.getNodeData().setTaxonomy( new Taxonomy() ); + } + } + + public static void ensurePresenceOfSequence( final PhylogenyNode node ) { + if ( !node.getNodeData().isHasSequence() ) { + node.getNodeData().setSequence( new Sequence() ); + } + } + + final public static void ensurePresenceOfDistribution( final PhylogenyNode node ) { + if ( !node.getNodeData().isHasDistribution() ) { + node.getNodeData().setDistribution( new Distribution( "" ) ); + } + } + + final public static void ensurePresenceOfDate( final PhylogenyNode node ) { + if ( !node.getNodeData().isHasDate() ) { + node.getNodeData().setDate( new org.forester.phylogeny.data.Date() ); + } + } + final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) { if ( sb.length() > 0 ) { sb.append( separator ); } } + public static boolean isWindowns() { + return ForesterUtil.OS_NAME.toLowerCase().indexOf( "win" ) > -1; + } + + 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 ) @@ -300,6 +337,14 @@ public final class ForesterUtil { return new BufferedWriter( new FileWriter( file ) ); } + final public static EasyWriter createEasyWriter( final File file ) throws IOException { + return new EasyWriter( createBufferedWriter( file ) ); + } + + final public static BufferedWriter createEasyWriter( final String name ) throws IOException { + return createEasyWriter( createFileForWriting( name ) ); + } + final public static BufferedWriter createBufferedWriter( final String name ) throws IOException { return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) ); } @@ -319,6 +364,16 @@ public final class ForesterUtil { System.exit( -1 ); } + public static void fatalErrorIfFileNotReadable( final String prg_name, final File file ) { + final String error = isReadableFile( file ); + if ( !isEmpty( error ) ) { + System.err.println(); + System.err.println( "[" + prg_name + "] > " + error ); + System.err.println(); + System.exit( -1 ); + } + } + public static String[] file2array( final File file ) throws IOException { final List list = file2list( file ); final String[] ary = new String[ list.size() ]; @@ -466,7 +521,7 @@ public final class ForesterUtil { } final public static boolean isEven( final int n ) { - return n % 2 == 0; + return ( n % 2 ) == 0; } /** @@ -486,10 +541,9 @@ public final class ForesterUtil { if ( ( a.length < 1 ) || ( b.length < 1 ) ) { return false; } - for( int i = 0; i < a.length; ++i ) { - final String ai = a[ i ]; - for( int j = 0; j < b.length; ++j ) { - if ( ( ai != null ) && ( b[ j ] != null ) && ai.equals( b[ j ] ) ) { + for( final String ai : a ) { + for( final String element : b ) { + if ( ( ai != null ) && ( element != null ) && ai.equals( element ) ) { return true; } } @@ -717,7 +771,7 @@ public final class ForesterUtil { } final public static void printErrorMessage( final String prg_name, final String message ) { - System.out.println( "[" + prg_name + "] > error: " + message ); + System.err.println( "[" + prg_name + "] > error: " + message ); } final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) { @@ -731,20 +785,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 + "]" ); @@ -752,6 +815,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 ); } @@ -775,7 +846,7 @@ public final class ForesterUtil { */ final public static String removeWhiteSpace( String s ) { int i; - for( i = 0; i <= s.length() - 1; i++ ) { + for( i = 0; i <= ( s.length() - 1 ); i++ ) { if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' ) || ( s.charAt( i ) == '\r' ) ) { s = s.substring( 0, i ) + s.substring( i + 1 ); @@ -841,7 +912,7 @@ public final class ForesterUtil { final public static String stringArrayToString( final String[] a, final String separator ) { final StringBuilder sb = new StringBuilder(); if ( ( a != null ) && ( a.length > 0 ) ) { - for( int i = 0; i < a.length - 1; ++i ) { + for( int i = 0; i < ( a.length - 1 ); ++i ) { sb.append( a[ i ] + separator ); } sb.append( a[ a.length - 1 ] ); @@ -864,7 +935,7 @@ public final class ForesterUtil { final public static String stringListToString( final List l, final String separator ) { final StringBuilder sb = new StringBuilder(); if ( ( l != null ) && ( l.size() > 0 ) ) { - for( int i = 0; i < l.size() - 1; ++i ) { + for( int i = 0; i < ( l.size() - 1 ); ++i ) { sb.append( l.get( i ) + separator ); } sb.append( l.get( l.size() - 1 ) ); @@ -884,7 +955,7 @@ public final class ForesterUtil { final public static void unexpectedFatalError( final String prg_name, final Exception e ) { System.err.println(); System.err.println( "[" + prg_name - + "] > unexpected error (Should not have occured! Please contact program author(s).)" ); + + "] > Unexpected error. Should not have occured! Please contact program author(s)." ); e.printStackTrace( System.err ); System.err.println(); System.exit( -1 ); @@ -893,7 +964,7 @@ public final class ForesterUtil { final public static void unexpectedFatalError( final String prg_name, final String message ) { System.err.println(); System.err.println( "[" + prg_name - + "] > unexpected error. Should not have occured! Please contact program author(s)." ); + + "] > Unexpected error. Should not have occured! Please contact program author(s)." ); System.err.println( message ); System.err.println(); System.exit( -1 ); @@ -902,7 +973,7 @@ public final class ForesterUtil { final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) { System.err.println(); System.err.println( "[" + prg_name - + "] > unexpected error. Should not have occured! Please contact program author(s)." ); + + "] > Unexpected error. Should not have occured! Please contact program author(s)." ); System.err.println( message ); e.printStackTrace( System.err ); System.err.println(); @@ -922,7 +993,7 @@ public final class ForesterUtil { ls = -1; start = i + 1; } - if ( i > start + width - 1 ) { + if ( i > ( ( start + width ) - 1 ) ) { if ( ls != -1 ) { sb.setCharAt( ls, '\n' ); start = ls + 1;