phylotastic hackathon at NESCENT 120606
[jalview.git] / forester / java / src / org / forester / test / Test.java
index dce54dc..9069693 100644 (file)
@@ -54,9 +54,11 @@ import org.forester.io.parsers.nhx.NHXParser;
 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
 import org.forester.io.parsers.tol.TolParser;
 import org.forester.io.writers.PhylogenyWriter;
+import org.forester.msa.BasicMsa;
 import org.forester.msa.Mafft;
 import org.forester.msa.Msa;
 import org.forester.msa.MsaInferrer;
+import org.forester.msa.MsaMethods;
 import org.forester.pccx.TestPccx;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyBranch;
@@ -101,6 +103,7 @@ import org.forester.util.DescriptiveStatistics;
 import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
 import org.forester.util.GeneralTable;
+import org.forester.util.SequenceIdParser;
 import org.forester.ws.uniprot.DatabaseTools;
 import org.forester.ws.uniprot.SequenceDatabaseEntry;
 import org.forester.ws.uniprot.UniProtTaxonomy;
@@ -169,6 +172,19 @@ public final class Test {
             System.exit( -1 );
         }
         final long start_time = new Date().getTime();
+        
+        
+       
+        System.out.print( "Sequence id parsing: " );
+        if (  testSequenceIdParsing() ) {
+            System.out.println( "OK." );
+            succeeded++;
+        }
+        else {
+            System.out.println( "failed." );
+            System.exit( -1 ); //TODO FIXME remove me!! ~
+            failed++;
+        }
         System.out.print( "Hmmscan output parser: " );
         if ( testHmmscanOutputParser() ) {
             System.out.println( "OK." );
@@ -704,6 +720,15 @@ public final class Test {
             System.out.println( "failed." );
             failed++;
         }
+        System.out.print( "Simple MSA quality: " );
+        if ( Test.testMsaQualityMethod() ) {
+            System.out.println( "OK." );
+            succeeded++;
+        }
+        else {
+            System.out.println( "failed." );
+            failed++;
+        }
         //        System.out.print( "WABI TxSearch: " );
         //        if ( Test.testWabiTxSearch() ) {
         //            System.out.println( "OK." );
@@ -2850,7 +2875,7 @@ public final class Test {
             dss3.addValue( 10 );
             final AsciiHistogram histo = new AsciiHistogram( dss3 );
             histo.toStringBuffer( 10, '=', 40, 5 );
-            histo.toStringBuffer( 3, 8, 10, '=', 40, 5 );
+            histo.toStringBuffer( 3, 8, 10, '=', 40, 5, null );
         }
         catch ( final Exception e ) {
             e.printStackTrace( System.out );
@@ -8200,12 +8225,48 @@ public final class Test {
         try {
             final String msa_str_0 = "seq1 abcd\n\nseq2 efgh\n";
             final Msa msa_0 = GeneralMsaParser.parse( new ByteArrayInputStream( msa_str_0.getBytes() ) );
-            final String msa_str_1 = "seq_1 abc\nseq2 ghi\nseq_1 def\nseq2 jkm\n";
+            final String msa_str_1 = "seq1 abc\nseq2 ghi\nseq1 def\nseq2 jkm\n";
             final Msa msa_1 = GeneralMsaParser.parse( new ByteArrayInputStream( msa_str_1.getBytes() ) );
             final String msa_str_2 = "seq1 abc\nseq2 ghi\n\ndef\njkm\n";
             final Msa msa_2 = GeneralMsaParser.parse( new ByteArrayInputStream( msa_str_2.getBytes() ) );
             final String msa_str_3 = "seq1 abc\n def\nseq2 ghi\n jkm\n";
             final Msa msa_3 = GeneralMsaParser.parse( new ByteArrayInputStream( msa_str_3.getBytes() ) );
+            if ( !msa_1.getSequenceAsString( 0 ).toString().equalsIgnoreCase( "abcdef" ) ) {
+                return false;
+            }
+            if ( !msa_1.getSequenceAsString( 1 ).toString().equalsIgnoreCase( "ghixkm" ) ) {
+                return false;
+            }
+            if ( !msa_1.getIdentifier( 0 ).toString().equals( "seq1" ) ) {
+                return false;
+            }
+            if ( !msa_1.getIdentifier( 1 ).toString().equals( "seq2" ) ) {
+                return false;
+            }
+            if ( !msa_2.getSequenceAsString( 0 ).toString().equalsIgnoreCase( "abcdef" ) ) {
+                return false;
+            }
+            if ( !msa_2.getSequenceAsString( 1 ).toString().equalsIgnoreCase( "ghixkm" ) ) {
+                return false;
+            }
+            if ( !msa_2.getIdentifier( 0 ).toString().equals( "seq1" ) ) {
+                return false;
+            }
+            if ( !msa_2.getIdentifier( 1 ).toString().equals( "seq2" ) ) {
+                return false;
+            }
+            if ( !msa_3.getSequenceAsString( 0 ).toString().equalsIgnoreCase( "abcdef" ) ) {
+                return false;
+            }
+            if ( !msa_3.getSequenceAsString( 1 ).toString().equalsIgnoreCase( "ghixkm" ) ) {
+                return false;
+            }
+            if ( !msa_3.getIdentifier( 0 ).toString().equals( "seq1" ) ) {
+                return false;
+            }
+            if ( !msa_3.getIdentifier( 1 ).toString().equals( "seq2" ) ) {
+                return false;
+            }
             final Msa msa_4 = GeneralMsaParser.parse( new FileInputStream( PATH_TO_TEST_DATA + "msa_1.txt" ) );
             if ( !msa_4.getSequenceAsString( 0 ).toString().equalsIgnoreCase( "abcdefeeeeeeeexx" ) ) {
                 return false;
@@ -8254,7 +8315,10 @@ public final class Test {
             Msa msa = null;
             final MsaInferrer mafft = Mafft.createInstance();
             msa = mafft.infer( new File( PATH_TO_TEST_DATA + "ncbi_sn.fasta" ), opts );
-            if ( ( msa == null ) || ( msa.getLength() < 10 ) || ( msa.getNumberOfSequences() != 19 ) ) {
+            if ( ( msa == null ) || ( msa.getLength() < 20 ) || ( msa.getNumberOfSequences() != 19 ) ) {
+                return false;
+            }
+            if ( !msa.getIdentifier( 0 ).toString().equals( "a" ) ) {
                 return false;
             }
         }
@@ -8825,4 +8889,50 @@ public final class Test {
         }
         return true;
     }
+
+    private static boolean testMsaQualityMethod() {
+        try {
+            final Sequence s0 = BasicSequence.createAaSequence( "a", "ABAXEFGHIJ" );
+            final Sequence s1 = BasicSequence.createAaSequence( "a", "ABBXEFGHIJ" );
+            final Sequence s2 = BasicSequence.createAaSequence( "a", "AXCXEFGHIJ" );
+            final Sequence s3 = BasicSequence.createAaSequence( "a", "AXDDEFGHIJ" );
+            final List<Sequence> l = new ArrayList<Sequence>();
+            l.add( s0 );
+            l.add( s1 );
+            l.add( s2 );
+            l.add( s3 );
+            final Msa msa = BasicMsa.createInstance( l );
+            if ( !isEqual( 1, MsaMethods.calculateIdentityRatio( msa, 0 ) ) ) {
+                return false;
+            }
+            if ( !isEqual( 0.5, MsaMethods.calculateIdentityRatio( msa, 1 ) ) ) {
+                return false;
+            }
+            if ( !isEqual( 0.25, MsaMethods.calculateIdentityRatio( msa, 2 ) ) ) {
+                return false;
+            }
+            if ( !isEqual( 0.75, MsaMethods.calculateIdentityRatio( msa, 3 ) ) ) {
+                return false;
+            }
+        }
+        catch ( final Exception e ) {
+            e.printStackTrace( System.out );
+            return false;
+        }
+        return true;
+    }
+    
+    private static boolean testSequenceIdParsing() {
+        try {
+            Identifier id = SequenceIdParser.parse( "mites|ref_XP_002434188_1" );
+            if ( id == null || ForesterUtil.isEmpty( id.getValue() ) || !id.getValue().equals( "002434188_1" ) ) {
+                return false;
+            }
+        }
+        catch ( final Exception e ) {
+            e.printStackTrace( System.out );
+            return false;
+        }
+        return true;
+    }
 }