package bicat.run_machine;

import javax.swing.JOptionPane;

import bicat.gui.BicatGui;
import java.util.*;
import bicat.biclustering.Bicluster;
/**
 * <p>Title: BicAT Tool </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * @author Amela Prelic
 * @version 1.0
 *
 * Run Machine for Iterative Signature algorithm (2002).
 *
 * For its execution, a shared library is needed (lib/linux/libisa.so for Linux).
 *
 **/

public class RunMachine_AdditiveBi extends RunMachine {

  // ===========================================================================
  public RunMachine_AdditiveBi() { }
  public RunMachine_AdditiveBi(BicatGui o) { owner = o; }

  


// ===========================================================================
  public static void run_AdditiveBi(double[] data, double alpha, double beta,
                             double gamma,String reference_gene_type,String  reference_gene_parameter,String  reference_condition_type,String  reference_condition_parameter ,String out_file){

       String arr[]={owner.input_Data_file,Double.toString(alpha),Double.toString(beta), Double.toString(gamma), reference_gene_type ,reference_gene_parameter,reference_condition_type,reference_condition_parameter,out_file};
       MSBE.AdditiveBicluster.main(arr);
       System.out.println("constantBi loaded ");
 }

  
public static void adjust_MSBE_OutputBiclusters(LinkedList outputBiclusters)
{
    for(int i=0;i<outputBiclusters.size();i++)
    {
        
        for(int j=0;j<((Bicluster)outputBiclusters.get(i)).getGenes().length;j++)
        {
            ((Bicluster)outputBiclusters.get(i)).getGenes()[j]=((Bicluster)outputBiclusters.get(i)).getGenes()[j]-1;
        }
        
        for(int j=0;j<((Bicluster)outputBiclusters.get(i)).getChips().length;j++)
        {
            ((Bicluster)outputBiclusters.get(i)).getChips()[j]=((Bicluster)outputBiclusters.get(i)).getChips()[j]-1;
        }
        
    }
    
}

  
  public synchronized void runBiclustering(final Arguments_AdditiveBi additiveBia) {

    final SwingWorker worker = new SwingWorker() {

      public Object construct() {
   
             run_AdditiveBi(additiveBia.getData(), additiveBia.getAlpha(), additiveBia.getBeta(),
                additiveBia.getGamma(),additiveBia.getGene_refernce_Type()
                , additiveBia.getGene_ref_passed_parameter(),additiveBia.getCondition_refernce_Type(),additiveBia.getCondition_ref_passed_parameter(),additiveBia.getOutputFile());
         

        if(owner.debug) System.out.println("Started Additive Bicluster in a new thread!\n");

        return null;
      }

      public void finished() {

        System.out.println("Getting results from Additive Biclustering... " + owner.currentDirectoryPath+"/"+additiveBia.getOutputFile());

        
        // for the Constant Bi algorithm
        computedBiclusters = getOutputBiclusters_MSBE(owner.currentDirectoryPath+"/"+additiveBia.getOutputFile()); //cca.getOutputFile());
        
        //computedBiclusters = getOutputBiclusters(owner.currentDirectoryPath+"/"+constantBia.getOutputFile()); //cca.getOutputFile());
        outputBiclusters = computedBiclusters; // is a LinkedList of BitSet representated-BCs (bicat.runMachine.Bicluster)
        //transferList(); // not needed here

        // adjust the biclusters (genes ,chips)to start from index 0 not 1 
        //(special case for constantbi and additivebi )
        adjust_MSBE_OutputBiclusters(outputBiclusters);
        
        
        owner.finishUpRun(additiveBia.getDatasetIdx(), outputBiclusters, additiveBia.getPreprocessOptions(), "additiveBi");
       // JOptionPane.showMessageDialog(null, "Calculations finished.\nThe results can be found in the bicluster results section of the current dataset.");

      }
    };
    worker.start();
  }


}