2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.util;
28 import java.awt.Color;
29 import java.io.BufferedReader;
30 import java.io.BufferedWriter;
32 import java.io.FileInputStream;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.FileReader;
36 import java.io.FileWriter;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.io.StringReader;
41 import java.io.Writer;
42 import java.math.BigDecimal;
44 import java.text.DateFormat;
45 import java.text.DecimalFormat;
46 import java.text.DecimalFormatSymbols;
47 import java.text.NumberFormat;
48 import java.text.ParseException;
49 import java.text.SimpleDateFormat;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Date;
53 import java.util.List;
55 import java.util.Map.Entry;
57 import java.util.SortedMap;
58 import java.util.SortedSet;
59 import java.util.TreeMap;
60 import java.util.TreeSet;
61 import java.util.regex.Matcher;
62 import java.util.regex.Pattern;
64 import org.forester.archaeopteryx.Constants;
65 import org.forester.phylogeny.PhylogenyNode;
66 import org.forester.phylogeny.data.Distribution;
67 import org.forester.phylogeny.data.Sequence;
68 import org.forester.phylogeny.data.Taxonomy;
69 import org.forester.protein.BasicProtein;
70 import org.forester.protein.Domain;
71 import org.forester.protein.Protein;
72 import org.forester.surfacing.SurfacingUtil;
74 public final class ForesterUtil {
76 public final static String FILE_SEPARATOR = System.getProperty( "file.separator" );
77 public final static String LINE_SEPARATOR = System.getProperty( "line.separator" );
78 public final static String JAVA_VENDOR = System.getProperty( "java.vendor" );
79 public final static String JAVA_VERSION = System.getProperty( "java.version" );
80 public final static String OS_ARCH = System.getProperty( "os.arch" );
81 public final static String OS_NAME = System.getProperty( "os.name" );
82 public final static String OS_VERSION = System.getProperty( "os.version" );
83 public final static Pattern PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s]" );
84 public final static double ZERO_DIFF = 1.0E-9;
85 public static final BigDecimal NULL_BD = new BigDecimal( 0 );
86 public static final NumberFormat FORMATTER_9;
87 public static final NumberFormat FORMATTER_6;
88 public static final NumberFormat FORMATTER_06;
89 public static final NumberFormat FORMATTER_3;
90 public static final String NCBI_PROTEIN = "http://www.ncbi.nlm.nih.gov/protein/";
91 public static final String NCBI_NUCCORE = "http://www.ncbi.nlm.nih.gov/nuccore/";
92 public final static String UNIPROT_KB = "http://www.uniprot.org/uniprot/";
93 public static final String NCBI_GI = "http://www.ncbi.nlm.nih.gov/protein/gi:";
95 final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
96 dfs.setDecimalSeparator( '.' );
97 // dfs.setGroupingSeparator( ( char ) 0 );
98 FORMATTER_9 = new DecimalFormat( "#.#########", dfs );
99 FORMATTER_6 = new DecimalFormat( "#.######", dfs );
100 FORMATTER_06 = new DecimalFormat( "0.######", dfs );
101 FORMATTER_3 = new DecimalFormat( "#.###", dfs );
104 private ForesterUtil() {
107 public static int calculateOverlap( final Domain domain, final List<Boolean> covered_positions ) {
108 int overlap_count = 0;
109 for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
110 if ( ( i < covered_positions.size() ) && ( covered_positions.get( i ) == true ) ) {
114 return overlap_count;
117 final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) {
118 if ( sb.length() > 0 ) {
119 sb.append( separator );
125 * Example regarding engulfment: ------------0.1 ----------0.2 --0.3 =>
126 * domain with 0.3 is ignored
128 * -----------0.1 ----------0.2 --0.3 => domain with 0.3 is ignored
131 * ------------0.1 ----------0.3 --0.2 => domains with 0.3 and 0.2 are _not_
134 * @param max_allowed_overlap
135 * maximal allowed overlap (inclusive) to be still considered not
136 * overlapping (zero or negative value to allow any overlap)
137 * @param remove_engulfed_domains
138 * to remove domains which are completely engulfed by coverage of
139 * domains with better support
143 public static Protein removeOverlappingDomains( final int max_allowed_overlap,
144 final boolean remove_engulfed_domains,
145 final Protein protein ) {
146 final Protein pruned_protein = new BasicProtein( protein.getProteinId().getId(), protein.getSpecies()
147 .getSpeciesId(), protein.getLength() );
148 final List<Domain> sorted = SurfacingUtil.sortDomainsWithAscendingConfidenceValues( protein );
149 final List<Boolean> covered_positions = new ArrayList<Boolean>();
150 for( final Domain domain : sorted ) {
151 if ( ( ( max_allowed_overlap < 0 ) || ( ForesterUtil.calculateOverlap( domain, covered_positions ) <= max_allowed_overlap ) )
152 && ( !remove_engulfed_domains || !isEngulfed( domain, covered_positions ) ) ) {
153 final int covered_positions_size = covered_positions.size();
154 for( int i = covered_positions_size; i < domain.getFrom(); ++i ) {
155 covered_positions.add( false );
157 final int new_covered_positions_size = covered_positions.size();
158 for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
159 if ( i < new_covered_positions_size ) {
160 covered_positions.set( i, true );
163 covered_positions.add( true );
166 pruned_protein.addProteinDomain( domain );
169 return pruned_protein;
173 * Returns true is Domain domain falls in an uninterrupted stretch of
177 * @param covered_positions
180 public static boolean isEngulfed( final Domain domain, final List<Boolean> covered_positions ) {
181 for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
182 if ( ( i >= covered_positions.size() ) || ( covered_positions.get( i ) != true ) ) {
190 * This calculates a color. If value is equal to min the returned color is
191 * minColor, if value is equal to max the returned color is maxColor,
192 * otherwise a color 'proportional' to value is returned.
206 final public static Color calcColor( double value,
209 final Color minColor,
210 final Color maxColor ) {
217 final double x = ForesterUtil.calculateColorFactor( value, max, min );
218 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), maxColor.getRed(), x );
219 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), maxColor.getGreen(), x );
220 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), maxColor.getBlue(), x );
221 return new Color( red, green, blue );
225 * This calculates a color. If value is equal to min the returned color is
226 * minColor, if value is equal to max the returned color is maxColor, if
227 * value is equal to mean the returned color is meanColor, otherwise a color
228 * 'proportional' to value is returned -- either between min-mean or
238 * the mean/median value
247 final public static Color calcColor( double value,
251 final Color minColor,
252 final Color maxColor,
253 final Color meanColor ) {
260 if ( value < mean ) {
261 final double x = ForesterUtil.calculateColorFactor( value, mean, min );
262 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), meanColor.getRed(), x );
263 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), meanColor.getGreen(), x );
264 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), meanColor.getBlue(), x );
265 return new Color( red, green, blue );
267 else if ( value > mean ) {
268 final double x = ForesterUtil.calculateColorFactor( value, max, mean );
269 final int red = ForesterUtil.calculateColorComponent( meanColor.getRed(), maxColor.getRed(), x );
270 final int green = ForesterUtil.calculateColorComponent( meanColor.getGreen(), maxColor.getGreen(), x );
271 final int blue = ForesterUtil.calculateColorComponent( meanColor.getBlue(), maxColor.getBlue(), x );
272 return new Color( red, green, blue );
279 final public static String collapseWhiteSpace( final String s ) {
280 return s.replaceAll( "[\\s]+", " " );
283 final public static void collection2file( final File file, final Collection<?> data, final String separator )
285 final Writer writer = new BufferedWriter( new FileWriter( file ) );
286 collection2writer( writer, data, separator );
290 final public static void collection2writer( final Writer writer, final Collection<?> data, final String separator )
292 boolean first = true;
293 for( final Object object : data ) {
295 writer.write( separator );
300 writer.write( object.toString() );
304 final public static String colorToHex( final Color color ) {
305 final String rgb = Integer.toHexString( color.getRGB() );
306 return rgb.substring( 2, rgb.length() );
309 synchronized public static void copyFile( final File in, final File out ) throws IOException {
310 final FileInputStream in_s = new FileInputStream( in );
311 final FileOutputStream out_s = new FileOutputStream( out );
313 final byte[] buf = new byte[ 1024 ];
315 while ( ( i = in_s.read( buf ) ) != -1 ) {
316 out_s.write( buf, 0, i );
319 catch ( final IOException e ) {
323 if ( in_s != null ) {
326 if ( out_s != null ) {
332 final public static int countChars( final String str, final char c ) {
334 for( int i = 0; i < str.length(); ++i ) {
335 if ( str.charAt( i ) == c ) {
342 final public static BufferedWriter createBufferedWriter( final File file ) throws IOException {
343 if ( file.exists() ) {
344 throw new IOException( "[" + file + "] already exists" );
346 return new BufferedWriter( new FileWriter( file ) );
349 final public static BufferedWriter createBufferedWriter( final String name ) throws IOException {
350 return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) );
353 final public static EasyWriter createEasyWriter( final File file ) throws IOException {
354 return new EasyWriter( createBufferedWriter( file ) );
357 final public static BufferedWriter createEasyWriter( final String name ) throws IOException {
358 return createEasyWriter( createFileForWriting( name ) );
361 final public static File createFileForWriting( final String name ) throws IOException {
362 final File file = new File( name );
363 if ( file.exists() ) {
364 throw new IOException( "[" + name + "] already exists" );
369 final public static void ensurePresenceOfDate( final PhylogenyNode node ) {
370 if ( !node.getNodeData().isHasDate() ) {
371 node.getNodeData().setDate( new org.forester.phylogeny.data.Date() );
375 final public static void ensurePresenceOfDistribution( final PhylogenyNode node ) {
376 if ( !node.getNodeData().isHasDistribution() ) {
377 node.getNodeData().setDistribution( new Distribution( "" ) );
381 public static void ensurePresenceOfSequence( final PhylogenyNode node ) {
382 if ( !node.getNodeData().isHasSequence() ) {
383 node.getNodeData().setSequence( new Sequence() );
387 public static void ensurePresenceOfTaxonomy( final PhylogenyNode node ) {
388 if ( !node.getNodeData().isHasTaxonomy() ) {
389 node.getNodeData().setTaxonomy( new Taxonomy() );
393 public static void fatalError( final String message ) {
394 System.err.println();
395 System.err.println( "error: " + message );
396 System.err.println();
400 public static void fatalError( final String prg_name, final String message ) {
401 System.err.println();
402 System.err.println( "[" + prg_name + "] > " + message );
403 System.err.println();
407 public static void fatalErrorIfFileNotReadable( final File file ) {
408 final String error = isReadableFile( file );
409 if ( !isEmpty( error ) ) {
410 System.err.println();
411 System.err.println( "error: " + error );
412 System.err.println();
417 public static void fatalErrorIfFileNotReadable( final String prg_name, final File file ) {
418 final String error = isReadableFile( file );
419 if ( !isEmpty( error ) ) {
420 System.err.println();
421 System.err.println( "[" + prg_name + "] > " + error );
422 System.err.println();
427 public static String[] file2array( final File file ) throws IOException {
428 final List<String> list = file2list( file );
429 final String[] ary = new String[ list.size() ];
431 for( final String s : list ) {
437 public static String[][] file22dArray( final File file ) throws IOException {
438 final List<String> list = new ArrayList<String>();
439 final BufferedReader in = new BufferedReader( new FileReader( file ) );
441 while ( ( str = in.readLine() ) != null ) {
443 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
448 final String[][] ary = new String[ list.size() ][ 2 ];
449 final Pattern pa = Pattern.compile( "(\\S+)\\s+(\\S+)" );
451 for( final String s : list ) {
452 final Matcher m = pa.matcher( s );
454 ary[ i ][ 0 ] = m.group( 1 );
455 ary[ i ][ 1 ] = m.group( 2 );
459 throw new IOException( "unexpcted format: " + s );
465 final public static List<String> file2list( final File file ) throws IOException {
466 final List<String> list = new ArrayList<String>();
467 final BufferedReader in = new BufferedReader( new FileReader( file ) );
469 while ( ( str = in.readLine() ) != null ) {
471 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
472 for( final String s : splitString( str ) ) {
481 final public static SortedSet<String> file2set( final File file ) throws IOException {
482 final SortedSet<String> set = new TreeSet<String>();
483 final BufferedReader in = new BufferedReader( new FileReader( file ) );
485 while ( ( str = in.readLine() ) != null ) {
487 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
488 for( final String s : splitString( str ) ) {
497 final public static String getCurrentDateTime() {
498 final DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
499 return format.format( new Date() );
502 final public static String getFileSeparator() {
503 return ForesterUtil.FILE_SEPARATOR;
506 final public static String getFirstLine( final Object source ) throws FileNotFoundException, IOException {
507 BufferedReader reader = null;
508 if ( source instanceof File ) {
509 final File f = ( File ) source;
511 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
513 else if ( !f.isFile() ) {
514 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
516 else if ( !f.canRead() ) {
517 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
519 reader = new BufferedReader( new FileReader( f ) );
521 else if ( source instanceof InputStream ) {
522 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
524 else if ( source instanceof String ) {
525 reader = new BufferedReader( new StringReader( ( String ) source ) );
527 else if ( source instanceof StringBuffer ) {
528 reader = new BufferedReader( new StringReader( source.toString() ) );
530 else if ( source instanceof URL ) {
531 reader = new BufferedReader( new InputStreamReader( ( ( URL ) source ).openStream() ) );
534 throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
537 while ( ( line = reader.readLine() ) != null ) {
539 if ( !ForesterUtil.isEmpty( line ) ) {
540 if ( reader != null ) {
546 if ( reader != null ) {
552 final public static String getForesterLibraryInformation() {
553 return "forester " + ForesterConstants.FORESTER_VERSION + " (" + ForesterConstants.FORESTER_DATE + ")";
556 final public static String getLineSeparator() {
557 return ForesterUtil.LINE_SEPARATOR;
560 final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
561 if ( !counting_map.containsKey( item_name ) ) {
562 counting_map.put( item_name, 1 );
565 counting_map.put( item_name, counting_map.get( item_name ) + 1 );
569 final public static boolean isContainsParanthesesableNhCharacter( final String nh ) {
570 return PARANTHESESABLE_NH_CHARS_PATTERN.matcher( nh ).find();
573 final public static boolean isEmpty( final List<?> l ) {
574 if ( ( l == null ) || l.isEmpty() ) {
577 for( final Object o : l ) {
585 final public static boolean isEmpty( final Set<?> s ) {
586 if ( ( s == null ) || s.isEmpty() ) {
589 for( final Object o : s ) {
597 final public static boolean isEmpty( final String s ) {
598 return ( ( s == null ) || ( s.length() < 1 ) );
601 final public static boolean isEqual( final double a, final double b ) {
602 return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
605 final public static boolean isEven( final int n ) {
606 return ( n % 2 ) == 0;
610 * This determines whether String[] a and String[] b have at least one
611 * String in common (intersect). Returns false if at least one String[] is
615 * a String[] b a String[]
616 * @return true if both a and b or not empty or null and contain at least
617 * one element in common false otherwise
619 final public static boolean isIntersecting( final String[] a, final String[] b ) {
620 if ( ( a == null ) || ( b == null ) ) {
623 if ( ( a.length < 1 ) || ( b.length < 1 ) ) {
626 for( final String ai : a ) {
627 for( final String element : b ) {
628 if ( ( ai != null ) && ( element != null ) && ai.equals( element ) ) {
636 final public static double isLargerOrEqualToZero( final double d ) {
645 final public static boolean isNull( final BigDecimal s ) {
646 return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
649 final public static String isReadableFile( final File f ) {
651 return "file [" + f + "] does not exist";
653 if ( f.isDirectory() ) {
654 return "[" + f + "] is a directory";
657 return "[" + f + "] is not a file";
659 if ( !f.canRead() ) {
660 return "file [" + f + "] is not readable";
662 if ( f.length() < 1 ) {
663 return "file [" + f + "] is empty";
668 final public static String isReadableFile( final String s ) {
669 return isReadableFile( new File( s ) );
672 public final static boolean isWindows() {
674 return OS_NAME.toLowerCase().indexOf( "win" ) > -1;
676 catch ( final Exception e ) {
677 ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e );
682 public final static boolean isMac() {
684 return OS_NAME.toLowerCase().startsWith( "mac" );
686 catch ( final Exception e ) {
687 ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e );
692 final public static String isWritableFile( final File f ) {
693 if ( f.isDirectory() ) {
694 return "[" + f + "] is a directory";
697 return "[" + f + "] already exists";
703 * Helper for method "stringToColor".
705 * (Last modified: 12/20/03)
707 final public static int limitRangeForColor( int i ) {
717 final public static SortedMap<Object, Integer> listToSortedCountsMap( final List<?> list ) {
718 final SortedMap<Object, Integer> map = new TreeMap<Object, Integer>();
719 for( final Object key : list ) {
720 if ( !map.containsKey( key ) ) {
724 map.put( key, map.get( key ) + 1 );
730 final public static void map2file( final File file,
731 final Map<?, ?> data,
732 final String entry_separator,
733 final String data_separator ) throws IOException {
734 final Writer writer = new BufferedWriter( new FileWriter( file ) );
735 map2writer( writer, data, entry_separator, data_separator );
739 final public static void map2writer( final Writer writer,
740 final Map<?, ?> data,
741 final String entry_separator,
742 final String data_separator ) throws IOException {
743 boolean first = true;
744 for( final Entry<?, ?> entry : data.entrySet() ) {
746 writer.write( data_separator );
751 writer.write( entry.getKey().toString() );
752 writer.write( entry_separator );
753 writer.write( entry.getValue().toString() );
757 final public static StringBuffer mapToStringBuffer( final Map<Object, Object> map, final String key_value_separator ) {
758 final StringBuffer sb = new StringBuffer();
759 for( final Object key : map.keySet() ) {
760 sb.append( key.toString() );
761 sb.append( key_value_separator );
762 sb.append( map.get( key ).toString() );
763 sb.append( ForesterUtil.getLineSeparator() );
768 final public static String normalizeString( final String s,
770 final boolean left_pad,
771 final char pad_char ) {
772 if ( s.length() > length ) {
773 return s.substring( 0, length );
776 final StringBuffer pad = new StringBuffer( length - s.length() );
777 for( int i = 0; i < ( length - s.length() ); ++i ) {
778 pad.append( pad_char );
789 final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
790 BufferedReader reader = null;
791 if ( source instanceof File ) {
792 final File f = ( File ) source;
794 throw new IOException( "\"" + f.getAbsolutePath() + "\" does not exist" );
796 else if ( !f.isFile() ) {
797 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a file" );
799 else if ( !f.canRead() ) {
800 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a readable" );
802 reader = new BufferedReader( new FileReader( f ) );
804 else if ( source instanceof InputStream ) {
805 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
807 else if ( source instanceof String ) {
808 reader = new BufferedReader( new StringReader( ( String ) source ) );
810 else if ( source instanceof StringBuffer ) {
811 reader = new BufferedReader( new StringReader( source.toString() ) );
814 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
815 + "] (can only parse objects of type File, InputStream, String, or StringBuffer)" );
820 final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
821 return pad( new StringBuffer( number + "" ), size, pad, left_pad );
824 final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
825 return pad( new StringBuffer( string ), size, pad, left_pad );
828 final public static StringBuffer pad( final StringBuffer string,
831 final boolean left_pad ) {
832 final StringBuffer padding = new StringBuffer();
833 final int s = size - string.length();
835 return new StringBuffer( string.substring( 0, size ) );
837 for( int i = 0; i < s; ++i ) {
838 padding.append( pad );
841 return padding.append( string );
844 return string.append( padding );
848 final public static double parseDouble( final String str ) throws ParseException {
849 if ( ForesterUtil.isEmpty( str ) ) {
852 return Double.parseDouble( str );
855 final public static int parseInt( final String str ) throws ParseException {
856 if ( ForesterUtil.isEmpty( str ) ) {
859 return Integer.parseInt( str );
862 final public static void printArray( final Object[] a ) {
863 for( int i = 0; i < a.length; ++i ) {
864 System.out.println( "[" + i + "]=" + a[ i ] );
868 final public static void printCountingMap( final Map<String, Integer> counting_map ) {
869 for( final String key : counting_map.keySet() ) {
870 System.out.println( key + ": " + counting_map.get( key ) );
874 final public static void printErrorMessage( final String prg_name, final String message ) {
875 System.err.println( "[" + prg_name + "] > error: " + message );
878 final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
879 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
880 System.out.println();
881 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
882 for( int i = 0; i < l; ++i ) {
883 System.out.print( "_" );
885 System.out.println();
888 final public static void printProgramInformation( final String prg_name,
889 final String prg_version,
893 printProgramInformation( prg_name, null, prg_version, date, email, www, null );
896 final public static void printProgramInformation( final String prg_name,
898 final String prg_version,
902 final String based_on ) {
903 String my_prg_name = new String( prg_name );
904 if ( !ForesterUtil.isEmpty( desc ) ) {
905 my_prg_name += ( " - " + desc );
907 final int l = my_prg_name.length() + prg_version.length() + date.length() + 4;
908 System.out.println();
909 System.out.println( my_prg_name + " " + prg_version + " (" + date + ")" );
910 for( int i = 0; i < l; ++i ) {
911 System.out.print( "_" );
913 System.out.println();
914 System.out.println();
915 System.out.println( "WWW : " + www );
916 System.out.println( "Contact : " + email );
917 if ( !ForesterUtil.isEmpty( based_on ) ) {
918 System.out.println( "Based on: " + based_on );
920 if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
921 System.out.println();
922 System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
924 System.out.println();
927 final public static void printWarningMessage( final String prg_name, final String message ) {
928 System.out.println( "[" + prg_name + "] > warning: " + message );
931 final public static void programMessage( final String prg_name, final String message ) {
932 System.out.println( "[" + prg_name + "] > " + message );
935 final public static String removeSuffix( final String file_name ) {
936 final int i = file_name.lastIndexOf( '.' );
938 return file_name.substring( 0, i );
944 * Removes all white space from String s.
946 * @return String s with white space removed
948 final public static String removeWhiteSpace( String s ) {
950 for( i = 0; i <= ( s.length() - 1 ); i++ ) {
951 if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' )
952 || ( s.charAt( i ) == '\r' ) ) {
953 s = s.substring( 0, i ) + s.substring( i + 1 );
960 final public static String replaceIllegalNhCharacters( final String nh ) {
964 return nh.trim().replaceAll( "[\\[\\]:]+", "_" );
967 final public static String replaceIllegalNhxCharacters( final String nhx ) {
971 return nhx.trim().replaceAll( "[\\[\\](),:;\\s]+", "_" );
974 final public static double round( final double value, final int decimal_place ) {
975 BigDecimal bd = new BigDecimal( value );
976 bd = bd.setScale( decimal_place, BigDecimal.ROUND_HALF_UP );
977 return bd.doubleValue();
981 * Rounds d to an int.
983 final public static int roundToInt( final double d ) {
984 return ( int ) ( d + 0.5 );
987 final public static int roundToInt( final float f ) {
988 return ( int ) ( f + 0.5f );
991 final public static short roundToShort( final double d ) {
992 return ( short ) ( d + 0.5 );
995 final public static String sanitizeString( final String s ) {
1004 public static boolean seqIsLikelyToBeAa( final String s ) {
1005 final String seq = s.toLowerCase();
1006 if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 )
1007 || ( seq.indexOf( 'q' ) > -1 ) || ( seq.indexOf( 'h' ) > -1 ) || ( seq.indexOf( 'k' ) > -1 )
1008 || ( seq.indexOf( 'w' ) > -1 ) || ( seq.indexOf( 's' ) > -1 ) || ( seq.indexOf( 'm' ) > -1 )
1009 || ( seq.indexOf( 'p' ) > -1 ) || ( seq.indexOf( 'v' ) > -1 ) ) {
1015 final public static String stringArrayToString( final String[] a ) {
1016 return stringArrayToString( a, ", " );
1019 final public static String stringArrayToString( final String[] a, final String separator ) {
1020 final StringBuilder sb = new StringBuilder();
1021 if ( ( a != null ) && ( a.length > 0 ) ) {
1022 for( int i = 0; i < ( a.length - 1 ); ++i ) {
1023 sb.append( a[ i ] + separator );
1025 sb.append( a[ a.length - 1 ] );
1027 return sb.toString();
1030 final public static String[] stringListToArray( final List<String> list ) {
1031 if ( list != null ) {
1032 final String[] str = new String[ list.size() ];
1034 for( final String l : list ) {
1042 final public static String stringListToString( final List<String> l, final String separator ) {
1043 final StringBuilder sb = new StringBuilder();
1044 if ( ( l != null ) && ( l.size() > 0 ) ) {
1045 for( int i = 0; i < ( l.size() - 1 ); ++i ) {
1046 sb.append( l.get( i ) + separator );
1048 sb.append( l.get( l.size() - 1 ) );
1050 return sb.toString();
1053 final public static String[] stringSetToArray( final Set<String> strings ) {
1054 final String[] str_array = new String[ strings.size() ];
1056 for( final String e : strings ) {
1057 str_array[ i++ ] = e;
1062 final public static void unexpectedFatalError( final Exception e ) {
1063 System.err.println();
1064 System.err.println( "unexpected exception: should not have occured! Please contact program author(s)." );
1065 e.printStackTrace( System.err );
1066 System.err.println();
1070 final public static void unexpectedFatalError( final Error e ) {
1071 System.err.println();
1072 System.err.println( "unexpected error: should not have occured! Please contact program author(s)." );
1073 e.printStackTrace( System.err );
1074 System.err.println();
1078 final public static void unexpectedFatalError( final String message ) {
1079 System.err.println();
1080 System.err.println( "unexpected error: should not have occured! Please contact program author(s)." );
1081 System.err.println( message );
1082 System.err.println();
1086 final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
1087 System.err.println();
1088 System.err.println( "[" + prg_name
1089 + "] > unexpected error; should not have occured! Please contact program author(s)." );
1090 e.printStackTrace( System.err );
1091 System.err.println();
1095 final public static void unexpectedFatalError( final String prg_name, final String message ) {
1096 System.err.println();
1097 System.err.println( "[" + prg_name
1098 + "] > unexpected error: should not have occured! Please contact program author(s)." );
1099 System.err.println( message );
1100 System.err.println();
1104 final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
1105 System.err.println();
1106 System.err.println( "[" + prg_name
1107 + "] > unexpected error: should not have occured! Please contact program author(s)." );
1108 System.err.println( message );
1109 e.printStackTrace( System.err );
1110 System.err.println();
1114 public final static void updateProgress( final double progress_percentage ) {
1115 final int width = 50;
1116 System.out.print( "\r[" );
1118 for( ; i <= ForesterUtil.roundToInt( progress_percentage * width ); i++ ) {
1119 System.out.print( "." );
1121 for( ; i < width; i++ ) {
1122 System.out.print( " " );
1124 System.out.print( "]" );
1127 public final static void updateProgress( final int i, final DecimalFormat f ) {
1128 System.out.print( "\r[" + f.format( i ) + "]" );
1131 public final static String wordWrap( final String str, final int width ) {
1132 final StringBuilder sb = new StringBuilder( str );
1136 while ( i < sb.length() ) {
1137 if ( sb.charAt( i ) == ' ' ) {
1140 if ( sb.charAt( i ) == '\n' ) {
1144 if ( i > ( ( start + width ) - 1 ) ) {
1146 sb.setCharAt( ls, '\n' );
1151 sb.insert( i, '\n' );
1157 return sb.toString();
1161 * Helper method for calcColor methods.
1163 * @param smallercolor_component_x
1164 * color component the smaller color
1165 * @param largercolor_component_x
1166 * color component the larger color
1169 * @return an int representing a color component
1171 final private static int calculateColorComponent( final double smallercolor_component_x,
1172 final double largercolor_component_x,
1174 return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
1178 * Helper method for calcColor methods.
1186 * the smallest value
1187 * @return a normalized value between larger and smaller
1189 final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
1190 return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
1193 final private static String[] splitString( final String str ) {
1194 final String regex = "[\\s;,]+";
1195 return str.split( regex );
1198 public final static void outOfMemoryError( final OutOfMemoryError e ) {
1199 System.err.println();
1200 System.err.println( "Java memory allocation might be too small, try \"-Xmx2048m\" java command line option" );
1201 System.err.println();
1202 e.printStackTrace( System.err );
1203 System.err.println();
1207 public final static Color obtainColorDependingOnTaxonomyGroup( final String tax, final String tax_code ) {
1208 if ( tax.equalsIgnoreCase( "deuterostomia" ) ) {
1209 printRel( tax_code, "deuterostomia" );
1210 return TaxonomyColors.DEUTEROSTOMIA_COLOR;
1212 else if ( tax.equalsIgnoreCase( "protostomia" ) ) {
1213 printRel( tax_code, "protostomia" );
1214 return TaxonomyColors.PROTOSTOMIA_COLOR;
1216 else if ( tax.equalsIgnoreCase( "cnidaria" ) ) {
1217 printRel( tax_code, "cnidaria" );
1218 return TaxonomyColors.CNIDARIA_COLOR;
1220 else if ( tax.toLowerCase().startsWith( "trichoplax" ) || tax.equalsIgnoreCase( "placozoa" ) ) {
1221 printRel( tax_code, "placozoa" );
1222 return TaxonomyColors.PLACOZOA_COLOR;
1224 else if ( tax.toLowerCase().startsWith( "mnemiopsis" ) || tax.equalsIgnoreCase( "ctenophora" ) ) {
1225 printRel( tax_code, "ctenophora" );
1226 return TaxonomyColors.CTENOPHORA_COLOR;
1228 else if ( tax.toLowerCase().startsWith( "amphimedon" ) || tax.equalsIgnoreCase( "porifera" ) ) {
1229 printRel( tax_code, "porifera" );
1230 return TaxonomyColors.PORIFERA_COLOR;
1232 else if ( tax.equalsIgnoreCase( "codonosigidae" ) || tax.equalsIgnoreCase( "choanoflagellida" ) ) {
1233 printRel( tax_code, "choanoflagellida" );
1234 return TaxonomyColors.CHOANOFLAGELLIDA;
1236 else if ( tax.toLowerCase().startsWith( "ichthyophonida & filasterea" )
1237 || tax.toLowerCase().startsWith( "ichthyophonida and filasterea" )
1238 || tax.toLowerCase().startsWith( "ichthyosporea & filasterea" )
1239 || tax.toLowerCase().startsWith( "ichthyosporea and filasterea" ) ) {
1240 printRel( tax_code, "ichthyophonida & filasterea" );
1241 return TaxonomyColors.ICHTHYOSPOREA_AND_FILASTEREA;
1243 else if ( tax.equalsIgnoreCase( "dikarya" ) ) {
1244 printRel( tax_code, "dikarya" );
1245 return TaxonomyColors.DIKARYA_COLOR;
1247 else if ( tax.equalsIgnoreCase( "fungi" ) || tax.equalsIgnoreCase( "other fungi" ) ) {
1248 printRel( tax_code, "other fungi" );
1249 return TaxonomyColors.OTHER_FUNGI_COLOR;
1251 else if ( tax.toLowerCase().startsWith( "nucleariidae and fonticula" ) ) {
1252 printRel( tax_code, "nucleariidae and fonticula group" );
1253 return TaxonomyColors.NUCLEARIIDAE_AND_FONTICULA_GROUP_COLOR;
1255 else if ( tax.equalsIgnoreCase( "amoebozoa" ) ) {
1256 printRel( tax_code, "amoebozoa" );
1257 return TaxonomyColors.AMOEBOZOA_COLOR;
1259 else if ( tax.equalsIgnoreCase( "embryophyta" ) ) {
1260 printRel( tax_code, "embryophyta" );
1261 return TaxonomyColors.EMBRYOPHYTA_COLOR;
1263 else if ( tax.equalsIgnoreCase( "chlorophyta" ) ) {
1264 printRel( tax_code, "chlorophyta" );
1265 return TaxonomyColors.CHLOROPHYTA_COLOR;
1267 else if ( tax.equalsIgnoreCase( "rhodophyta" ) ) {
1268 printRel( tax_code, "rhodophyta" );
1269 return TaxonomyColors.RHODOPHYTA_COLOR;
1271 else if ( tax.toLowerCase().startsWith( "hacrobia" ) ) {
1272 printRel( tax_code, "hacrobia" );
1273 return TaxonomyColors.HACROBIA_COLOR;
1275 else if ( tax.equalsIgnoreCase( "glaucocystophyceae" ) || tax.equalsIgnoreCase( "glaucophyta" ) ) {
1276 printRel( tax_code, "glaucocystophyceae" );
1277 return TaxonomyColors.GLAUCOPHYTA_COLOR;
1279 else if ( tax.equalsIgnoreCase( "stramenopiles" ) ) {
1280 printRel( tax_code, "stramenopiles" );
1281 return TaxonomyColors.STRAMENOPILES_COLOR;
1283 else if ( tax.equalsIgnoreCase( "alveolata" ) ) {
1284 printRel( tax_code, "alveolata" );
1285 return TaxonomyColors.ALVEOLATA_COLOR;
1287 else if ( tax.equalsIgnoreCase( "rhizaria" ) ) {
1288 printRel( tax_code, "rhizaria" );
1289 return TaxonomyColors.RHIZARIA_COLOR;
1291 else if ( tax.equalsIgnoreCase( "excavata" ) ) {
1292 printRel( tax_code, "excavata" );
1293 return TaxonomyColors.EXCAVATA_COLOR;
1295 else if ( tax.equalsIgnoreCase( "apusozoa" ) ) {
1296 printRel( tax_code, "apusozoa" );
1297 return TaxonomyColors.APUSOZOA_COLOR;
1299 else if ( tax.equalsIgnoreCase( "archaea" ) ) {
1300 printRel( tax_code, "archaea" );
1301 return TaxonomyColors.ARCHAEA_COLOR;
1303 else if ( tax.equalsIgnoreCase( "bacteria" ) ) {
1304 printRel( tax_code, "bacteria" );
1305 return TaxonomyColors.BACTERIA_COLOR;
1310 private final static void printRel( final String tax_code, final String group ) {
1311 //System.out.println( tax_code + "->" + group );
1312 //System.out.println( "_default_taxcode_taxgroup_map.put( \"" + tax_code + "\", \"" + group + "\" );" );