Exemple #1
0
 public static Node find(Node n, Object key, Comparator comparator) {
   while (n != Null) {
     int difference = Compare.compare(key, n.key, comparator);
     if (difference < 0) {
       n = n.left;
     } else if (difference > 0) {
       n = n.right;
     } else {
       return n;
     }
   }
   return Null;
 }
Exemple #2
0
  public static Node blaze(
      BlazeResult result,
      Object token,
      NodeStack stack,
      Node root,
      Object key,
      Comparator comparator) {
    if (key == null) throw new NullPointerException();

    if (root == null) {
      root = Null;
    }

    if (Debug) {
      token = new Object();
      validate(null, root);
    }

    stack = new NodeStack(stack);
    Node newRoot = getNode(token, root);

    Node old = root;
    Node new_ = newRoot;
    while (old != Null) {
      int difference = Compare.compare(key, old.key, comparator);
      if (difference < 0) {
        stack.push(new_);
        old = old.left;
        new_ = new_.left = getNode(token, old);
      } else if (difference > 0) {
        stack.push(new_);
        old = old.right;
        new_ = new_.right = getNode(token, old);
      } else {
        result.node = new_;
        stack.popStack();
        if (Debug) {
          validate(root, newRoot);
        }
        return newRoot;
      }
    }

    new_.key = key;
    result.node = new_;

    // rebalance
    new_.red = true;

    while (stack.top != null && stack.top.red) {
      if (stack.top == stack.peek().left) {
        if (stack.peek().right.red) {
          stack.top.red = false;
          stack.peek().right = getNode(token, stack.peek().right);
          stack.peek().right.red = false;
          stack.peek().red = true;
          new_ = stack.peek();
          stack.pop(2);
        } else {
          if (new_ == stack.top.right) {
            new_ = stack.top;
            stack.pop();

            Node n = leftRotate(token, new_);
            if (stack.top.right == new_) {
              stack.top.right = n;
            } else {
              stack.top.left = n;
            }
            stack.push(n);
          }
          stack.top.red = false;
          stack.peek().red = true;

          Node n = rightRotate(token, stack.peek());
          if (stack.index <= stack.base + 1) {
            newRoot = n;
          } else if (stack.peek(1).right == stack.peek()) {
            stack.peek(1).right = n;
          } else {
            stack.peek(1).left = n;
          }
          // done
        }
      } else {
        // this is just the above code with left and right swapped:
        if (stack.peek().left.red) {
          stack.top.red = false;
          stack.peek().left = getNode(token, stack.peek().left);
          stack.peek().left.red = false;
          stack.peek().red = true;
          new_ = stack.peek();
          stack.pop(2);
        } else {
          if (new_ == stack.top.left) {
            new_ = stack.top;
            stack.pop();

            Node n = rightRotate(token, new_);
            if (stack.top.right == new_) {
              stack.top.right = n;
            } else {
              stack.top.left = n;
            }
            stack.push(n);
          }
          stack.top.red = false;
          stack.peek().red = true;

          Node n = leftRotate(token, stack.peek());
          if (stack.index <= stack.base + 1) {
            newRoot = n;
          } else if (stack.peek(1).right == stack.peek()) {
            stack.peek(1).right = n;
          } else {
            stack.peek(1).left = n;
          }
          // done
        }
      }
    }

    newRoot.red = false;

    stack.popStack();
    if (Debug) {
      validate(root, newRoot);
    }
    return newRoot;
  }
Exemple #3
0
  public static Node delete(
      Object token, NodeStack stack, Node root, Object key, Comparator comparator) {
    if (Debug) {
      token = new Object();
      validate(null, root);
    }

    if (root == Null) {
      return root;
    } else if (root.left == Null && root.right == Null) {
      if (Compare.equal(key, root.key, comparator)) {
        return Null;
      } else {
        return root;
      }
    }

    stack = new NodeStack(stack);
    Node newRoot = getNode(token, root);

    Node old = root;
    Node new_ = newRoot;
    while (old != Null) {
      if (key == null) throw new NullPointerException();
      if (old.key == null) {
        throw new NullPointerException();
      }
      int difference = Compare.compare(key, old.key, comparator);
      if (difference < 0) {
        stack.push(new_);
        old = old.left;
        new_ = new_.left = getNode(token, old);
      } else if (difference > 0) {
        stack.push(new_);
        old = old.right;
        new_ = new_.right = getNode(token, old);
      } else {
        break;
      }
    }

    if (old == Null) {
      if (stack.top.left == new_) {
        stack.top.left = Null;
      } else {
        stack.top.right = Null;
      }
      stack.popStack();
      return root;
    }

    Node dead;
    if (new_.left == Null || new_.right == Null) {
      dead = new_;
    } else {
      successor(token, new_, stack);
      dead = stack.top;
      stack.pop();
    }

    Node child;
    if (dead.left != Null) {
      child = getNode(token, dead.left);
    } else if (dead.right != Null) {
      child = getNode(token, dead.right);
    } else {
      child = Null;
    }

    if (stack.top == null) {
      child.red = false;
      stack.popStack();
      if (Debug) {
        validate(root, child);
      }
      return child;
    } else if (dead == stack.top.left) {
      stack.top.left = child;
    } else {
      stack.top.right = child;
    }

    if (dead != new_) {
      new_.key = dead.key;
      new_.value = dead.value;
    }

    if (!dead.red) {
      // rebalance
      while (stack.top != null && !child.red) {
        if (Debug) checkArgument(stack.top.token == token);
        if (child == stack.top.left) {
          Node sibling = stack.top.right = getNode(token, stack.top.right);
          if (sibling.red) {
            if (Debug) checkArgument(sibling.token == token);
            sibling.red = false;
            stack.top.red = true;

            Node n = leftRotate(token, stack.top);
            if (stack.index == stack.base) {
              newRoot = n;
            } else if (stack.peek().right == stack.top) {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().right = n;
            } else {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().left = n;
            }
            Node parent = stack.top;
            stack.top = n;
            stack.push(parent);

            sibling = stack.top.right = getNode(token, stack.top.right);
          }

          if (!(sibling.left.red || sibling.right.red)) {
            if (Debug) checkArgument(sibling.token == token);
            sibling.red = true;
            child = stack.top;
            stack.pop();
          } else {
            if (!sibling.right.red) {
              sibling.left = getNode(token, sibling.left);
              sibling.left.red = false;

              if (Debug) checkArgument(sibling.token == token);
              sibling.red = true;
              sibling = stack.top.right = rightRotate(token, sibling);
            }

            if (Debug) checkArgument(sibling.token == token);
            sibling.red = stack.top.red;
            stack.top.red = false;

            sibling.right = getNode(token, sibling.right);
            sibling.right.red = false;

            Node n = leftRotate(token, stack.top);
            if (stack.index == stack.base) {
              newRoot = n;
            } else if (stack.peek().right == stack.top) {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().right = n;
            } else {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().left = n;
            }

            child = newRoot;
            stack.clear();
          }
        } else {
          // this is just the above code with left and right swapped:
          Node sibling = stack.top.left = getNode(token, stack.top.left);
          if (sibling.red) {
            if (Debug) checkArgument(sibling.token == token);
            sibling.red = false;
            stack.top.red = true;

            Node n = rightRotate(token, stack.top);
            if (stack.index == stack.base) {
              newRoot = n;
            } else if (stack.peek().left == stack.top) {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().left = n;
            } else {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().right = n;
            }
            Node parent = stack.top;
            stack.top = n;
            stack.push(parent);

            sibling = stack.top.left = getNode(token, stack.top.left);
          }

          if (!(sibling.right.red || sibling.left.red)) {
            if (Debug) checkArgument(sibling.token == token);
            sibling.red = true;
            child = stack.top;
            stack.pop();
          } else {
            if (!sibling.left.red) {
              sibling.right = getNode(token, sibling.right);
              sibling.right.red = false;

              if (Debug) checkArgument(sibling.token == token);
              sibling.red = true;
              sibling = stack.top.left = leftRotate(token, sibling);
            }

            if (Debug) checkArgument(sibling.token == token);
            sibling.red = stack.top.red;
            stack.top.red = false;

            sibling.left = getNode(token, sibling.left);
            sibling.left.red = false;

            Node n = rightRotate(token, stack.top);
            if (stack.index == stack.base) {
              newRoot = n;
            } else if (stack.peek().left == stack.top) {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().left = n;
            } else {
              if (Debug) checkArgument(stack.peek().token == token);
              stack.peek().right = n;
            }

            child = newRoot;
            stack.clear();
          }
        }
      }

      if (Debug) checkArgument(child.token == token);
      child.red = false;
    }

    stack.popStack();
    if (Debug) {
      validate(root, newRoot);
    }
    return newRoot;
  }
 public void start(String[] args) {
   parseargs(args);
   if (printhelp) {
     System.out.println(
         "LB-Impute is a program for imputing missing data and correcting erroneous data in biallelic populations. Command line options are as follows:");
     System.out.println(
         "-method	<impute, compare, randomremove>	Determines what operation LB-Impute will undertake. REQUIRED.\n");
     System.out.println();
     System.out.println();
     System.out.println("IMPUTE OPTIONS\n");
     System.out.println("-f	<filename>	VCF input file name. REQUIRED.");
     System.out.println("-o	<filename>	Output file name. REQUIRED.");
     System.out.println(
         "-parents	<parentnames>	Names of parental samples separated by comma. NO SPACE BETWEEN COMMA AND NAMES. Names with spaces or commas will not work properly. REQUIRED.");
     System.out.println("-offspringimpute		LB-Impute will impute offspring.");
     System.out.println("-parentimpute		Default. LB-Impute will impute parents.");
     System.out.println(
         "-readerr	<double>	Default value is 0.05. Probability that any given read is erroneous.");
     System.out.println(
         "-genotypeerr	<double>	Default value is 0.05. Probability that any given genotype call is erroneous.");
     System.out.println(
         "-recombdist	<integer>	Default value is 10,000,000. Expected distance of 50cM.");
     System.out.println(
         "-resolveconflicts		Default is off. When on, it will use the path with the highest probability to resolve a conflict rather than leaving a conflicting genotype empty. May reduce accuracy.");
     System.out.println(
         "-dr 		Default is off. When on, a homozygous to homozygous recombination event (double recombination) will have the same probability as a single event. This may be useful for inbred populations such as NAMs.\n");
     System.out.println(
         "-minsamples	<integer>	Default is 5. Minimum number of imputed samples required to infer a parental genotype. Has no function when imputing offspring.\n");
     System.out.println(
         "-minfraction	<double>	Default is 0.5. For a genotype at a given locus to be assigned to a parent, that genotype must be at least this fraction of the total genotypes of offspring assigned to that parent. This value should be somewhat low for low coverage populations, as false homozygotes will be confounding.");
     System.out.println(
         "-window	<integer>	Default is 7. Window length of the Vitterbi trellis. Longer windows produce more accurate results, but have longer runtimes. It is recommended to keep this between 5 and 7, though 2 is the minimum.");
     System.out.println();
     System.out.println();
     System.out.println();
     System.out.println("RANDOMREMOVE OPTIONS\n");
     System.out.println(
         "Random remove removes calls at a specific level or range of coverage to serve as a validation set. When used along with compare, it can validate imputation on a dataset. Prints new dataset to stdout.");
     System.out.println();
     System.out.println("-f	<filename>	Name of VCF file. REQUIRED.");
     System.out.println(
         "-parents	<parentnames>	Names of parental samples separated by comma. NO SPACE BETWEEN COMMA AND NAMES. Names with spaces or commas will not work properly. REQUIRED.");
     System.out.println(
         "-removefraction	<double>	Fraction of calls fitting criteria to be removed.");
     System.out.println(
         "-minhomcov	<int>	Minimum coverage of homozygous calls to be removed. Inclusive. REQUIRED.");
     System.out.println(
         "-maxhomcov	<int>	Maximum coverage of homozygous calls to be removed. Inclusive. REQUIRED.");
     System.out.println(
         "-minhetcov	<int>	Minimum coverage of heterozygous calls to be removed. Inclusive. REQUIRED.");
     System.out.println(
         "-maxhetcov	<int>	Maximum coverage of heterozygous calls to be removed. Inclusive. REQUIRED.");
     System.out.println();
     System.out.println();
     System.out.println("COMPARE OPTIONS");
     System.out.println(
         "Compare compares the original file with an imputed file and a file with a validation set removed then outputs statistics to stdout.");
     System.out.println("-mainfile	<filename>	Name of original file. REQUIRED.");
     System.out.println("-imputefile	<filename>	Name of imputed file. REQUIRED.");
     System.out.println(
         "-missfile	<filename>	Name of file with validation set removed. REQUIRED.");
     System.out.println("-offspringcompare		Compares offspring rather than parental calls.");
     System.out.println(
         "-parents	<parentnames>	Names of parental samples separated by comma. NO SPACE BETWEEN COMMA AND NAMES. Names with spaces or commas will not work properly. REQUIRED.");
     System.exit(0);
   }
   if (method.equals(
       "compare")) { // this is the algorithm for accuracy metrics. Requires a file with correct
     // genotypes, erroneous or missing genotypes, and imputed genotypes.
     Compare compare = new Compare();
     compare.compare(args);
   }
   if (method.equals("randomremove")) { // I don't think this is used anymore.
     RemoveValues randomremove = new RemoveValues();
     randomremove.remove(args);
   }
   if (method.equals("impute")) { // this is the primary algorithm for imputation.
     Impute impute = new Impute();
     impute.impute(args);
   }
 }
Exemple #5
0
 public void compare(ObjectContainer con, Object obj, int ver) {
   Compare.compare(con, set(newInstance(), ver), obj, "", null);
 }
Exemple #6
0
  /** TwoGtp main function. */
  public static void main(String[] args) {
    boolean exitError = false;
    try {
      String options[] = {
        "alternate",
        "analyze:",
        "auto",
        "black:",
        "compare",
        "config:",
        "debugtocomment",
        "force",
        "games:",
        "help",
        "komi:",
        "maxmoves:",
        "observer:",
        "openings:",
        "referee:",
        "sgffile:",
        "size:",
        "threads:",
        "time:",
        "verbose",
        "version",
        "white:",
        "xml"
      };
      Options opt = Options.parse(args, options);
      opt.checkNoArguments();
      if (opt.contains("help")) {
        String helpText =
            "Usage: gogui-twogtp [options]\n"
                + "\n"
                + "-alternate      alternate colors\n"
                + "-analyze file   analyze result file\n"
                + "-auto           autoplay games\n"
                + "-black          command for black program\n"
                + "-compare        compare list of sgf files\n"
                + "-config         config file\n"
                + "-debugtocomment save stderr of programs in SGF comments\n"
                + "-force          overwrite existing files\n"
                + "-games          number of games (0=unlimited)\n"
                + "-help           display this help and exit\n"
                + "-komi           komi\n"
                + "-maxmoves       move limit\n"
                + "-observer       command for observer program\n"
                + "-openings       directory with opening sgf files\n"
                + "-referee        command for referee program\n"
                + "-sgffile        filename prefix\n"
                + "-size           board size for autoplay (default 19)\n"
                + "-threads n      number of threads\n"
                + "-time spec      set time limits (min[+min/moves])\n"
                + "-verbose        log GTP streams to stderr\n"
                + "-version        print version and exit\n"
                + "-white          command for white program\n"
                + "-xml            save games in XML format\n";
        System.out.print(helpText);
        System.exit(0);
      }
      boolean compare = opt.contains("compare");
      if (compare) {
        Compare.compare(opt.getArguments());
        System.exit(0);
      }
      if (opt.contains("version")) {
        System.out.println("gogui-twogtp " + "XXX");
        System.exit(0);
      }
      boolean force = opt.contains("force");
      if (opt.contains("analyze")) {
        String filename = opt.get("analyze");
        new Analyze(filename, force);
        return;
      }
      boolean alternate = opt.contains("alternate");
      boolean auto = opt.contains("auto");
      boolean debugToComment = opt.contains("debugtocomment");
      boolean verbose = opt.contains("verbose");
      String black = opt.get("black", "");
      if (black.equals("")) throw new ErrorMessage("No black program set");
      String white = opt.get("white", "");
      if (white.equals("")) throw new ErrorMessage("No white program set");
      String referee = opt.get("referee", "");
      String observer = opt.get("observer", "");
      int size = opt.getInteger("size", GoPoint.DEFAULT_SIZE, 1, GoPoint.MAX_SIZE);
      Komi komi = new Komi(6.5);
      if (opt.contains("komi")) komi = Komi.parseKomi(opt.get("komi"));
      int maxMoves = opt.getInteger("maxmoves", 1000, -1);
      TimeSettings timeSettings = null;
      if (opt.contains("time")) timeSettings = TimeSettings.parse(opt.get("time"));
      int defaultGames = (auto ? 1 : 0);
      int numberGames = opt.getInteger("games", defaultGames, 0);
      int numberThreads = opt.getInteger("threads", 1, 1);
      if (numberThreads > 1 && !auto) throw new ErrorMessage("Option -threads needs option -auto");
      String sgfFile = opt.get("sgffile", "");
      if (opt.contains("games") && sgfFile.equals(""))
        throw new ErrorMessage("Use option -sgffile with -games");
      Openings openings = null;
      if (opt.contains("openings")) openings = new Openings(new File(opt.get("openings")));
      boolean useXml = opt.contains("xml");
      if (auto) System.in.close();

      TwoGtp twoGtp[] = new TwoGtp[numberThreads];
      TwoGtpThread thread[] = new TwoGtpThread[numberThreads];
      ResultFile resultFile = null;
      for (int i = 0; i < numberThreads; ++i) {
        ArrayList<Program> allPrograms = new ArrayList<Program>();
        Program blackProgram = new Program(black, "Black", "B", verbose);
        allPrograms.add(blackProgram);
        Program whiteProgram = new Program(white, "White", "W", verbose);
        allPrograms.add(whiteProgram);
        Program refereeProgram;
        if (referee.equals("")) refereeProgram = null;
        else {
          refereeProgram = new Program(referee, "Referee", "R", verbose);
          allPrograms.add(refereeProgram);
        }
        for (Program program : allPrograms) program.setLabel(allPrograms);
        if (!sgfFile.equals("") && resultFile == null)
          resultFile =
              new ResultFile(
                  force,
                  blackProgram,
                  whiteProgram,
                  refereeProgram,
                  numberGames,
                  size,
                  komi,
                  sgfFile,
                  openings,
                  alternate,
                  useXml,
                  numberThreads);
        if (i > 0) verbose = false;
        twoGtp[i] =
            new TwoGtp(
                blackProgram,
                whiteProgram,
                refereeProgram,
                observer,
                size,
                komi,
                numberGames,
                alternate,
                sgfFile,
                verbose,
                openings,
                timeSettings,
                resultFile);
        twoGtp[i].setMaxMoves(maxMoves);
        if (debugToComment) twoGtp[i].setDebugToComment(true);
        if (auto) {
          thread[i] = new TwoGtpThread(twoGtp[i]);
          thread[i].start();
        }
      }
      if (auto) {
        for (int i = 0; i < numberThreads; ++i) thread[i].join();
        for (int i = 0; i < numberThreads; ++i)
          if (thread[i].getException() != null) {
            StringUtil.printException(thread[i].getException());
            exitError = true;
          }
      } else twoGtp[0].mainLoop(System.in, System.out);
      if (resultFile != null) resultFile.close();
    } catch (Throwable t) {
      StringUtil.printException(t);
      exitError = true;
    }
    if (exitError) System.exit(1);
  }