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();
}
final public static MolecularSequence.TYPE guessMolecularSequenceType( final String mol_seq ) {
- if ( mol_seq.contains( "L" ) || mol_seq.contains( "I" ) || mol_seq.contains( "E" ) || mol_seq.contains( "H" )
- || mol_seq.contains( "D" ) || mol_seq.contains( "Q" ) ) {
+ 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 ( mol_seq.contains( "T" ) ) {
+ if ( s.contains( "T" ) ) {
return TYPE.DNA;
}
- else if ( mol_seq.contains( "U" ) ) {
+ else if ( s.contains( "U" ) ) {
return TYPE.RNA;
}
}
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.
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;
}
return null;
}
+ final public static String isWritableFile( final String s ) {
+ return isWritableFile( new File( s ) );
+ }
+
/**
* Helper for method "stringToColor".
* <p>
is.close();
}
catch ( final Exception e ) {
- // ignore
+ // ignore
}
return trees;
}
- private ForesterUtil() {
- }
-
public final static File getMatchingFile( final File dir, final String prefix, final String suffix )
throws IOException {
if ( !dir.exists() ) {
if ( !dir.isDirectory() ) {
throw new IOException( "[" + dir + "] is not a directory" );
}
- final File mapping_files[] = dir.listFiles( new FilenameFilter() {
+ 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 ( mapping_files.length == 1 ) {
+ if ( files.length == 0 ) {
throw new IOException( "no files ending with \"" + suffix + "\" found in [" + dir + "]" );
}
- String my_prefix = removeFileExtension( prefix );
+ String my_prefix = prefix;
boolean done = false;
boolean more_than_one = false;
File the_one = null;
do {
int matches = 0;
- for( File file : mapping_files ) {
+ for( final File file : files ) {
if ( file.getName().startsWith( my_prefix ) ) {
matches++;
if ( matches > 1 ) {
}
return the_one;
}
+
+ 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<String> 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<String> 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<String> spliIntoPrefixes( final String prefix, final String separator ) {
+ final String[] a = prefix.split( Pattern.quote( separator ) );
+ final List<String> l = new ArrayList<String>();
+ 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() {
+ }
}