/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bicat.GO;

import bicat.gui.BicatGui;
import java.io.File;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author Hamed
 */
public class GOBean {
    
    private Vector comparedLists=null;
    private Vector externalLists = null;
    private Vector totalLists=null;
    private String goCategory = "";
    private double[] pValues = null;
    private boolean filterGeneNo = false;
    private boolean filterStudyFrac = false;
    private boolean filterPopFrac = false;
    private int minGeneNo = 0;
    private double minStudyFrac = 0;
    private double minPopFrac = 0;

    public Vector getComparedLists() {
        return comparedLists;
    }

    public void setComparedLists(Vector comparedLists) {
        this.comparedLists = comparedLists;
    }
    
    public Vector getExternalLists() {
        return externalLists;
    }

    public void setExtrenalLists(Vector extrenalLists) {
        this.externalLists = extrenalLists;
    }

    public String getGoCategory() {
        return goCategory;
    }

    public void setGoCategory(String goCategory) {
        this.goCategory = goCategory;
    }

    public double[] getPValues() {
        return pValues;
    }

    public void setPValues(double[] pValues) {
        this.pValues=pValues;
    }

    public boolean isFilterGeneNo() {
        return filterGeneNo;
    }

    public void setFilterGeneNo(boolean filterGeneNo) {
        this.filterGeneNo = filterGeneNo;
    }

    public boolean isFilterStudyFrac() {
        return filterStudyFrac;
    }

    public void setFilterStudyFrac(boolean filterStudyFrac) {
        this.filterStudyFrac = filterStudyFrac;
    }

    public boolean isFilterPopFrac() {
        return filterPopFrac;
    }

    public void setFilterPopFrac(boolean filterPopFrac) {
        this.filterPopFrac = filterPopFrac;
    }

    public int getMinGeneNo() {
        return minGeneNo;
    }

    public void setMinGeneNo(int minGeneNo) {
        this.minGeneNo = minGeneNo;
    }

    public double getMinStudyFrac() {
        return minStudyFrac;
    }

    public void setMinStudyFrac(double minStudyFrac) {
        this.minStudyFrac = minStudyFrac;
    }

    public double getMinPopFrac() {
        return minPopFrac;
    }

    public void setMinPopFrac(double minPopFrac) {
        this.minPopFrac = minPopFrac;
    }
 
  public Vector getTotalList() {
        return totalLists;
    }

    public void setTotalList(Vector totalList) {
        this.totalLists = totalList;
    }   
    
public void createGOFilesForExtrenalLists()
    {
    File dir=null;
    List list=null ; 
    String [] clusterFilePaths=null;
    File [] files=null;
    
    
    
    // get the cluster files for each 
    for(int i=0;i<externalLists.size();i++)
        {
            
            list= (List)externalLists.get(i);
            if(list.getGoFolderPath()==null || list.getGoFolderPath().equalsIgnoreCase(" ") || list.getGoFolderPath().equalsIgnoreCase(""))
            {
                // get the cluster files absolute pathes 
                dir =new File(list.getClusterFolderPath());
                files= dir.listFiles();
                clusterFilePaths=new String[files.length]; 
                for(int j=0;j<files.length;j++)
                {
                    clusterFilePaths[j]=files[j].getAbsolutePath();            
                }
                list.setClusterFilePaths(clusterFilePaths);
            }else{
                // set the go files paths
                dir =new File(list.getGoFolderPath());
                files= dir.listFiles();
                clusterFilePaths=new String[files.length]; 
                for(int j=0;j<files.length;j++)
                {
                    clusterFilePaths[j]=files[j].getAbsolutePath();            
                }
                list.setGoFilePaths(clusterFilePaths);
                list.setClusterFilePaths(clusterFilePaths);
               }
         }
    // get the gene merge for the external lists and save the out put files.
    GeneMergeForGO.createGeneMergeFiles_GoComapare_extLists(externalLists,goCategory);
   }
public void checkGOFilesForInternalLists()
    {
    List list=null ; 
    for(int i=0;i<comparedLists.size();i++)
      {
         list=(List)comparedLists.get(i);
         if( (goCategory.equalsIgnoreCase(GeneMergeForGO.GO_BIOLOGICAL_PROCESS) && list.isHasBP_goFiles()) ||
             (goCategory.equalsIgnoreCase(GeneMergeForGO.GO_MOLECULAR_FUNCTION) && list.isHasMF_goFiles()) || 
             (goCategory.equalsIgnoreCase(GeneMergeForGO.GO_CELLULAR_COMPONENT) && list.isHasCC_goFiles()))
         {
             // get the path of the go files
             BicatGui.setGoFilePathsForList(list);
         }else   // create gene merge files for the internal lists with the req go category 
         {   
            JOptionPane.showMessageDialog(null,"please select Biological process category, as the other categories still in development");  
         }
         
      }
        
        
    }

public void setTotalList()
{
    totalLists=new Vector();
    for(int i=0;i<comparedLists.size();i++)
    {
        totalLists.add((List)comparedLists.get(i));
    }
    for(int i=0;i<externalLists.size();i++)
    {
        totalLists.add((List)externalLists.get(i));
    }
}

public void  readFunctionalClassesForTotalList ()
{
    List list=null;
    Vector fc_AllPvalues=null;
    Vector fc_pvalue=null;
    
    double pvalue=0;
    LinkedList fClassList=null;
    
    for(int i=0;i<totalLists.size();i++)
    {
        list=(List)totalLists.get(i);
        fc_AllPvalues=new Vector();
        for(int j=0;j<pValues.length;j++)
        {
            pvalue=pValues[j];
            fc_pvalue=new Vector();
            for(int k=0;k<list.getGoFilePaths().length;k++)
            {
                fClassList= GoReader.getFunctionalClasses(list.getGoFilePaths()[k], pvalue);
                if(fClassList!=null &&  fClassList.size()>0) // condition of enrichement
                fc_pvalue.add(fClassList);
            }
            
           fc_AllPvalues.add(fc_pvalue); 
        }
        list.setFc_AllPvalues(fc_AllPvalues);
    }
   filterFunctionalClasses();
}
public void  readFunctionalClassesForTotalList_enhanced()
{
    List list=null;
    Vector fc_AllPvalues=null;
    Vector fc_pvalue=null;
    
    double pvalue=0;
    LinkedList allFClassList=null;
    LinkedList filteredFClassList=null;
    Vector allFcClassLists=new Vector();
    
    for(int i=0;i<totalLists.size();i++)
    {
        list=(List)totalLists.get(i);
        fc_AllPvalues=new Vector();
        allFcClassLists=new Vector();
        for(int j=0;j<list.getGoFilePaths().length;j++)
        {
            allFClassList=GoReader.getAllFunctionalClasses(list.getGoFilePaths()[j]);
            allFcClassLists.add(allFClassList);
        }   
        for(int k=0;k<pValues.length;k++)
        {
            pvalue=pValues[k];
            fc_pvalue=new Vector();
            for(int h=0;h<allFcClassLists.size();h++)
            {
                filteredFClassList=GoReader.filterFCListByPvalue((LinkedList)allFcClassLists.get(h), pvalue);
                if(filteredFClassList!=null &&  filteredFClassList.size()>0) // condition of enrichement
                 fc_pvalue.add(filteredFClassList);
            }
            fc_AllPvalues.add(fc_pvalue);  
        }
     list.setFc_AllPvalues(fc_AllPvalues);      
   }
  filterFunctionalClasses();
}

public void filterFunctionalClasses()
{
    if (isFilterGeneNo())
        filterByGeneNo();
    if(isFilterStudyFrac())
        filterByStudyFrac();
    if(isFilterPopFrac())
        filterByPopFrac();
}

public void filterByGeneNo()
{
    List list=null;
    Vector fc_pvalue=null;
    LinkedList fClassList=null;
    FunctionalClass fClass ;
    System.out.println("min Gene No >"+minGeneNo);
    for(int i=0;i<totalLists.size();i++)
    {
        list=(List)totalLists.get(i);
        
        for(int j=0;j<list.getFc_AllPvalues().size();j++)
        {
            fc_pvalue=(Vector)list.getFc_AllPvalues().get(j);
            for(int k=0;k<fc_pvalue.size();k++)
            {
                
                fClassList= (LinkedList)fc_pvalue.get(k);
                for(int h=0;h<fClassList.size();h++)
                {
                   fClass=(FunctionalClass)fClassList.get(h);
                   if(fClass.getContributed_Gene_No_In_Cluster() < minGeneNo )
                   {
                    fClassList.remove(h);
                    h=h-1;
                   }else
                   {
                       System.out.println("the passed from filter >>>"+fClass.getName()+"   "+ fClass.getContributed_Gene_No_In_Cluster());
                   }
                }
             }
        }

    }
}    

public void filterByStudyFrac()
{
    List list=null;
    Vector fc_pvalue=null;
    LinkedList fClassList=null;
    FunctionalClass fClass ;
    System.out.println("min Study frac>>>>"+minStudyFrac);
    for(int i=0;i<totalLists.size();i++)
    {
        list=(List)totalLists.get(i);
        
        for(int j=0;j<list.getFc_AllPvalues().size();j++)
        {
            fc_pvalue=(Vector)list.getFc_AllPvalues().get(j);
            for(int k=0;k<fc_pvalue.size();k++)
            {
                
                fClassList= (LinkedList)fc_pvalue.get(k);
                for(int h=0;h<fClassList.size();h++)
                {
                   fClass=(FunctionalClass)fClassList.get(h);
                   if(fClass.getStudy_Freq()< (minStudyFrac/100) )
                   {
                       fClassList.remove(h);
                       h=h-1;
                   }
                }
             }
        }

    }
}    
    
public void filterByPopFrac()
{
    List list=null;
    Vector fc_pvalue=null;
    LinkedList fClassList=null;
    FunctionalClass fClass ;
    double pop_freq=0;
    System.out.println("min pop frac>>>>"+minPopFrac);
    for(int i=0;i<totalLists.size();i++)
    {
        list=(List)totalLists.get(i);
        
        for(int j=0;j<list.getFc_AllPvalues().size();j++)
        {
            fc_pvalue=(Vector)list.getFc_AllPvalues().get(j);
            for(int k=0;k<fc_pvalue.size();k++)
            {
                
                fClassList= (LinkedList)fc_pvalue.get(k);
                for(int h=0;h<fClassList.size();h++)
                {
                   fClass=(FunctionalClass)fClassList.get(h);
                   pop_freq=fClass.getContributed_Gene_No_In_Cluster()*100/fClass.getContributed_Gene_No_In_Pop();
                   if(pop_freq < minPopFrac )
                       {
                           fClassList.remove(h);
                           h=h-1;
                       }
                }
             }
        }

    }
}        
    
 
}
