package medusa.georgios.ClusteringAlgorithms;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;






public class Clustering_NJ_UPGMA {
    NJ njclu;
    UPGMA upgmaclu;
    boolean NJ_selected=false;
    boolean UPGMA_selected=false;
    
    
    public static ArrayList labels ;
    float[][] Distance_Matrix;
            
    
    public String get_NewickFormat_UPGMA()
    {
        String str ="";
        if(UPGMA_selected==true && upgmaclu!=null){
         Clustering_NJ_UPGMA.labels=List2Graph.labels;   
         str =  upgmaclu.NewickFormat(upgmaclu.getRoot());
        int end = upgmaclu.NHX.length()-1;
        upgmaclu.NHX.delete(0,end);
       // Save_To_File(MainPackage.GraphInformation.codebase+"UPGMA_clustering.txt",str);
       // System.out.println("UPGMA Clustering saved in UPGMA_clustering.txt");
        }
        return str;

    }
    
    
      public String get_NewickFormat_UPGMA(String layer)
    {
        String str ="";
        if(UPGMA_selected==true && upgmaclu!=null){
         Clustering_NJ_UPGMA.labels=List2Graph.labels;   
         str =  upgmaclu.NewickFormat(upgmaclu.getRoot());
        int end = upgmaclu.NHX.length()-1;
        upgmaclu.NHX.delete(0,end);
       // Save_To_File(MainPackage.GraphInformation.codebase+layer+ " UPGMA_clustering.txt",str);
       // System.out.println("UPGMA Clustering saved in UPGMA_clustering.txt");
        }
        return str;

    }
    
    public String get_NewickFormat_NeighborJoining(String layer)
    {
        String str ="";
        if(NJ_selected==true && njclu!=null){
         Clustering_NJ_UPGMA.labels=List2Graph.labels; 
         str =  njclu.NewickFormat(njclu.getRoot());
        int end = njclu.NHX.length()-1;
        njclu.NHX.delete(0,end);
       // Save_To_File(MainPackage.GraphInformation.codebase+layer+" NJ_clustering.txt",str);
        }
      //  System.out.println("NJ Clustering saved in NJ_clustering.txt");
        return str; 
    }
     public String get_NewickFormat_NeighborJoining()
    {
        String str ="";
        if(NJ_selected==true && njclu!=null){
         Clustering_NJ_UPGMA.labels=List2Graph.labels;     
         str =  njclu.NewickFormat(njclu.getRoot());
        int end = njclu.NHX.length()-1;
        njclu.NHX.delete(0,end);
       // Save_To_File(MainPackage.GraphInformation.codebase+" NJ_clustering.txt",str);
        }
       // System.out.println("NJ Clustering saved in NJ_clustering.txt");
        return str; 
    }
    
    
     public void Save_To_File(String filename, String information)
     { 
     try {
        BufferedWriter outp = new BufferedWriter(new FileWriter(filename));  
         outp.append(information);
        outp.close();
        } catch (IOException e) {}
     
     }  

    
    public Clustering_NJ_UPGMA(float[][] distance_Matrix, ArrayList Leaf_Labels,int choose_clustering)
    {
      Distance_Matrix=distance_Matrix;
      labels = new ArrayList();
        if(choose_clustering==1)
       {  
        NJ_selected=false; 
        UPGMA_selected=true;
        upgmaclu = new UPGMA(Distance_Matrix);}
      
      if(choose_clustering==2)
      {
       NJ_selected=true; 
       UPGMA_selected=false; 
       njclu = new NJ(Distance_Matrix);}
      
      
      for (int i = 0; i < Leaf_Labels.size(); i++) 
          labels.add(Leaf_Labels.get(i));
    }
    
    

     
      
   void read_file_and_create_matrix(String filename)
    {
       
       List2Graph g = new List2Graph(filename);
       Distance_Matrix=g.A;

    }
      
      
      public void read_file_and_create_matrix2(String filename)
      {   
 
        float maximum=-22222;
        float infinite=9999999;
        StringBuffer x_p = new StringBuffer();
        StringBuffer y_p = new StringBuffer();
        StringBuffer val = new StringBuffer();
         
        
        int  so_far=1;
        String  thisLine;
        Hashtable labels_index = new Hashtable();
        int counter=0;
        System.out.println("Loading File "+ filename);
        try{
            FileInputStream fin =  new FileInputStream(filename);
            BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
            while ((thisLine = myInput.readLine()) != null) 
            {   
                StringTokenizer st = new StringTokenizer (thisLine,"\t");
                 x_p.append(st.nextToken());
                 y_p.append(st.nextToken());
                 val.append(st.nextToken());
                 String X=x_p.toString();
                 String Y=y_p.toString();
                 String Val=val.toString();
                if(labels.contains(X)==false){labels.add(X);labels_index.put(X,counter);counter++;} 
                if(labels.contains(Y)==false){labels.add(Y);labels_index.put(Y,counter);counter++;} 
                float value=Float.valueOf(val.toString());

                if(value>=maximum) maximum=value;
               
                if(so_far%100==0)
                {System.out.println(so_far);
                
               // System.out.println(so_far);
               
                so_far++;}
                x_p.delete(0,x_p.length());
                y_p.delete(0,y_p.length());
                val.delete(0,val.length());
            }
            fin.close(); 
            
        } catch (Exception e) {
            e.printStackTrace();
        }   


      infinite=maximum+1.0f;  
      
      Distance_Matrix=new float[labels.size()][labels.size()] ;
      for (int i = 0; i < Distance_Matrix.length; i++) {
          for (int j = 0; j < Distance_Matrix.length; j++) {
              Distance_Matrix[i][j]=infinite;
              if(i==j)Distance_Matrix[i][j]=0;
          }
      }
      
      so_far=1;
      System.out.println("Filling Matrix with values");
      try{ 
            FileInputStream fin =  new FileInputStream(filename);
            BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
            while ((thisLine = myInput.readLine()) != null) {
                 StringTokenizer st = new StringTokenizer (thisLine,"\t");
                 x_p.append(st.nextToken());
                 y_p.append(st.nextToken());
                 val.append(st.nextToken());
                 String X=x_p.toString();
                 String Y=y_p.toString();
                 String Val=val.toString();
                  
                          try {
                               int x=labels_index.get(X);
                               int y=labels_index.get(Y);
                               float weight = Float.valueOf(Val);
                               Distance_Matrix[x][y]=weight;
                               Distance_Matrix[y][x]=weight;
                              // System.out.println(x+" "+y+" "+weight);
                          } catch (Exception e){e.printStackTrace();}    
               x_p.delete(0,x_p.length());
                y_p.delete(0,y_p.length());
                val.delete(0,val.length());
            }fin.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }   
      
      
//       for (int i = 0; i < Distance_Matrix.length; i++){
//           for (int j = 0; j < Distance_Matrix.length; j++)
//               System.out.print(Distance_Matrix[i][j]+"\t");
//               System.out.println("");
//       }
   
        labels_index.clear();
      }
      
      
 
     
       public Clustering_NJ_UPGMA(String filename,int choose_clustering)
       {        
        System.out.println("Start Clustering"); 
       labels = new ArrayList();
        if(choose_clustering==1)
       {read_file_and_create_matrix( filename);
        NJ_selected=false; 
        UPGMA_selected=true;
        System.out.println("UPGMA clustering");
        upgmaclu = new UPGMA(Distance_Matrix);}
      
      if(choose_clustering==2)
      {read_file_and_create_matrix( filename);
       NJ_selected=true; 
       UPGMA_selected=false; 
        System.out.println("Neighbor Joining clustering");
       njclu = new NJ(Distance_Matrix);}
     
     System.out.println("Finished Clustering"); 
       }
       
       
        public Clustering_NJ_UPGMA(ArrayList list,int choose_clustering)
       {    

        System.out.println("Start Clustering"); 
        if(choose_clustering==1)//-----------------------UPGMA
       { List2Graph g = new List2Graph(list);
        Distance_Matrix=g.A;
        labels=g.labels;
        NJ_selected=false; 
        UPGMA_selected=true;
        System.out.println("UPGMA clustering");
        upgmaclu = new UPGMA(Distance_Matrix);}
      
      if(choose_clustering==2)//-----------------------NJ
      { List2Graph g = new List2Graph(list);
       Distance_Matrix=g.A;
       labels=g.labels;
       NJ_selected=true; 
       UPGMA_selected=false; 
        System.out.println("Neighbor Joining clustering");
       njclu = new NJ(Distance_Matrix);}
     
     System.out.println("Finished Clustering"); 
       }
      
    

     


  
  
  
}








 

Ads help maintain this website.