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.net.URLConnection;
45 import java.text.DateFormat;
46 import java.text.DecimalFormat;
47 import java.text.DecimalFormatSymbols;
48 import java.text.NumberFormat;
49 import java.text.ParseException;
50 import java.text.SimpleDateFormat;
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Date;
54 import java.util.List;
56 import java.util.Map.Entry;
58 import java.util.SortedMap;
59 import java.util.SortedSet;
60 import java.util.TreeMap;
61 import java.util.TreeSet;
62 import java.util.regex.Matcher;
63 import java.util.regex.Pattern;
65 import org.forester.archaeopteryx.Constants;
66 import org.forester.phylogeny.PhylogenyNode;
67 import org.forester.phylogeny.data.Distribution;
68 import org.forester.phylogeny.data.Sequence;
69 import org.forester.phylogeny.data.Taxonomy;
70 import org.forester.protein.BasicProtein;
71 import org.forester.protein.Domain;
72 import org.forester.protein.Protein;
73 import org.forester.sequence.MolecularSequence;
74 import org.forester.sequence.MolecularSequence.TYPE;
75 import org.forester.surfacing.SurfacingUtil;
77 public final class ForesterUtil {
79 public final static String FILE_SEPARATOR = System.getProperty( "file.separator" );
80 public static final NumberFormat FORMATTER_06;
81 public static final NumberFormat FORMATTER_3;
82 public static final NumberFormat FORMATTER_6;
83 public static final NumberFormat FORMATTER_9;
84 public final static String JAVA_VENDOR = System.getProperty( "java.vendor" );
85 public final static String JAVA_VERSION = System.getProperty( "java.version" );
86 public final static String LINE_SEPARATOR = System.getProperty( "line.separator" );
87 public static final String NCBI_GI = "http://www.ncbi.nlm.nih.gov/protein/gi:";
88 public static final String NCBI_NUCCORE = "http://www.ncbi.nlm.nih.gov/nuccore/";
89 public static final String NCBI_PROTEIN = "http://www.ncbi.nlm.nih.gov/protein/";
90 public static final BigDecimal NULL_BD = new BigDecimal( 0 );
91 public final static String OS_ARCH = System.getProperty( "os.arch" );
92 public final static String OS_NAME = System.getProperty( "os.name" );
93 public final static String OS_VERSION = System.getProperty( "os.version" );
94 public static final String PDB = "http://www.pdb.org/pdb/explore/explore.do?pdbId=";
95 public final static String UNIPROT_KB = "http://www.uniprot.org/uniprot/";
96 public final static double ZERO_DIFF = 1.0E-9;
97 private static final Pattern PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s:\\[\\]]" );
99 final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
100 dfs.setDecimalSeparator( '.' );
101 // dfs.setGroupingSeparator( ( char ) 0 );
102 FORMATTER_9 = new DecimalFormat( "#.#########", dfs );
103 FORMATTER_6 = new DecimalFormat( "#.######", dfs );
104 FORMATTER_06 = new DecimalFormat( "0.######", dfs );
105 FORMATTER_3 = new DecimalFormat( "#.###", dfs );
108 final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) {
109 if ( sb.length() > 0 ) {
110 sb.append( separator );
115 * This calculates a color. If value is equal to min the returned color is
116 * minColor, if value is equal to max the returned color is maxColor,
117 * otherwise a color 'proportional' to value is returned.
131 final public static Color calcColor( double value,
134 final Color minColor,
135 final Color maxColor ) {
142 final double x = ForesterUtil.calculateColorFactor( value, max, min );
143 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), maxColor.getRed(), x );
144 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), maxColor.getGreen(), x );
145 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), maxColor.getBlue(), x );
146 return new Color( red, green, blue );
150 * This calculates a color. If value is equal to min the returned color is
151 * minColor, if value is equal to max the returned color is maxColor, if
152 * value is equal to mean the returned color is meanColor, otherwise a color
153 * 'proportional' to value is returned -- either between min-mean or
163 * the mean/median value
172 final public static Color calcColor( double value,
176 final Color minColor,
177 final Color maxColor,
178 final Color meanColor ) {
185 if ( value < mean ) {
186 final double x = ForesterUtil.calculateColorFactor( value, mean, min );
187 final int red = ForesterUtil.calculateColorComponent( minColor.getRed(), meanColor.getRed(), x );
188 final int green = ForesterUtil.calculateColorComponent( minColor.getGreen(), meanColor.getGreen(), x );
189 final int blue = ForesterUtil.calculateColorComponent( minColor.getBlue(), meanColor.getBlue(), x );
190 return new Color( red, green, blue );
192 else if ( value > mean ) {
193 final double x = ForesterUtil.calculateColorFactor( value, max, mean );
194 final int red = ForesterUtil.calculateColorComponent( meanColor.getRed(), maxColor.getRed(), x );
195 final int green = ForesterUtil.calculateColorComponent( meanColor.getGreen(), maxColor.getGreen(), x );
196 final int blue = ForesterUtil.calculateColorComponent( meanColor.getBlue(), maxColor.getBlue(), x );
197 return new Color( red, green, blue );
205 * Helper method for calcColor methods.
207 * @param smallercolor_component_x
208 * color component the smaller color
209 * @param largercolor_component_x
210 * color component the larger color
213 * @return an int representing a color component
215 final private static int calculateColorComponent( final double smallercolor_component_x,
216 final double largercolor_component_x,
218 return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) );
222 * Helper method for calcColor methods.
231 * @return a normalized value between larger and smaller
233 final private static double calculateColorFactor( final double value, final double larger, final double smaller ) {
234 return ( 255.0 * ( value - smaller ) ) / ( larger - smaller );
237 public static int calculateOverlap( final Domain domain, final List<Boolean> covered_positions ) {
238 int overlap_count = 0;
239 for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
240 if ( ( i < covered_positions.size() ) && ( covered_positions.get( i ) == true ) ) {
244 return overlap_count;
247 final public static String collapseWhiteSpace( final String s ) {
248 return s.replaceAll( "[\\s]+", " " );
251 final public static void collection2file( final File file, final Collection<?> data, final String separator )
253 final Writer writer = new BufferedWriter( new FileWriter( file ) );
254 collection2writer( writer, data, separator );
258 final public static void collection2writer( final Writer writer, final Collection<?> data, final String separator )
260 boolean first = true;
261 for( final Object object : data ) {
263 writer.write( separator );
268 writer.write( object.toString() );
272 final public static String colorToHex( final Color color ) {
273 final String rgb = Integer.toHexString( color.getRGB() );
274 return rgb.substring( 2, rgb.length() );
277 synchronized public static void copyFile( final File in, final File out ) throws IOException {
278 final FileInputStream in_s = new FileInputStream( in );
279 final FileOutputStream out_s = new FileOutputStream( out );
281 final byte[] buf = new byte[ 1024 ];
283 while ( ( i = in_s.read( buf ) ) != -1 ) {
284 out_s.write( buf, 0, i );
287 catch ( final IOException e ) {
291 if ( in_s != null ) {
294 if ( out_s != null ) {
300 final public static int countChars( final String str, final char c ) {
302 for( int i = 0; i < str.length(); ++i ) {
303 if ( str.charAt( i ) == c ) {
310 final public static BufferedWriter createBufferedWriter( final File file ) throws IOException {
311 if ( file.exists() ) {
312 throw new IOException( "[" + file + "] already exists" );
314 return new BufferedWriter( new FileWriter( file ) );
317 final public static BufferedWriter createBufferedWriter( final String name ) throws IOException {
318 return new BufferedWriter( new FileWriter( createFileForWriting( name ) ) );
321 final public static EasyWriter createEasyWriter( final File file ) throws IOException {
322 return new EasyWriter( createBufferedWriter( file ) );
325 final public static BufferedWriter createEasyWriter( final String name ) throws IOException {
326 return createEasyWriter( createFileForWriting( name ) );
329 final public static File createFileForWriting( final String name ) throws IOException {
330 final File file = new File( name );
331 if ( file.exists() ) {
332 throw new IOException( "[" + name + "] already exists" );
337 final public static void ensurePresenceOfDate( final PhylogenyNode node ) {
338 if ( !node.getNodeData().isHasDate() ) {
339 node.getNodeData().setDate( new org.forester.phylogeny.data.Date() );
343 final public static void ensurePresenceOfDistribution( final PhylogenyNode node ) {
344 if ( !node.getNodeData().isHasDistribution() ) {
345 node.getNodeData().setDistribution( new Distribution( "" ) );
349 public static void ensurePresenceOfSequence( final PhylogenyNode node ) {
350 if ( !node.getNodeData().isHasSequence() ) {
351 node.getNodeData().setSequence( new Sequence() );
355 public static void ensurePresenceOfTaxonomy( final PhylogenyNode node ) {
356 if ( !node.getNodeData().isHasTaxonomy() ) {
357 node.getNodeData().setTaxonomy( new Taxonomy() );
361 public static void fatalError( final String message ) {
362 System.err.println();
363 System.err.println( "error: " + message );
364 System.err.println();
368 public static void fatalError( final String prg_name, final String message ) {
369 System.err.println();
370 System.err.println( "[" + prg_name + "] > " + message );
371 System.err.println();
375 public static void fatalErrorIfFileNotReadable( final File file ) {
376 final String error = isReadableFile( file );
377 if ( !isEmpty( error ) ) {
378 System.err.println();
379 System.err.println( "error: " + error );
380 System.err.println();
385 public static void fatalErrorIfFileNotReadable( final String prg_name, final File file ) {
386 final String error = isReadableFile( file );
387 if ( !isEmpty( error ) ) {
388 System.err.println();
389 System.err.println( "[" + prg_name + "] > " + error );
390 System.err.println();
395 public static String[][] file22dArray( final File file ) throws IOException {
396 final List<String> list = new ArrayList<String>();
397 final BufferedReader in = new BufferedReader( new FileReader( file ) );
399 while ( ( str = in.readLine() ) != null ) {
401 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
406 final String[][] ary = new String[ list.size() ][ 2 ];
407 final Pattern pa = Pattern.compile( "(\\S+)\\s+(\\S+)" );
409 for( final String s : list ) {
410 final Matcher m = pa.matcher( s );
412 ary[ i ][ 0 ] = m.group( 1 );
413 ary[ i ][ 1 ] = m.group( 2 );
417 throw new IOException( "unexpcted format: " + s );
423 public static String[] file2array( final File file ) throws IOException {
424 final List<String> list = file2list( file );
425 final String[] ary = new String[ list.size() ];
427 for( final String s : list ) {
433 final public static List<String> file2list( final File file ) throws IOException {
434 final List<String> list = new ArrayList<String>();
435 final BufferedReader in = new BufferedReader( new FileReader( file ) );
437 while ( ( str = in.readLine() ) != null ) {
439 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
440 for( final String s : splitString( str ) ) {
449 final public static SortedSet<String> file2set( final File file ) throws IOException {
450 final SortedSet<String> set = new TreeSet<String>();
451 final BufferedReader in = new BufferedReader( new FileReader( file ) );
453 while ( ( str = in.readLine() ) != null ) {
455 if ( ( str.length() > 0 ) && !str.startsWith( "#" ) ) {
456 for( final String s : splitString( str ) ) {
465 final public static String getCurrentDateTime() {
466 final DateFormat format = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
467 return format.format( new Date() );
470 final public static String getFileSeparator() {
471 return ForesterUtil.FILE_SEPARATOR;
474 final public static String getFirstLine( final Object source ) throws FileNotFoundException, IOException {
475 BufferedReader reader = null;
476 if ( source instanceof File ) {
477 final File f = ( File ) source;
479 throw new IOException( "[" + f.getAbsolutePath() + "] does not exist" );
481 else if ( !f.isFile() ) {
482 throw new IOException( "[" + f.getAbsolutePath() + "] is not a file" );
484 else if ( !f.canRead() ) {
485 throw new IOException( "[" + f.getAbsolutePath() + "] is not a readable" );
487 reader = new BufferedReader( new FileReader( f ) );
489 else if ( source instanceof InputStream ) {
490 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
492 else if ( source instanceof String ) {
493 reader = new BufferedReader( new StringReader( ( String ) source ) );
495 else if ( source instanceof StringBuffer ) {
496 reader = new BufferedReader( new StringReader( source.toString() ) );
498 else if ( source instanceof URL ) {
499 final URLConnection url_connection = ( ( URL ) source ).openConnection();
500 url_connection.setDefaultUseCaches( false );
501 reader = new BufferedReader( new InputStreamReader( url_connection.getInputStream() ) );
504 throw new IllegalArgumentException( "dont know how to read [" + source.getClass() + "]" );
507 while ( ( line = reader.readLine() ) != null ) {
509 if ( !ForesterUtil.isEmpty( line ) ) {
510 if ( reader != null ) {
516 if ( reader != null ) {
522 final public static String getForesterLibraryInformation() {
523 return "forester " + ForesterConstants.FORESTER_VERSION + " (" + ForesterConstants.FORESTER_DATE + ")";
526 final public static String getLineSeparator() {
527 return ForesterUtil.LINE_SEPARATOR;
530 final public static MolecularSequence.TYPE guessMolecularSequenceType( final String mol_seq ) {
531 if ( mol_seq.contains( "L" ) || mol_seq.contains( "I" ) || mol_seq.contains( "E" ) || mol_seq.contains( "H" )
532 || mol_seq.contains( "D" ) || mol_seq.contains( "Q" ) ) {
536 if ( mol_seq.contains( "T" ) ) {
539 else if ( mol_seq.contains( "U" ) ) {
546 final public static void increaseCountingMap( final Map<String, Integer> counting_map, final String item_name ) {
547 if ( !counting_map.containsKey( item_name ) ) {
548 counting_map.put( item_name, 1 );
551 counting_map.put( item_name, counting_map.get( item_name ) + 1 );
555 final public static boolean isEmpty( final List<?> l ) {
556 if ( ( l == null ) || l.isEmpty() ) {
559 for( final Object o : l ) {
567 final public static boolean isEmpty( final Set<?> s ) {
568 if ( ( s == null ) || s.isEmpty() ) {
571 for( final Object o : s ) {
579 final public static boolean isEmpty( final String s ) {
580 return ( ( s == null ) || ( s.length() < 1 ) );
584 * Returns true is Domain domain falls in an uninterrupted stretch of
588 * @param covered_positions
591 public static boolean isEngulfed( final Domain domain, final List<Boolean> covered_positions ) {
592 for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
593 if ( ( i >= covered_positions.size() ) || ( covered_positions.get( i ) != true ) ) {
600 final public static boolean isEqual( final double a, final double b ) {
601 return ( ( Math.abs( a - b ) ) < ZERO_DIFF );
604 final public static boolean isEven( final int n ) {
605 return ( n % 2 ) == 0;
609 * This determines whether String[] a and String[] b have at least one
610 * String in common (intersect). Returns false if at least one String[] is
614 * a String[] b a String[]
615 * @return true if both a and b or not empty or null and contain at least
616 * one element in common false otherwise
618 final public static boolean isIntersecting( final String[] a, final String[] b ) {
619 if ( ( a == null ) || ( b == null ) ) {
622 if ( ( a.length < 1 ) || ( b.length < 1 ) ) {
625 for( final String ai : a ) {
626 for( final String element : b ) {
627 if ( ( ai != null ) && ( element != null ) && ai.equals( element ) ) {
635 final public static double isLargerOrEqualToZero( final double d ) {
644 public final static boolean isMac() {
646 return OS_NAME.toLowerCase().startsWith( "mac" );
648 catch ( final Exception e ) {
649 ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e );
654 final public static boolean isNull( final BigDecimal s ) {
655 return ( ( s == null ) || ( s.compareTo( NULL_BD ) == 0 ) );
658 final public static String isReadableFile( final File f ) {
660 return "file [" + f + "] does not exist";
662 if ( f.isDirectory() ) {
663 return "[" + f + "] is a directory";
666 return "[" + f + "] is not a file";
668 if ( !f.canRead() ) {
669 return "file [" + f + "] is not readable";
671 if ( f.length() < 1 ) {
672 return "file [" + f + "] is empty";
677 final public static String isReadableFile( final String s ) {
678 return isReadableFile( new File( s ) );
681 public final static boolean isWindows() {
683 return OS_NAME.toLowerCase().indexOf( "win" ) > -1;
685 catch ( final Exception e ) {
686 ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e );
691 final public static String isWritableFile( final File f ) {
692 if ( f.isDirectory() ) {
693 return "[" + f + "] is a directory";
696 return "[" + f + "] already exists";
702 * Helper for method "stringToColor".
704 * (Last modified: 12/20/03)
706 final public static int limitRangeForColor( int i ) {
716 final public static SortedMap<Object, Integer> listToSortedCountsMap( final List<?> list ) {
717 final SortedMap<Object, Integer> map = new TreeMap<Object, Integer>();
718 for( final Object key : list ) {
719 if ( !map.containsKey( key ) ) {
723 map.put( key, map.get( key ) + 1 );
729 final public static void map2file( final File file,
730 final Map<?, ?> data,
731 final String entry_separator,
732 final String data_separator ) throws IOException {
733 final Writer writer = new BufferedWriter( new FileWriter( file ) );
734 map2writer( writer, data, entry_separator, data_separator );
738 final public static void map2writer( final Writer writer,
739 final Map<?, ?> data,
740 final String entry_separator,
741 final String data_separator ) throws IOException {
742 boolean first = true;
743 for( final Entry<?, ?> entry : data.entrySet() ) {
745 writer.write( data_separator );
750 writer.write( entry.getKey().toString() );
751 writer.write( entry_separator );
752 writer.write( entry.getValue().toString() );
756 final public static StringBuffer mapToStringBuffer( final Map<Object, Object> map, final String key_value_separator ) {
757 final StringBuffer sb = new StringBuffer();
758 for( final Object key : map.keySet() ) {
759 sb.append( key.toString() );
760 sb.append( key_value_separator );
761 sb.append( map.get( key ).toString() );
762 sb.append( ForesterUtil.getLineSeparator() );
767 final public static String normalizeString( final String s,
769 final boolean left_pad,
770 final char pad_char ) {
771 if ( s.length() > length ) {
772 return s.substring( 0, length );
775 final StringBuffer pad = new StringBuffer( length - s.length() );
776 for( int i = 0; i < ( length - s.length() ); ++i ) {
777 pad.append( pad_char );
788 public final static Color obtainColorDependingOnTaxonomyGroup( final String tax_group ) {
789 if ( !ForesterUtil.isEmpty( tax_group ) ) {
790 if ( tax_group.equals( TaxonomyGroups.DEUTEROSTOMIA ) ) {
791 return TaxonomyColors.DEUTEROSTOMIA_COLOR;
793 else if ( tax_group.equals( TaxonomyGroups.PROTOSTOMIA ) ) {
794 return TaxonomyColors.PROTOSTOMIA_COLOR;
796 else if ( tax_group.equals( TaxonomyGroups.CNIDARIA ) ) {
797 return TaxonomyColors.CNIDARIA_COLOR;
799 else if ( tax_group.equals( TaxonomyGroups.PLACOZOA ) ) {
800 return TaxonomyColors.PLACOZOA_COLOR;
802 else if ( tax_group.equals( TaxonomyGroups.CTENOPHORA ) ) {
803 return TaxonomyColors.CTENOPHORA_COLOR;
805 else if ( tax_group.equals( TaxonomyGroups.PORIFERA ) ) {
806 return TaxonomyColors.PORIFERA_COLOR;
808 else if ( tax_group.equals( TaxonomyGroups.CHOANOFLAGELLIDA ) ) {
809 return TaxonomyColors.CHOANOFLAGELLIDA;
811 else if ( tax_group.equals( TaxonomyGroups.ICHTHYOPHONIDA_FILASTEREA ) ) {
812 return TaxonomyColors.ICHTHYOSPOREA_AND_FILASTEREA;
814 else if ( tax_group.equals( TaxonomyGroups.DIKARYA ) ) {
815 return TaxonomyColors.DIKARYA_COLOR;
817 else if ( tax_group.equalsIgnoreCase( TaxonomyGroups.FUNGI )
818 || tax_group.equalsIgnoreCase( TaxonomyGroups.OTHER_FUNGI ) ) {
819 return TaxonomyColors.OTHER_FUNGI_COLOR;
821 else if ( tax_group.equals( TaxonomyGroups.NUCLEARIIDAE_AND_FONTICULA_GROUP ) ) {
822 return TaxonomyColors.NUCLEARIIDAE_AND_FONTICULA_GROUP_COLOR;
824 else if ( tax_group.equals( TaxonomyGroups.AMOEBOZOA ) ) {
825 return TaxonomyColors.AMOEBOZOA_COLOR;
827 else if ( tax_group.equals( TaxonomyGroups.EMBRYOPHYTA ) ) {
828 return TaxonomyColors.EMBRYOPHYTA_COLOR;
830 else if ( tax_group.equals( TaxonomyGroups.CHLOROPHYTA ) ) {
831 return TaxonomyColors.CHLOROPHYTA_COLOR;
833 else if ( tax_group.equals( TaxonomyGroups.RHODOPHYTA ) ) {
834 return TaxonomyColors.RHODOPHYTA_COLOR;
836 else if ( tax_group.equals( TaxonomyGroups.HACROBIA ) ) {
837 return TaxonomyColors.HACROBIA_COLOR;
839 else if ( tax_group.equals( TaxonomyGroups.GLAUCOCYSTOPHYCEAE ) ) {
840 return TaxonomyColors.GLAUCOPHYTA_COLOR;
842 else if ( tax_group.equals( TaxonomyGroups.STRAMENOPILES ) ) {
843 return TaxonomyColors.STRAMENOPILES_COLOR;
845 else if ( tax_group.equals( TaxonomyGroups.ALVEOLATA ) ) {
846 return TaxonomyColors.ALVEOLATA_COLOR;
848 else if ( tax_group.equals( TaxonomyGroups.RHIZARIA ) ) {
849 return TaxonomyColors.RHIZARIA_COLOR;
851 else if ( tax_group.equals( TaxonomyGroups.EXCAVATA ) ) {
852 return TaxonomyColors.EXCAVATA_COLOR;
854 else if ( tax_group.equals( TaxonomyGroups.APUSOZOA ) ) {
855 return TaxonomyColors.APUSOZOA_COLOR;
857 else if ( tax_group.equals( TaxonomyGroups.ARCHAEA ) ) {
858 return TaxonomyColors.ARCHAEA_COLOR;
860 else if ( tax_group.equals( TaxonomyGroups.BACTERIA ) ) {
861 return TaxonomyColors.BACTERIA_COLOR;
867 public final static String obtainNormalizedTaxonomyGroup( final String tax ) {
868 if ( tax.equalsIgnoreCase( TaxonomyGroups.DEUTEROSTOMIA ) ) {
869 return TaxonomyGroups.DEUTEROSTOMIA;
871 else if ( tax.equalsIgnoreCase( TaxonomyGroups.PROTOSTOMIA ) ) {
872 return TaxonomyGroups.PROTOSTOMIA;
874 else if ( tax.equalsIgnoreCase( TaxonomyGroups.CNIDARIA ) ) {
875 return TaxonomyGroups.CNIDARIA;
877 else if ( tax.toLowerCase().startsWith( "trichoplax" ) || tax.equalsIgnoreCase( TaxonomyGroups.PLACOZOA ) ) {
878 return TaxonomyGroups.PLACOZOA;
880 else if ( tax.toLowerCase().startsWith( "mnemiopsis" ) || tax.equalsIgnoreCase( TaxonomyGroups.CTENOPHORA ) ) {
881 return TaxonomyGroups.CTENOPHORA;
883 else if ( tax.toLowerCase().startsWith( "amphimedon" ) || tax.equalsIgnoreCase( TaxonomyGroups.PORIFERA ) ) {
884 return TaxonomyGroups.PORIFERA;
886 else if ( tax.equalsIgnoreCase( "codonosigidae" ) || tax.equalsIgnoreCase( TaxonomyGroups.CHOANOFLAGELLIDA ) ) {
887 return TaxonomyGroups.CHOANOFLAGELLIDA;
889 else if ( tax.toLowerCase().startsWith( TaxonomyGroups.ICHTHYOPHONIDA_FILASTEREA )
890 || tax.toLowerCase().startsWith( "ichthyophonida and filasterea" )
891 || tax.toLowerCase().startsWith( "ichthyosporea & filasterea" )
892 || tax.toLowerCase().startsWith( "ichthyosporea and filasterea" ) ) {
893 return TaxonomyGroups.ICHTHYOPHONIDA_FILASTEREA;
895 else if ( tax.equalsIgnoreCase( TaxonomyGroups.DIKARYA ) ) {
896 return TaxonomyGroups.DIKARYA;
898 else if ( tax.equalsIgnoreCase( TaxonomyGroups.FUNGI ) || tax.equalsIgnoreCase( TaxonomyGroups.OTHER_FUNGI ) ) {
899 return TaxonomyGroups.OTHER_FUNGI;
901 else if ( tax.toLowerCase().startsWith( "nucleariidae and fonticula" ) ) {
902 return TaxonomyGroups.NUCLEARIIDAE_AND_FONTICULA_GROUP;
904 else if ( tax.equalsIgnoreCase( TaxonomyGroups.AMOEBOZOA ) ) {
905 return TaxonomyGroups.AMOEBOZOA;
907 else if ( tax.equalsIgnoreCase( TaxonomyGroups.EMBRYOPHYTA ) ) {
908 return TaxonomyGroups.EMBRYOPHYTA;
910 else if ( tax.equalsIgnoreCase( TaxonomyGroups.CHLOROPHYTA ) ) {
911 return TaxonomyGroups.CHLOROPHYTA;
913 else if ( tax.equalsIgnoreCase( TaxonomyGroups.RHODOPHYTA ) ) {
914 return TaxonomyGroups.RHODOPHYTA;
916 else if ( tax.toLowerCase().startsWith( TaxonomyGroups.HACROBIA ) ) {
917 return TaxonomyGroups.HACROBIA;
919 else if ( tax.equalsIgnoreCase( TaxonomyGroups.GLAUCOCYSTOPHYCEAE ) || tax.equalsIgnoreCase( "glaucophyta" ) ) {
920 return TaxonomyGroups.GLAUCOCYSTOPHYCEAE;
922 else if ( tax.equalsIgnoreCase( TaxonomyGroups.STRAMENOPILES ) ) {
923 return TaxonomyGroups.STRAMENOPILES;
925 else if ( tax.equalsIgnoreCase( TaxonomyGroups.ALVEOLATA ) ) {
926 return TaxonomyGroups.ALVEOLATA;
928 else if ( tax.equalsIgnoreCase( TaxonomyGroups.RHIZARIA ) ) {
929 return TaxonomyGroups.RHIZARIA;
931 else if ( tax.equalsIgnoreCase( TaxonomyGroups.EXCAVATA ) ) {
932 return TaxonomyGroups.EXCAVATA;
934 else if ( tax.equalsIgnoreCase( TaxonomyGroups.APUSOZOA ) ) {
935 return TaxonomyGroups.APUSOZOA;
937 else if ( tax.equalsIgnoreCase( TaxonomyGroups.ARCHAEA ) ) {
938 return TaxonomyGroups.ARCHAEA;
940 else if ( tax.equalsIgnoreCase( TaxonomyGroups.BACTERIA ) ) {
941 return TaxonomyGroups.BACTERIA;
946 final public static BufferedReader obtainReader( final Object source ) throws IOException, FileNotFoundException {
947 BufferedReader reader = null;
948 if ( source instanceof File ) {
949 final File f = ( File ) source;
951 throw new IOException( "\"" + f.getAbsolutePath() + "\" does not exist" );
953 else if ( !f.isFile() ) {
954 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a file" );
956 else if ( !f.canRead() ) {
957 throw new IOException( "\"" + f.getAbsolutePath() + "\" is not a readable" );
959 reader = new BufferedReader( new FileReader( f ) );
961 else if ( source instanceof InputStream ) {
962 reader = new BufferedReader( new InputStreamReader( ( InputStream ) source ) );
964 else if ( source instanceof String ) {
965 reader = new BufferedReader( new StringReader( ( String ) source ) );
967 else if ( source instanceof StringBuffer ) {
968 reader = new BufferedReader( new StringReader( source.toString() ) );
971 throw new IllegalArgumentException( "attempt to parse object of type [" + source.getClass()
972 + "] (can only parse objects of type File, InputStream, String, or StringBuffer)" );
977 public final static void outOfMemoryError( final OutOfMemoryError e ) {
978 System.err.println();
979 System.err.println( "Java memory allocation might be too small, try \"-Xmx2048m\" java command line option" );
980 System.err.println();
981 e.printStackTrace( System.err );
982 System.err.println();
986 final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) {
987 return pad( new StringBuffer( number + "" ), size, pad, left_pad );
990 final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) {
991 return pad( new StringBuffer( string ), size, pad, left_pad );
994 final public static StringBuffer pad( final StringBuffer string,
997 final boolean left_pad ) {
998 final StringBuffer padding = new StringBuffer();
999 final int s = size - string.length();
1001 return new StringBuffer( string.substring( 0, size ) );
1003 for( int i = 0; i < s; ++i ) {
1004 padding.append( pad );
1007 return padding.append( string );
1010 return string.append( padding );
1014 final public static double parseDouble( final String str ) throws ParseException {
1015 if ( ForesterUtil.isEmpty( str ) ) {
1018 return Double.parseDouble( str );
1021 final public static int parseInt( final String str ) throws ParseException {
1022 if ( ForesterUtil.isEmpty( str ) ) {
1025 return Integer.parseInt( str );
1028 final public static void printArray( final Object[] a ) {
1029 for( int i = 0; i < a.length; ++i ) {
1030 System.out.println( "[" + i + "]=" + a[ i ] );
1034 final public static void printCountingMap( final Map<String, Integer> counting_map ) {
1035 for( final String key : counting_map.keySet() ) {
1036 System.out.println( key + ": " + counting_map.get( key ) );
1040 final public static void printErrorMessage( final String prg_name, final String message ) {
1041 System.err.println( "[" + prg_name + "] > error: " + message );
1044 final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) {
1045 final int l = prg_name.length() + prg_version.length() + date.length() + 4;
1046 System.out.println();
1047 System.out.println( prg_name + " " + prg_version + " (" + date + ")" );
1048 for( int i = 0; i < l; ++i ) {
1049 System.out.print( "_" );
1051 System.out.println();
1054 final public static void printProgramInformation( final String prg_name,
1055 final String prg_version,
1058 final String www ) {
1059 printProgramInformation( prg_name, null, prg_version, date, email, www, null );
1062 final public static void printProgramInformation( final String prg_name,
1064 final String prg_version,
1068 final String based_on ) {
1069 String my_prg_name = new String( prg_name );
1070 if ( !ForesterUtil.isEmpty( desc ) ) {
1071 my_prg_name += ( " - " + desc );
1073 final int l = my_prg_name.length() + prg_version.length() + date.length() + 4;
1074 System.out.println();
1075 System.out.println( my_prg_name + " " + prg_version + " (" + date + ")" );
1076 for( int i = 0; i < l; ++i ) {
1077 System.out.print( "_" );
1079 System.out.println();
1080 System.out.println();
1081 System.out.println( "WWW : " + www );
1082 System.out.println( "Contact : " + email );
1083 if ( !ForesterUtil.isEmpty( based_on ) ) {
1084 System.out.println( "Based on: " + based_on );
1086 if ( !ForesterUtil.isEmpty( ForesterUtil.JAVA_VERSION ) && !ForesterUtil.isEmpty( ForesterUtil.JAVA_VENDOR ) ) {
1087 System.out.println();
1088 System.out.println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" );
1090 System.out.println();
1093 final public static void printWarningMessage( final String prg_name, final String message ) {
1094 System.out.println( "[" + prg_name + "] > warning: " + message );
1097 final public static void programMessage( final String prg_name, final String message ) {
1098 System.out.println( "[" + prg_name + "] > " + message );
1101 public static List<String> readUrl( final String url_str ) throws IOException {
1102 final URL url = new URL( url_str );
1103 final URLConnection urlc = url.openConnection();
1104 //urlc.setRequestProperty( "User-Agent", "" );
1105 final BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) );
1107 final List<String> result = new ArrayList<String>();
1108 while ( ( line = in.readLine() ) != null ) {
1117 * Example regarding engulfment: ------------0.1 ----------0.2 --0.3 =>
1118 * domain with 0.3 is ignored
1120 * -----------0.1 ----------0.2 --0.3 => domain with 0.3 is ignored
1123 * ------------0.1 ----------0.3 --0.2 => domains with 0.3 and 0.2 are _not_
1126 * @param max_allowed_overlap
1127 * maximal allowed overlap (inclusive) to be still considered not
1128 * overlapping (zero or negative value to allow any overlap)
1129 * @param remove_engulfed_domains
1130 * to remove domains which are completely engulfed by coverage of
1131 * domains with better support
1135 public static Protein removeOverlappingDomains( final int max_allowed_overlap,
1136 final boolean remove_engulfed_domains,
1137 final Protein protein ) {
1138 final Protein pruned_protein = new BasicProtein( protein.getProteinId().getId(), protein.getSpecies()
1139 .getSpeciesId(), protein.getLength() );
1140 final List<Domain> sorted = SurfacingUtil.sortDomainsWithAscendingConfidenceValues( protein );
1141 final List<Boolean> covered_positions = new ArrayList<Boolean>();
1142 for( final Domain domain : sorted ) {
1143 if ( ( ( max_allowed_overlap < 0 ) || ( ForesterUtil.calculateOverlap( domain, covered_positions ) <= max_allowed_overlap ) )
1144 && ( !remove_engulfed_domains || !isEngulfed( domain, covered_positions ) ) ) {
1145 final int covered_positions_size = covered_positions.size();
1146 for( int i = covered_positions_size; i < domain.getFrom(); ++i ) {
1147 covered_positions.add( false );
1149 final int new_covered_positions_size = covered_positions.size();
1150 for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) {
1151 if ( i < new_covered_positions_size ) {
1152 covered_positions.set( i, true );
1155 covered_positions.add( true );
1158 pruned_protein.addProteinDomain( domain );
1161 return pruned_protein;
1164 final public static String removeSuffix( final String file_name ) {
1165 final int i = file_name.lastIndexOf( '.' );
1167 return file_name.substring( 0, i );
1173 * Removes all white space from String s.
1175 * @return String s with white space removed
1177 final public static String removeWhiteSpace( String s ) {
1179 for( i = 0; i <= ( s.length() - 1 ); i++ ) {
1180 if ( ( s.charAt( i ) == ' ' ) || ( s.charAt( i ) == '\t' ) || ( s.charAt( i ) == '\n' )
1181 || ( s.charAt( i ) == '\r' ) ) {
1182 s = s.substring( 0, i ) + s.substring( i + 1 );
1189 final public static String replaceIllegalNhxCharacters( final String nhx ) {
1190 if ( nhx == null ) {
1193 return nhx.trim().replaceAll( "[\\[\\]']+", "_" );
1196 final public static double round( final double value, final int decimal_place ) {
1197 BigDecimal bd = new BigDecimal( value );
1198 bd = bd.setScale( decimal_place, BigDecimal.ROUND_HALF_UP );
1199 return bd.doubleValue();
1203 * Rounds d to an int.
1205 final public static int roundToInt( final double d ) {
1206 return ( int ) ( d + 0.5 );
1209 final public static int roundToInt( final float f ) {
1210 return ( int ) ( f + 0.5f );
1213 final public static short roundToShort( final double d ) {
1214 return ( short ) ( d + 0.5 );
1217 final public static String sanitizeString( final String s ) {
1226 public final static StringBuilder santitizeStringForNH( String data ) {
1227 data = data.replaceAll( "\\s+", " " ).trim();
1228 final StringBuilder sb = new StringBuilder();
1229 if ( data.length() > 0 ) {
1230 final boolean single_pars = data.indexOf( '\'' ) > -1;
1231 final boolean double_pars = data.indexOf( '"' ) > -1;
1232 if ( single_pars && double_pars ) {
1233 data = data.replace( '\'', '`' );
1238 else if ( single_pars ) {
1243 else if ( PARANTHESESABLE_NH_CHARS_PATTERN.matcher( data ).find() ) {
1255 public static boolean seqIsLikelyToBeAa( final String s ) {
1256 final String seq = s.toLowerCase();
1257 if ( ( seq.indexOf( 'r' ) > -1 ) || ( seq.indexOf( 'd' ) > -1 ) || ( seq.indexOf( 'e' ) > -1 )
1258 || ( seq.indexOf( 'q' ) > -1 ) || ( seq.indexOf( 'h' ) > -1 ) || ( seq.indexOf( 'k' ) > -1 )
1259 || ( seq.indexOf( 'w' ) > -1 ) || ( seq.indexOf( 's' ) > -1 ) || ( seq.indexOf( 'm' ) > -1 )
1260 || ( seq.indexOf( 'p' ) > -1 ) || ( seq.indexOf( 'v' ) > -1 ) ) {
1266 final private static String[] splitString( final String str ) {
1267 final String regex = "[\\s;,]+";
1268 return str.split( regex );
1271 final public static String stringArrayToString( final String[] a ) {
1272 return stringArrayToString( a, ", " );
1275 final public static String stringArrayToString( final String[] a, final String separator ) {
1276 final StringBuilder sb = new StringBuilder();
1277 if ( ( a != null ) && ( a.length > 0 ) ) {
1278 for( int i = 0; i < ( a.length - 1 ); ++i ) {
1279 sb.append( a[ i ] + separator );
1281 sb.append( a[ a.length - 1 ] );
1283 return sb.toString();
1286 final public static String[] stringListToArray( final List<String> list ) {
1287 if ( list != null ) {
1288 final String[] str = new String[ list.size() ];
1290 for( final String l : list ) {
1298 final public static String stringListToString( final List<String> l, final String separator ) {
1299 final StringBuilder sb = new StringBuilder();
1300 if ( ( l != null ) && ( l.size() > 0 ) ) {
1301 for( int i = 0; i < ( l.size() - 1 ); ++i ) {
1302 sb.append( l.get( i ) + separator );
1304 sb.append( l.get( l.size() - 1 ) );
1306 return sb.toString();
1309 final public static String[] stringSetToArray( final Set<String> strings ) {
1310 final String[] str_array = new String[ strings.size() ];
1312 for( final String e : strings ) {
1313 str_array[ i++ ] = e;
1318 final public static void unexpectedFatalError( final Error e ) {
1319 System.err.println();
1320 System.err.println( "unexpected error: should not have occured! Please contact program author(s)." );
1321 e.printStackTrace( System.err );
1322 System.err.println();
1326 final public static void unexpectedFatalError( final Exception e ) {
1327 System.err.println();
1328 System.err.println( "unexpected exception: should not have occured! Please contact program author(s)." );
1329 e.printStackTrace( System.err );
1330 System.err.println();
1334 final public static void unexpectedFatalError( final String message ) {
1335 System.err.println();
1336 System.err.println( "unexpected error: should not have occured! Please contact program author(s)." );
1337 System.err.println( message );
1338 System.err.println();
1342 final public static void unexpectedFatalError( final String prg_name, final Exception e ) {
1343 System.err.println();
1344 System.err.println( "[" + prg_name
1345 + "] > unexpected error; should not have occured! Please contact program author(s)." );
1346 e.printStackTrace( System.err );
1347 System.err.println();
1351 final public static void unexpectedFatalError( final String prg_name, final String message ) {
1352 System.err.println();
1353 System.err.println( "[" + prg_name
1354 + "] > unexpected error: should not have occured! Please contact program author(s)." );
1355 System.err.println( message );
1356 System.err.println();
1360 final public static void unexpectedFatalError( final String prg_name, final String message, final Exception e ) {
1361 System.err.println();
1362 System.err.println( "[" + prg_name
1363 + "] > unexpected error: should not have occured! Please contact program author(s)." );
1364 System.err.println( message );
1365 e.printStackTrace( System.err );
1366 System.err.println();
1370 public final static void updateProgress( final double progress_percentage ) {
1371 final int width = 50;
1372 System.out.print( "\r[" );
1374 for( ; i <= ForesterUtil.roundToInt( progress_percentage * width ); i++ ) {
1375 System.out.print( "." );
1377 for( ; i < width; i++ ) {
1378 System.out.print( " " );
1380 System.out.print( "]" );
1383 public final static void updateProgress( final int i, final DecimalFormat f ) {
1384 System.out.print( "\r[" + f.format( i ) + "]" );
1387 public final static String wordWrap( final String str, final int width ) {
1388 final StringBuilder sb = new StringBuilder( str );
1392 while ( i < sb.length() ) {
1393 if ( sb.charAt( i ) == ' ' ) {
1396 if ( sb.charAt( i ) == '\n' ) {
1400 if ( i > ( ( start + width ) - 1 ) ) {
1402 sb.setCharAt( ls, '\n' );
1407 sb.insert( i, '\n' );
1413 return sb.toString();
1416 private ForesterUtil() {