package edu.columbia.gemma.tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

import junit.framework.TestCase;
import baseCode.dataStructure.matrix.DenseDoubleMatrix2DNamed;
import baseCode.dataStructure.matrix.NamedMatrix;
import baseCode.io.reader.DoubleMatrixReader;
import edu.columbia.gemma.expression.arrayDesign.ArrayDesign;

/**
 * <hr>
 * <p>
 * Copyright (c) 2004-2005 Columbia University
 * 
 * @author pavlidis
 * @version $Id$
 */
public class AffyAnalyzeTest extends TestCase {
    // private static Log log = LogFactory.getLog( AffyAnalyzeTest.class.getName() );
    AffyAnalyze aa;
    DenseDoubleMatrix2DNamed celmatrix;
    ArrayDesign arrayDesign;
    InputStream is;

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();

        // test data are from the affybatch.example in the affy package.
        DoubleMatrixReader reader = new DoubleMatrixReader();
        is = new GZIPInputStream( this.getClass().getResourceAsStream( "/data/testShortCel.txt.gz" ) );
        if ( is == null ) throw new IOException();
        celmatrix = ( DenseDoubleMatrix2DNamed ) reader.read( is );
        is.close();
        arrayDesign = ArrayDesign.Factory.newInstance();
        arrayDesign.setName( "cdfenv.example" );
        aa = new AffyAnalyze();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
        aa = null;
    }

    /*
     * Test method for 'edu.columbia.gemma.tools.AffyAnalyze.AffyBatch(DenseDoubleMatrix2DNamed, ArrayDesign)'
     */
    public void testAffyBatch() {
        aa.AffyBatch( celmatrix, arrayDesign );
    }

    /*
     * Test method for 'edu.columbia.gemma.tools.AffyAnalyze.rma(DenseDoubleMatrix2DNamed, ArrayDesign)'
     */
    public void testRma() {
        NamedMatrix result = aa.rma( celmatrix, arrayDesign );
        assertTrue( result != null );
        assertEquals( 150, result.rows() );
        assertEquals( 3, result.columns() );
        assertEquals( "A28102_at", result.getRowName( 0 ) );
    }

    /*
     * Test method for 'edu.columbia.gemma.tools.AffyAnalyze.normalize(DenseDoubleMatrix2DNamed, ArrayDesign)'
     */
    public void testNormalize() {
        aa.normalize( celmatrix, arrayDesign );
    }

    /*
     * Test method for 'edu.columbia.gemma.tools.AffyAnalyze.backgroundTreat(DenseDoubleMatrix2DNamed, ArrayDesign)'
     */
    public void testBackgroundTreat() {
        aa.backgroundTreat( celmatrix, arrayDesign );
    }

}

@@ -1,12 +1,650 @@
+/*%0A * The Gemma project%0A * %0A * Copyright (c) 2005 Columbia University%0A * %0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *       http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A *%0A */%0A
 package edu.
@@ -761,16 +761,102 @@
 tream;%0A%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0A
 import j
@@ -1264,19 +1264,16 @@
 se %7B%0A   
- //
  private
@@ -1454,16 +1454,48 @@
 am is;%0A%0A
+    boolean connected = false;%0A%0A
     /*%0A 
@@ -2080,24 +2080,43 @@
 example%22 );%0A
+%0A        try %7B%0A    
         aa =
@@ -2131,24 +2131,136 @@
 yAnalyze();%0A
+            connected = true;%0A        %7D catch ( RuntimeException e ) %7B%0A            connected = false;%0A        %7D%0A
     %7D%0A%0A    /
@@ -2558,24 +2558,153 @@
 fyBatch() %7B%0A
+        if ( !connected ) %7B%0A            log.warn( %22Could not connect to RServe, skipping test.%22 );%0A            return;%0A        %7D%0A
         aa.A
@@ -2890,24 +2890,153 @@
 testRma() %7B%0A
+        if ( !connected ) %7B%0A            log.warn( %22Could not connect to RServe, skipping test.%22 );%0A            return;%0A        %7D%0A
         Name
@@ -3437,24 +3437,153 @@
 rmalize() %7B%0A
+        if ( !connected ) %7B%0A            log.warn( %22Could not connect to RServe, skipping test.%22 );%0A            return;%0A        %7D%0A
         aa.n
@@ -3749,32 +3749,32 @@
 esign)'%0A     */%0A
-
     public void 
@@ -3793,24 +3793,153 @@
 ndTreat() %7B%0A
+        if ( !connected ) %7B%0A            log.warn( %22Could not connect to RServe, skipping test.%22 );%0A            return;%0A        %7D%0A
         aa.b

make test conditional on server running