code: package edu.cmu.tetrad.algcomparison.algorithm.cluster;
  
  import edu.cmu.tetrad.algcomparison.algorithm.Algorithm;
  import edu.cmu.tetrad.algcomparison.utils.HasKnowledge;
  import edu.cmu.tetrad.algcomparison.utils.TakesInitialGraph;
  import edu.cmu.tetrad.annotation.AlgType;
  import edu.cmu.tetrad.data.*;
  import edu.cmu.tetrad.graph.EdgeListGraph;
  import edu.cmu.tetrad.graph.Graph;
  import edu.cmu.tetrad.search.FindOneFactorClusters;
  import edu.cmu.tetrad.search.SearchGraphUtils;
  import edu.cmu.tetrad.search.TestType;
  import edu.cmu.tetrad.util.Parameters;
  import edu.pitt.dbmi.algo.resampling.GeneralResamplingTest;
  import edu.pitt.dbmi.algo.resampling.ResamplingEdgeEnsemble;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * FOFC.
   *
   * @author jdramsey
   */
  @edu.cmu.tetrad.annotation.Algorithm(
          name = "FOFC",
          command = "fofc",
          algoType = AlgType.search_for_structure_over_latents
  )
  public class Fofc implements Algorithm, TakesInitialGraph, HasKnowledge, ClusterAlgorithm {
  
      static final long serialVersionUID = 23L;
      private Graph initialGraph = null;
      private Algorithm algorithm = null;
      private IKnowledge knowledge = new Knowledge2();
  
      public Fofc() {
      }
  
      @Override
      public Graph search(DataModel dataSet, Parameters parameters) {
      	if (parameters.getInt("numberResampling") < 1) {
              ICovarianceMatrix cov = DataUtils.getCovMatrix(dataSet);
              double alpha = parameters.getDouble("alpha");
  
              boolean wishart = parameters.getBoolean("useWishart", true);
              TestType testType;
  
              if (wishart) {
                  testType = TestType.TETRAD_WISHART;
              } else {
                  testType = TestType.TETRAD_DELTA;
              }
  
              boolean gap = parameters.getBoolean("useGap", true);
              FindOneFactorClusters.Algorithm algorithm;
  
              if (gap) {
                  algorithm = FindOneFactorClusters.Algorithm.GAP;
              } else {
                  algorithm = FindOneFactorClusters.Algorithm.SAG;
              }
  
              edu.cmu.tetrad.search.FindOneFactorClusters search
                      = new edu.cmu.tetrad.search.FindOneFactorClusters(cov, testType, algorithm, alpha);
              search.setVerbose(parameters.getBoolean("verbose"));
  
              return search.search();
          } else {
              Fofc algorithm = new Fofc();
  
              //algorithm.setKnowledge(knowledge);
  //          if (initialGraph != null) {
  //      		algorithm.setInitialGraph(initialGraph);
  //  		}
  
              DataSet data = (DataSet) dataSet;
              GeneralResamplingTest search = new GeneralResamplingTest(data, algorithm, parameters.getInt("numberResampling"));
              search.setKnowledge(knowledge);
              
              search.setPercentResampleSize(parameters.getDouble("percentResampleSize"));
              search.setResamplingWithReplacement(parameters.getBoolean("resamplingWithReplacement"));
              
              ResamplingEdgeEnsemble edgeEnsemble = ResamplingEdgeEnsemble.Highest;
              switch (parameters.getInt("resamplingEnsemble", 1)) {
                  case 0:
                      edgeEnsemble = ResamplingEdgeEnsemble.Preserved;
                      break;
                  case 1:
                      edgeEnsemble = ResamplingEdgeEnsemble.Highest;
                      break;
                  case 2:
                      edgeEnsemble = ResamplingEdgeEnsemble.Majority;
              }
              search.setEdgeEnsemble(edgeEnsemble);
              search.setAddOriginalDataset(parameters.getBoolean("addOriginalDataset"));
              
              search.setParameters(parameters);
              search.setVerbose(parameters.getBoolean("verbose"));
              return search.search();
          }
      }
  
      @Override
      public Graph getComparisonGraph(Graph graph) {
          return SearchGraphUtils.patternForDag(new EdgeListGraph(graph));
      }
  
      @Override
      public String getDescription() {
          return "FOFC (Find One Factor Clusters)";
      }
  
      @Override
      public DataType getDataType() {
          return DataType.Continuous;
      }
  
      @Override
      public List<String> getParameters() {
          List<String> parameters = new ArrayList<>();
          parameters.add("alpha");
          parameters.add("useWishart");
          parameters.add("useGap");
          parameters.add("verbose");
          // Resampling
          parameters.add("numberResampling");
          parameters.add("percentResampleSize");
          parameters.add("resamplingWithReplacement");
          parameters.add("resamplingEnsemble");
          parameters.add("addOriginalDataset");
          return parameters;
      }
  
      @Override
      public IKnowledge getKnowledge() {
          return knowledge;
      }
  
      @Override
      public void setKnowledge(IKnowledge knowledge) {
          this.knowledge = knowledge;
      }
  
      @Override
      public Graph getInitialGraph() {
          return initialGraph;
      }
  
      @Override
      public void setInitialGraph(Graph initialGraph) {
          this.initialGraph = initialGraph;
      }
  
      @Override
      public void setInitialGraph(Algorithm algorithm) {
          this.algorithm = algorithm;
      }
  
  }
  