X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Futil%2FForesterUtil.java;h=f144a81684894675f7a3442a03586b1560e9b619;hb=aff0b302cc19e0ee3e73bc4a87884b4591ac5f08;hp=84be63a8484b4bd556f7d3d8490be7ea5d5657de;hpb=7f6a75230f6a811e2409dc710aac2189f28351f5;p=jalview.git diff --git a/forester/java/src/org/forester/util/ForesterUtil.java b/forester/java/src/org/forester/util/ForesterUtil.java index 84be63a..f144a81 100644 --- a/forester/java/src/org/forester/util/ForesterUtil.java +++ b/forester/java/src/org/forester/util/ForesterUtil.java @@ -34,6 +34,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; +import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -42,6 +43,8 @@ import java.io.Writer; import java.math.BigDecimal; import java.net.URL; import java.net.URLConnection; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; @@ -62,14 +65,19 @@ import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.forester.archaeopteryx.Constants; +import org.forester.archaeopteryx.AptxConstants; +import org.forester.io.parsers.PhylogenyParser; +import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.data.Distribution; import org.forester.phylogeny.data.Sequence; import org.forester.phylogeny.data.Taxonomy; +import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory; import org.forester.protein.BasicProtein; import org.forester.protein.Domain; import org.forester.protein.Protein; +import org.forester.sequence.MolecularSequence; +import org.forester.sequence.MolecularSequence.TYPE; import org.forester.surfacing.SurfacingUtil; public final class ForesterUtil { @@ -91,7 +99,7 @@ public final class ForesterUtil { public final static String OS_VERSION = System.getProperty( "os.version" ); public static final String PDB = "http://www.pdb.org/pdb/explore/explore.do?pdbId="; public final static String UNIPROT_KB = "http://www.uniprot.org/uniprot/"; - public final static double ZERO_DIFF = 1.0E-9; + public final static double ZERO_DIFF = 1.0E-12; private static final Pattern PARANTHESESABLE_NH_CHARS_PATTERN = Pattern.compile( "[(),;\\s:\\[\\]]" ); static { final DecimalFormatSymbols dfs = new DecimalFormatSymbols(); @@ -103,26 +111,30 @@ public final class ForesterUtil { FORMATTER_3 = new DecimalFormat( "#.###", dfs ); } - private ForesterUtil() { - } - final public static void appendSeparatorIfNotEmpty( final StringBuffer sb, final char separator ) { if ( sb.length() > 0 ) { sb.append( separator ); } } + final public static String removeFileExtension( final String file_name ) { + if ( file_name.indexOf( "." ) > 0 ) { + return file_name.substring( 0, file_name.lastIndexOf( "." ) ); + } + return file_name; + } + /** * This calculates a color. If value is equal to min the returned color is * minColor, if value is equal to max the returned color is maxColor, * otherwise a color 'proportional' to value is returned. - * + * * @param value - * the value + * the value * @param min - * the smallest value + * the smallest value * @param max - * the largest value + * the largest value * @param minColor * the color for min * @param maxColor @@ -153,15 +165,15 @@ public final class ForesterUtil { * value is equal to mean the returned color is meanColor, otherwise a color * 'proportional' to value is returned -- either between min-mean or * mean-max - * + * * @param value * the value * @param min * the smallest value * @param max - * the largest value + * the largest value * @param mean - * the mean/median value + * the mean/median value * @param minColor * the color for min * @param maxColor @@ -202,6 +214,40 @@ public final class ForesterUtil { } } + /** + * Helper method for calcColor methods. + * + * @param smallercolor_component_x + * color component the smaller color + * @param largercolor_component_x + * color component the larger color + * @param x + * factor + * @return an int representing a color component + */ + final private static int calculateColorComponent( final double smallercolor_component_x, + final double largercolor_component_x, + final double x ) { + return ( int ) ( smallercolor_component_x + + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) ); + } + + /** + * Helper method for calcColor methods. + * + * + * @param value + * the value + * @param larger + * the largest value + * @param smaller + * the smallest value + * @return a normalized value between larger and smaller + */ + final private static double calculateColorFactor( final double value, final double larger, final double smaller ) { + return ( 255.0 * ( value - smaller ) ) / ( larger - smaller ); + } + public static int calculateOverlap( final Domain domain, final List covered_positions ) { int overlap_count = 0; for( int i = domain.getFrom(); i <= domain.getTo(); ++i ) { @@ -495,6 +541,23 @@ public final class ForesterUtil { return ForesterUtil.LINE_SEPARATOR; } + final public static MolecularSequence.TYPE guessMolecularSequenceType( final String mol_seq ) { + final String s = mol_seq.toUpperCase(); + if ( s.contains( "L" ) || s.contains( "I" ) || s.contains( "E" ) || s.contains( "H" ) || s.contains( "D" ) + || s.contains( "Q" ) ) { + return TYPE.AA; + } + else { + if ( s.contains( "T" ) ) { + return TYPE.DNA; + } + else if ( s.contains( "U" ) ) { + return TYPE.RNA; + } + } + return null; + } + final public static void increaseCountingMap( final Map counting_map, final String item_name ) { if ( !counting_map.containsKey( item_name ) ) { counting_map.put( item_name, 1 ); @@ -532,10 +595,17 @@ public final class ForesterUtil { return ( ( s == null ) || ( s.length() < 1 ) ); } + final public static boolean isEmptyTrimmed( final String s ) { + if ( s == null ) { + return true; + } + return ( ( s.trim().length() < 1 ) ); + } + /** * Returns true is Domain domain falls in an uninterrupted stretch of * covered positions. - * + * * @param domain * @param covered_positions * @return @@ -553,6 +623,10 @@ public final class ForesterUtil { return ( ( Math.abs( a - b ) ) < ZERO_DIFF ); } + final public static boolean isEqual( final double a, final double b, final double tolerance ) { + return ( ( Math.abs( a - b ) ) < tolerance ); + } + final public static boolean isEven( final int n ) { return ( n % 2 ) == 0; } @@ -561,7 +635,7 @@ public final class ForesterUtil { * This determines whether String[] a and String[] b have at least one * String in common (intersect). Returns false if at least one String[] is * null or empty. - * + * * @param a * a String[] b a String[] * @return true if both a and b or not empty or null and contain at least @@ -598,7 +672,7 @@ public final class ForesterUtil { return OS_NAME.toLowerCase().startsWith( "mac" ); } catch ( final Exception e ) { - ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e ); + ForesterUtil.printWarningMessage( AptxConstants.PRG_NAME, "minor error: " + e ); return false; } } @@ -635,7 +709,7 @@ public final class ForesterUtil { return OS_NAME.toLowerCase().indexOf( "win" ) > -1; } catch ( final Exception e ) { - ForesterUtil.printWarningMessage( Constants.PRG_NAME, "minor error: " + e ); + ForesterUtil.printWarningMessage( AptxConstants.PRG_NAME, "minor error: " + e ); return false; } } @@ -650,6 +724,10 @@ public final class ForesterUtil { return null; } + final public static String isWritableFile( final String s ) { + return isWritableFile( new File( s ) ); + } + /** * Helper for method "stringToColor". *

@@ -681,7 +759,8 @@ public final class ForesterUtil { final public static void map2file( final File file, final Map data, final String entry_separator, - final String data_separator ) throws IOException { + final String data_separator ) + throws IOException { final Writer writer = new BufferedWriter( new FileWriter( file ) ); map2writer( writer, data, entry_separator, data_separator ); writer.close(); @@ -690,7 +769,8 @@ public final class ForesterUtil { final public static void map2writer( final Writer writer, final Map data, final String entry_separator, - final String data_separator ) throws IOException { + final String data_separator ) + throws IOException { boolean first = true; for( final Entry entry : data.entrySet() ) { if ( !first ) { @@ -705,7 +785,8 @@ public final class ForesterUtil { } } - final public static StringBuffer mapToStringBuffer( final Map map, final String key_value_separator ) { + final public static StringBuffer mapToStringBuffer( final Map map, + final String key_value_separator ) { final StringBuffer sb = new StringBuffer(); for( final Object key : map.keySet() ) { sb.append( key.toString() ); @@ -812,6 +893,21 @@ public final class ForesterUtil { else if ( tax_group.equals( TaxonomyGroups.BACTERIA ) ) { return TaxonomyColors.BACTERIA_COLOR; } + else if ( tax_group.equals( TaxonomyGroups.VIRUSES ) ) { + return TaxonomyColors.VIRUSES_COLOR; + } + else if ( tax_group.equals( TaxonomyGroups.ALPHAHERPESVIRINAE ) ) { + return TaxonomyColors.ALPHAHERPESVIRINAE_COLOR; + } + else if ( tax_group.equals( TaxonomyGroups.BETAHERPESVIRINAE ) ) { + return TaxonomyColors.BETAHERPESVIRINAE_COLOR; + } + else if ( tax_group.equals( TaxonomyGroups.GAMMAHERPESVIRINAE ) ) { + return TaxonomyColors.GAMMAHERPESVIRINAE_COLOR; + } + else if ( tax_group.equals( TaxonomyGroups.OTHER ) ) { + return TaxonomyColors.OTHER_COLOR; + } } return null; } @@ -892,6 +988,21 @@ public final class ForesterUtil { else if ( tax.equalsIgnoreCase( TaxonomyGroups.BACTERIA ) ) { return TaxonomyGroups.BACTERIA; } + else if ( tax.equalsIgnoreCase( TaxonomyGroups.BACTERIA ) ) { + return TaxonomyGroups.BACTERIA; + } + else if ( tax.equalsIgnoreCase( TaxonomyGroups.VIRUSES ) ) { + return TaxonomyGroups.VIRUSES; + } + else if ( tax.equalsIgnoreCase( TaxonomyGroups.ALPHAHERPESVIRINAE ) ) { + return TaxonomyGroups.ALPHAHERPESVIRINAE; + } + else if ( tax.equalsIgnoreCase( TaxonomyGroups.BETAHERPESVIRINAE ) ) { + return TaxonomyGroups.BETAHERPESVIRINAE; + } + else if ( tax.equalsIgnoreCase( TaxonomyGroups.GAMMAHERPESVIRINAE ) ) { + return TaxonomyGroups.GAMMAHERPESVIRINAE; + } return null; } @@ -935,11 +1046,17 @@ public final class ForesterUtil { System.exit( -1 ); } - final public static StringBuffer pad( final double number, final int size, final char pad, final boolean left_pad ) { + final public static StringBuffer pad( final double number, + final int size, + final char pad, + final boolean left_pad ) { return pad( new StringBuffer( number + "" ), size, pad, left_pad ); } - final public static StringBuffer pad( final String string, final int size, final char pad, final boolean left_pad ) { + final public static StringBuffer pad( final String string, + final int size, + final char pad, + final boolean left_pad ) { return pad( new StringBuffer( string ), size, pad, left_pad ); } @@ -993,7 +1110,9 @@ public final class ForesterUtil { System.err.println( "[" + prg_name + "] > error: " + message ); } - final public static void printProgramInformation( final String prg_name, final String prg_version, final String date ) { + final public static void printProgramInformation( final String prg_name, + final String prg_version, + final String date ) { final int l = prg_name.length() + prg_version.length() + date.length() + 4; System.out.println(); System.out.println( prg_name + " " + prg_version + " (" + date + ")" ); @@ -1037,7 +1156,8 @@ public final class ForesterUtil { } 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 + "]" ); + System.out + .println( "[running on Java " + ForesterUtil.JAVA_VERSION + " " + ForesterUtil.JAVA_VENDOR + "]" ); } System.out.println(); } @@ -1050,17 +1170,31 @@ public final class ForesterUtil { System.out.println( "[" + prg_name + "] > " + message ); } + public static List readUrl( final String url_str ) throws IOException { + final URL url = new URL( url_str ); + final URLConnection urlc = url.openConnection(); + //urlc.setRequestProperty( "User-Agent", "" ); + final BufferedReader in = new BufferedReader( new InputStreamReader( urlc.getInputStream() ) ); + String line; + final List result = new ArrayList(); + while ( ( line = in.readLine() ) != null ) { + result.add( line ); + } + in.close(); + return result; + } + /** - * + * * Example regarding engulfment: ------------0.1 ----------0.2 --0.3 => * domain with 0.3 is ignored - * + * * -----------0.1 ----------0.2 --0.3 => domain with 0.3 is ignored - * - * + * + * * ------------0.1 ----------0.3 --0.2 => domains with 0.3 and 0.2 are _not_ * ignored - * + * * @param max_allowed_overlap * maximal allowed overlap (inclusive) to be still considered not * overlapping (zero or negative value to allow any overlap) @@ -1073,12 +1207,14 @@ public final class ForesterUtil { public static Protein removeOverlappingDomains( final int max_allowed_overlap, final boolean remove_engulfed_domains, final Protein protein ) { - final Protein pruned_protein = new BasicProtein( protein.getProteinId().getId(), protein.getSpecies() - .getSpeciesId(), protein.getLength() ); + final Protein pruned_protein = new BasicProtein( protein.getProteinId().getId(), + protein.getSpecies().getSpeciesId(), + protein.getLength() ); final List sorted = SurfacingUtil.sortDomainsWithAscendingConfidenceValues( protein ); final List covered_positions = new ArrayList(); for( final Domain domain : sorted ) { - if ( ( ( max_allowed_overlap < 0 ) || ( ForesterUtil.calculateOverlap( domain, covered_positions ) <= max_allowed_overlap ) ) + if ( ( ( max_allowed_overlap < 0 ) + || ( ForesterUtil.calculateOverlap( domain, covered_positions ) <= max_allowed_overlap ) ) && ( !remove_engulfed_domains || !isEngulfed( domain, covered_positions ) ) ) { final int covered_positions_size = covered_positions.size(); for( int i = covered_positions_size; i < domain.getFrom(); ++i ) { @@ -1109,7 +1245,7 @@ public final class ForesterUtil { /** * Removes all white space from String s. - * + * * @return String s with white space removed */ final public static String removeWhiteSpace( String s ) { @@ -1201,6 +1337,11 @@ public final class ForesterUtil { return false; } + final private static String[] splitString( final String str ) { + final String regex = "[\\s;,]+"; + return str.split( regex ); + } + final public static String stringArrayToString( final String[] a ) { return stringArrayToString( a, ", " ); } @@ -1346,41 +1487,224 @@ public final class ForesterUtil { return sb.toString(); } - /** - * Helper method for calcColor methods. - * - * @param smallercolor_component_x - * color component the smaller color - * @param largercolor_component_x - * color component the larger color - * @param x - * factor - * @return an int representing a color component - */ - final private static int calculateColorComponent( final double smallercolor_component_x, - final double largercolor_component_x, - final double x ) { - return ( int ) ( smallercolor_component_x + ( ( x * ( largercolor_component_x - smallercolor_component_x ) ) / 255.0 ) ); + public final static Phylogeny[] readPhylogeniesFromUrl( final URL url, final PhylogenyParser parser ) + throws NoSuchAlgorithmException, IOException, KeyManagementException { + if ( url == null ) { + throw new IllegalArgumentException( "URL to read from must not be null" ); + } + else if ( parser == null ) { + throw new IllegalArgumentException( "parser to use to read from URL must not be null" ); + } + final URLConnection con; + if ( url.toString().startsWith( "https:" ) ) { + con = TrustManager.makeHttpsURLConnection( url ); + } + else if ( url.toString().startsWith( "http:" ) ) { + con = url.openConnection(); + } + else { + throw new IllegalArgumentException( "Cannot deal with URL: " + url ); + } + if ( con == null ) { + throw new IOException( "could not create connection from " + url ); + } + con.setDefaultUseCaches( false ); + final InputStream is = con.getInputStream(); + if ( is == null ) { + throw new IOException( "could not create input stream from " + url ); + } + final Phylogeny[] trees = ParserBasedPhylogenyFactory.getInstance().create( is, parser ); + try { + is.close(); + } + catch ( final Exception e ) { + // ignore + } + return trees; } - /** - * Helper method for calcColor methods. - * - * - * @param value - * the value - * @param larger - * the largest value - * @param smaller - * the smallest value - * @return a normalized value between larger and smaller - */ - final private static double calculateColorFactor( final double value, final double larger, final double smaller ) { - return ( 255.0 * ( value - smaller ) ) / ( larger - smaller ); + public final static File getMatchingFile( final File dir, final String prefix, final String suffix ) + throws IOException { + if ( !dir.exists() ) { + throw new IOException( "[" + dir + "] does not exist" ); + } + if ( !dir.isDirectory() ) { + throw new IOException( "[" + dir + "] is not a directory" ); + } + if ( dir.listFiles().length == 0 ) { + throw new IOException( "[" + dir + "] is empty" ); + } + final File files[] = dir.listFiles( new FilenameFilter() { + + @Override + public boolean accept( final File dir, final String name ) { + return ( name.endsWith( suffix ) ); + } + } ); + if ( files.length == 0 ) { + throw new IOException( "no files ending with \"" + suffix + "\" found in [" + dir + "]" ); + } + String my_prefix = prefix; + boolean done = false; + boolean more_than_one = false; + File the_one = null; + do { + int matches = 0; + for( final File file : files ) { + if ( file.getName().startsWith( my_prefix ) ) { + matches++; + if ( matches > 1 ) { + the_one = null; + break; + } + the_one = file; + } + } + if ( matches > 1 ) { + more_than_one = true; + done = true; + } + if ( matches == 1 ) { + done = true; + } + else { + if ( my_prefix.length() <= 1 ) { + throw new IOException( "no file matching \"" + removeFileExtension( prefix ) + + "\" and ending with \"" + suffix + "\" found in [" + dir + "]" ); + } + my_prefix = my_prefix.substring( 0, my_prefix.length() - 1 ); + } + } while ( !done ); + if ( more_than_one ) { + throw new IOException( "multiple files matching \"" + removeFileExtension( prefix ) + + "\" and ending with \"" + suffix + "\" found in [" + dir + "]" ); + } + else if ( the_one != null ) { + } + else { + throw new IOException( "no file matching \"" + removeFileExtension( prefix ) + "\" and ending with \"" + + suffix + "\" found in [" + dir + "]" ); + } + return the_one; } - final private static String[] splitString( final String str ) { - final String regex = "[\\s;,]+"; - return str.split( regex ); + public final static String greatestCommonPrefix( final String a, final String b ) { + final int min_length = Math.min( a.length(), b.length() ); + for( int i = 0; i < min_length; ++i ) { + if ( a.charAt( i ) != b.charAt( i ) ) { + return a.substring( 0, i ); + } + } + return a.substring( 0, min_length ); + } + + public final static boolean isContainsPrefix( final String word, final String prefix, final String separator ) { + if ( ForesterUtil.isEmpty( separator ) ) { + throw new IllegalArgumentException( "separator must not be null or empty" ); + } + final String[] word_ary = word.split( Pattern.quote( separator ) ); + final String[] prefix_ary = prefix.split( Pattern.quote( separator ) ); + if ( word_ary.length < prefix_ary.length ) { + return false; + } + final int prefix_ary_length = prefix_ary.length; + for( int i = 0; i < prefix_ary_length; ++i ) { + if ( !( word_ary[ i ].equals( prefix_ary[ i ] ) ) ) { + return false; + } + } + return true; + } + + public final static String greatestCommonPrefix( final String a, final String b, final String separator ) { + if ( ForesterUtil.isEmpty( separator ) ) { + throw new IllegalArgumentException( "separator must not be null or empty" ); + } + final String[] as = a.split( Pattern.quote( separator ) ); + final String[] bs = b.split( Pattern.quote( separator ) ); + final int min_length = Math.min( as.length, bs.length ); + for( int i = 0; i < min_length; ++i ) { + if ( !( as[ i ].equals( bs[ i ] ) ) ) { + final StringBuilder sb = new StringBuilder(); + boolean first = true; + for( int j = 0; j < i; ++j ) { + if ( first ) { + first = false; + } + else { + sb.append( separator ); + } + sb.append( as[ j ] ); + } + return sb.toString(); + } + } + final StringBuilder sb = new StringBuilder(); + boolean first = true; + for( int j = 0; j < min_length; ++j ) { + if ( first ) { + first = false; + } + else { + sb.append( separator ); + } + sb.append( as[ j ] ); + } + return sb.toString(); + } + + public final static String greatestCommonPrefix( final List strings ) { + if ( strings == null ) { + throw new IllegalArgumentException( "list of strings is null" ); + } + if ( strings.isEmpty() ) { + throw new IllegalArgumentException( "list of strings is empty" ); + } + String common = strings.get( 0 ); + for( int i = 1; i < strings.size(); ++i ) { + common = greatestCommonPrefix( common, strings.get( i ) ); + } + return common; + } + + public final static String greatestCommonPrefix( final List strings, final String separator ) { + if ( ForesterUtil.isEmpty( separator ) ) { + return greatestCommonPrefix( strings ); + } + if ( strings == null ) { + throw new IllegalArgumentException( "list of strings is null" ); + } + if ( strings.isEmpty() ) { + throw new IllegalArgumentException( "list of strings is empty" ); + } + String common = strings.get( 0 ); + for( int i = 1; i < strings.size(); ++i ) { + common = greatestCommonPrefix( common, strings.get( i ), separator ); + } + return common; + } + + public static List spliIntoPrefixes( final String prefix, final String separator ) { + final String[] a = prefix.split( Pattern.quote( separator ) ); + final List l = new ArrayList(); + for( int i = 0; i < a.length; ++i ) { + final StringBuilder sb = new StringBuilder(); + for( int j = 0; j <= i; ++j ) { + sb.append( a[ j ] ); + if ( j < i ) { + sb.append( separator ); + } + } + l.add( sb.toString() ); + } + return l; + } + + public static boolean isLooksLikeFasta( final File file ) throws IOException { + final String first_line = ForesterUtil.getFirstLine( file ).trim().toLowerCase(); + return ( ( !isEmptyTrimmed( first_line ) && first_line.trim().startsWith( ">" ) ) ); + } + + private ForesterUtil() { } }