public boolean isIndependent(Node x, Node y, List<Node> z) {
    int[] all = new int[z.size() + 2];
    all[0] = variablesMap.get(x);
    all[1] = variablesMap.get(y);
    for (int i = 0; i < z.size(); i++) {
      all[i + 2] = variablesMap.get(z.get(i));
    }

    int sampleSize = data.get(0).rows();
    List<Double> pValues = new ArrayList<Double>();

    for (int m = 0; m < ncov.size(); m++) {
      TetradMatrix _ncov = ncov.get(m).getSelection(all, all);
      TetradMatrix inv = _ncov.inverse();
      double r = -inv.get(0, 1) / sqrt(inv.get(0, 0) * inv.get(1, 1));

      double fisherZ =
          sqrt(sampleSize - z.size() - 3.0) * 0.5 * (Math.log(1.0 + r) - Math.log(1.0 - r));
      double pValue;

      if (Double.isInfinite(fisherZ)) {
        pValue = 0;
      } else {
        pValue = 2.0 * (1.0 - RandomUtil.getInstance().normalCdf(0, 1, abs(fisherZ)));
      }

      pValues.add(pValue);
    }

    double _cutoff = alpha;

    if (fdr) {
      _cutoff = StatUtils.fdrCutoff(alpha, pValues, false);
    }

    Collections.sort(pValues);
    int index = (int) round((1.0 - percent) * pValues.size());
    this.pValue = pValues.get(index);

    //        if (this.pValue == 0) {
    //            System.out.println("Zero pvalue "+ SearchLogUtils.independenceFactMsg(x, y, z,
    // getPValue()));
    //        }

    boolean independent = this.pValue > _cutoff;

    if (verbose) {
      if (independent) {
        TetradLogger.getInstance()
            .log("independencies", SearchLogUtils.independenceFactMsg(x, y, z, getPValue()));
        //            System.out.println(SearchLogUtils.independenceFactMsg(x, y, z, getPValue()));
      } else {
        TetradLogger.getInstance()
            .log("dependencies", SearchLogUtils.dependenceFactMsg(x, y, z, getPValue()));
      }
    }

    return independent;
  }
示例#2
0
 public int[] getPositionsWithinDistanceToPredecessor(double distance) {
   List<Integer> result = new ArrayList<>();
   List<P> positions = getPositions();
   if (positions.size() <= 2) return new int[0];
   P previous = positions.get(0);
   for (int i = 1; i < positions.size() - 1; i++) {
     P next = positions.get(i);
     if (!next.hasCoordinates() || next.calculateDistance(previous) <= distance) result.add(i);
     else previous = next;
   }
   return toArray(result);
 }
  private static void runSimulationExecutive(
      int sim_duration,
      int simulation_time_interval,
      List<Cell> cell_population,
      int cell_doubling_rate) {
    int population_size = cell_population.size();
    int next_div_time = cell_doubling_rate;
    int[][][] diploid_genome =
        new int[haploid_number][2]
            [2]; // The genome, chromosome->homologous pair->complementary DNA strands

    // Progress through time at selected intervals for a specified duration
    for (int current_time = 0;
        current_time < sim_duration;
        current_time += simulation_time_interval) {
      Random rand = new Random();
      // Perform division of all cells that can divide as determined by a random number generator
      // and the set division threshhold
      // As new cells are being added to the list, only go through the cells in the population
      // before new cells are added i.e use the previous population size as max number of iterations
      for (int counter = 0; counter < population_size; counter++) {
        if (cell_population.get(counter).getDivisionStatus()) {
          // Random number generator, 0 = divide, 1 = don't divide

          double coin_flip = rand.nextDouble();
          // printString(Double.toString(coin_flip));
          if (coin_flip >= 0.5) {
            // Remove one cell from the current generation and add two to the next
            // by changing the generation of the dividing cell to current_gen + 1 and
            // create a new cell object for the next generation
            cell_population.get(counter).setGen(cell_population.get(counter).getGen() + 1);
            cell_population.add(
                new Cell(
                    id_of_last_created_cell + 1, newest_generation + 1, -1, true, diploid_genome));
            id_of_last_created_cell++;
          }
        } // if the cell can divide
      } // for all items in main population array

      population_size =
          cell_population.size(); // Update the population size after the round of division
      newest_generation++; // Update the value of the most recent generation after the division
      // round
      next_div_time +=
          cell_doubling_rate; // Set the next time of division by adding the doubling rate to the
      // current time

      printString(current_time + " " + population_size);
    } // for
  } // runSimulationExecutive
  public IndTestFisherZPercentIndependent(List<DataSet> dataSets, double alpha) {
    this.dataSets = dataSets;
    this.variables = dataSets.get(0).getVariables();

    data = new ArrayList<TetradMatrix>();

    for (DataSet dataSet : dataSets) {
      dataSet = DataUtils.center(dataSet);
      TetradMatrix _data = dataSet.getDoubleData();
      data.add(_data);
    }

    ncov = new ArrayList<TetradMatrix>();
    for (TetradMatrix d : this.data) ncov.add(d.transpose().times(d).scalarMult(1.0 / d.rows()));

    setAlpha(alpha);
    rows = new int[dataSets.get(0).getNumRows()];
    for (int i = 0; i < getRows().length; i++) getRows()[i] = i;

    variablesMap = new HashMap<Node, Integer>();
    for (int i = 0; i < variables.size(); i++) {
      variablesMap.put(variables.get(i), i);
    }

    this.recursivePartialCorrelation = new ArrayList<RecursivePartialCorrelation>();
    for (TetradMatrix covMatrix : ncov) {
      recursivePartialCorrelation.add(new RecursivePartialCorrelation(getVariables(), covMatrix));
    }
  }
示例#5
0
  public void ensureIncreasingTime() {
    if (getPositionCount() < 2) return;

    long completeTime = getTime(); // ms
    double completeDistance = getDistance(); // m
    double averageSpeed = completeTime > 0 ? completeDistance / completeTime * 1000 : 1.0; // m/s

    List<P> positions = getPositions();
    P first = positions.get(0);
    if (!first.hasTime()) first.setTime(fromCalendar(Calendar.getInstance(UTC)));

    P previous = first;
    for (int i = 1; i < positions.size(); i++) {
      P next = positions.get(i);
      CompactCalendar time = next.getTime();
      if (time == null || time.equals(previous.getTime())) {
        Double distance = next.calculateDistance(previous);
        Long millis = distance != null ? (long) (distance / averageSpeed * 1000) : null;
        if (millis == null || millis < 1000) millis = 1000L;
        next.setTime(
            fromMillisAndTimeZone(
                previous.getTime().getTimeInMillis() + millis, previous.getTime().getTimeZoneId()));
      }
      previous = next;
    }
  }
  public void insertSimple(int val, Node ptr, int i) {
    // declare two lists to store keys and ptrs
    List<Integer> tempKeys = new ArrayList<Integer>();
    List<Node> tempPtrs = new ArrayList<Node>();

    // store keys and ptrs into list
    for (int j = i; j <= this.getLast(); j++) {
      tempKeys.add(this.getKey(j));
      tempPtrs.add(this.getPtr(j));
    }

    // set keys and ptrs at position i
    this.keys[i] = val;
    this.ptrs[i] = ptr;
    // set parent reference
    this.ptrs[i].setParent(new Reference(this, i, false));

    int end = tempKeys.size();
    // add keys and ptrs to current node from previous lists
    for (int j = i + 1; j <= i + end; j++) {
      this.keys[j] = tempKeys.get(j - i - 1);
      this.ptrs[j] = tempPtrs.get(j - i - 1);
      // set parent reference
      this.ptrs[j].setParent(new Reference(this, j, false));
    }

    // increase lastindex because of insertion
    this.lastindex++;
  }
示例#7
0
 public void sort(Comparator<P> comparator) {
   List<P> positions = getPositions();
   @SuppressWarnings({"SuspiciousToArrayCall", "unchecked"})
   P[] sorted = (P[]) positions.toArray(new BaseNavigationPosition[positions.size()]);
   Arrays.sort(sorted, comparator);
   //noinspection unchecked
   order(asList(sorted));
 }
示例#8
0
 public int[] getContainedPositions(BoundingBox boundingBox) {
   List<Integer> result = new ArrayList<>();
   List<P> positions = getPositions();
   for (int i = 0; i < positions.size(); i++) {
     P position = positions.get(i);
     if (position.hasCoordinates() && boundingBox.contains(position)) result.add(i);
   }
   return toArray(result);
 }
示例#9
0
 public double getElevationDelta(int index) {
   List<P> positions = getPositions();
   NavigationPosition previous = index > 0 ? positions.get(index - 1) : null;
   NavigationPosition current = index < positions.size() ? positions.get(index) : null;
   if (previous != null && current != null) {
     Double elevation = previous.calculateElevation(current);
     if (elevation != null) return elevation;
   }
   return 0;
 }
示例#10
0
 /**
  * Removes duplicate adjacent {@link #getPositions() positions} from this route, leaving only
  * distinct neighbours
  */
 public void removeDuplicates() {
   List<P> positions = getPositions();
   P previous = null;
   int index = 0;
   while (index < positions.size()) {
     P next = positions.get(index);
     if (previous != null && (!next.hasCoordinates() || next.calculateDistance(previous) <= 0.0)) {
       positions.remove(index);
     } else index++;
     previous = next;
   }
 }
示例#11
0
  public int getClosestPosition(double longitude, double latitude, double threshold) {
    int closestIndex = -1;
    double closestDistance = MAX_VALUE;

    List<P> positions = getPositions();
    for (int i = 0; i < positions.size(); ++i) {
      P point = positions.get(i);
      Double distance = point.calculateDistance(longitude, latitude);
      if (distance != null && distance < closestDistance && distance < threshold) {
        closestDistance = distance;
        closestIndex = i;
      }
    }
    return closestIndex;
  }
示例#12
0
 public double[] getDistancesFromStart(int startIndex, int endIndex) {
   double[] result = new double[endIndex - startIndex + 1];
   List<P> positions = getPositions();
   int index = 0;
   double distance = 0.0;
   NavigationPosition previous = positions.size() > 0 ? positions.get(0) : null;
   while (index <= endIndex) {
     NavigationPosition next = positions.get(index);
     if (previous != null) {
       Double delta = previous.calculateDistance(next);
       if (delta != null) distance += delta;
       if (index >= startIndex) result[index - startIndex] = distance;
     }
     index++;
     previous = next;
   }
   return result;
 }
示例#13
0
  public void solve() throws Exception {
    P[] ps = new P[N];
    Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
    for (int i = 0; i < N; i++) {
      P p = new P();
      p.x = sc.nextInt();
      p.y = sc.nextInt();
      ps[i] = p;
      if (!map.containsKey(p.x)) {
        Set<Integer> set = new HashSet<Integer>();
        map.put(p.x, set);
      }
      map.get(p.x).add(p.y);
    }
    Arrays.sort(
        ps,
        new Comparator<P>() {
          @Override
          public int compare(P p1, P p2) {
            if (p1.x != p2.x) {
              return p1.x - p2.x;
            }
            return p1.y - p2.y;
          }
        });

    List<Pair> yp = new ArrayList<Pair>();
    for (int i = 0; i < ps.length - 1; i++) {
      if (ps[i].x != ps[i + 1].x) continue;
      int lidx = i + 1;
      while (lidx + 1 < ps.length && ps[i].x == ps[lidx + 1].x) {
        lidx++;
      }
      Pair pair = new Pair();
      pair.s = ps[i].y;
      pair.l = ps[lidx].y;
      pair.base = ps[i].x;
      yp.add(pair);
      i = lidx;
    }

    Arrays.sort(
        ps,
        new Comparator<P>() {
          @Override
          public int compare(P p1, P p2) {
            if (p1.y != p2.y) {
              return p1.y - p2.y;
            }
            return p1.x - p2.x;
          }
        });
    List<Pair> xp = new ArrayList<Pair>();
    for (int i = 0; i < ps.length - 1; i++) {
      if (ps[i].y != ps[i + 1].y) continue;
      int lidx = i + 1;
      while (lidx + 1 < ps.length && ps[i].y == ps[lidx + 1].y) {
        lidx++;
      }
      Pair pair = new Pair();
      pair.s = ps[i].x;
      pair.l = ps[lidx].x;
      pair.base = ps[i].y;
      xp.add(pair);
      i = lidx;
    }

    int ans = 0;
    for (int i = 0; i < yp.size(); i++) {
      int xnow = yp.get(i).base;
      int sy = yp.get(i).s;
      int ly = yp.get(i).l;
      for (int j = 0; j < xp.size(); j++) {
        int y = xp.get(j).base;
        if (y < sy || ly < y) continue;
        if (xp.get(j).s > xnow || xp.get(j).l < xnow) continue;
        try {
          if (!map.get(xnow).contains(y)) {
            ans++;
            map.get(xnow).add(y);
          }
        } catch (Exception ex) {

        }
      }
    }
    ans += ps.length;
    out.println(ans);
  }
  private static List<String> importGenomeData(
      File genome_text_file, String target_organism, int sex) throws IOException {

    List<String> temp_genome_data =
        new ArrayList<String>(); // The genome data array that contains the sizes of each chromosome
    // int haploid_number; // Used to determine the size of the first dimension in the
    // temp_genome_data array
    boolean found_target_organism =
        false; // Used to determine what action to take when a new header line in the file is found;
    // close if true, keep reading if false
    boolean end_of_genome = false; // True when the Y chromosome has been dealt with

    // Construct BufferedReader from FileReader; search for header line of target organism and
    // obtain the haploid number then create a two dimensional array, size of the first dimension
    // equals the haploid number, size of second dimension equals two (two chromosomes to form
    // deploid organism)
    // At this point, the next lines correspond to the size of each chromosome so populate the newly
    // created array
    // with this information. Stop reading when no more new lines or when a new header line is found
    BufferedReader genome_file_reader = new BufferedReader(new FileReader(genome_text_file));

    String line = null;
    StringBuilder organism_name;
    while ((line = genome_file_reader.readLine()) != null) {
      String[] split_line = line.split(" ");

      if (split_line[0].equals(">")) // If this is a first header line
      {
        organism_name =
            new StringBuilder()
                .append(split_line[1])
                .append(" ")
                .append(
                    split_line[
                        2]); // Recreate the genus and species of the organism from the strings that
        // were separated during the splitting of the line
        if (organism_name
            .toString()
            .equals(target_organism)) // If this line refers to the organism of interest
        {
          found_target_organism = true;
          haploid_number =
              Integer.parseInt(split_line[4]); // Get the haploid number stored in the header line
        } else {
          found_target_organism = false;
          continue; // This is a header line but it is not the organism of interest
        }
      } else if (found_target_organism
          && !end_of_genome) // This is not a header line and we probably want to import this line
      {
        // boolean autosome = true;
        switch (sex) {
          case 1: // Female
            {
              if (split_line[0].equals("chrX")) {
                temp_genome_data.add(split_line[1] + "," + split_line[1]);
              } else if (split_line[0].equals("chrY")) {
                end_of_genome = true;
              } // Ignore the Y chromosome
              else
                temp_genome_data.add(
                    split_line[1] + "," + split_line[1]); // The current line is an autosome
            }
            break;
          case 2: // Male
            {
              if (split_line[0].equals("chrX")) {
                temp_genome_data.add(split_line[1] + ",");
              } else if (split_line[0].equals("chrY")) {
                String temp =
                    temp_genome_data.get(
                        temp_genome_data.size() - 1); // Store the value already there
                temp_genome_data.remove(temp_genome_data.size() - 1);

                temp = temp + split_line[1];
                temp_genome_data.add(temp);

                end_of_genome = true;
              } else
                temp_genome_data.add(
                    split_line[1] + "," + split_line[1]); // The current line is an autosome
            }
            break;
        }
      }
    } // while ((line = genome_file_reader.readLine()) != null)
    genome_file_reader.close();
    return temp_genome_data;
  } // importGenomeData
示例#15
0
 public void order(List<P> positions) {
   List<P> existing = getPositions();
   for (int i = 0; i < positions.size(); i++) {
     existing.set(i, positions.get(i));
   }
 }
示例#16
0
 public P getSuccessor(P position) {
   List<P> positions = getPositions();
   int index = positions.indexOf(position);
   return index != -1 && index < positions.size() - 1 ? positions.get(index + 1) : null;
 }