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_ConstantBi extends RunMachine {

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

  




// ===========================================================================
  public static void run_ConstantBi(double[] data, double alpha, double beta,
                             double gamma,String reference_gene_type,String reference_Gene_passed_parameter,
                             String out_file){

  
      
      
       String arr[]={owner.input_Data_file,Double.toString(alpha),Double.toString(beta), Double.toString(gamma), reference_gene_type ,reference_Gene_passed_parameter,out_file};
       MSBE.ConstantBicluster.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_ConstantBi constantBia) {

    final SwingWorker worker = new SwingWorker() {

      public Object construct() {

       
                   
                run_ConstantBi(constantBia.getData(), constantBia.getAlpha(), constantBia.getBeta(),
                constantBia.getGamma(),constantBia.getGene_refernce_Type(), constantBia.getGene_ref_passed_parameter()
                ,constantBia.getOutputFile());
          
        if(owner.debug) System.out.println("Started Constant Bicluster in a new thread!\n");

        return null;
      }

      public void finished() {

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

        
        // for the Constant Bi algorithm
        computedBiclusters = getOutputBiclusters_MSBE(owner.currentDirectoryPath+"/"+constantBia.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(constantBia.getDatasetIdx(), outputBiclusters, constantBia.getPreprocessOptions(), "constantBi");
       // JOptionPane.showMessageDialog(null, "Calculations finished.\nThe results can be found in the bicluster results section of the current dataset.");

      }
    };
    worker.start();
  }


}