Example #1
0
  public double next(int val) {

    if (q.size() == this.max) {
      total -= q.poll();
    }

    q.offer(val);
    total += val;

    return (double) total / q.size();
  }
 public int completed(R request) {
   assert request.equals(requests.peek());
   requests.poll();
   int remaining = requests.size();
   if (remaining != 0) coordinator.send(requests.peek());
   return remaining;
 }
  @Override
  public Boolean call() throws Exception {

    long timeoutMillis = 5000;

    try {
      getServersFile();
      getZkRunning();

      while (true) {
        while (!restartQueue.isEmpty()) {
          LOG.debug("Restart queue size [" + restartQueue.size() + "]");
          RestartHandler handler = restartQueue.poll();
          Future<ScriptContext> runner = pool.submit(handler);
          ScriptContext scriptContext = runner.get(); // blocking call
          if (scriptContext.getExitCode() != 0) restartQueue.add(handler);
        }

        try {
          Thread.sleep(timeoutMillis);
        } catch (InterruptedException e) {
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e);
      pool.shutdown();
      throw e;
    }
  }
  /**
   * Returns the number of instances pending output
   *
   * @return the number of instances pending output
   * @exception NullPointerException if no input structure has been defined
   */
  public int numPendingOutput() {

    if (m_OutputFormat == null) {
      throw new NullPointerException("No output instance format defined");
    }
    return m_OutputQueue.size();
  }
  @Override
  public void endWindow() {
    if (currentWindowId > idempotentStorageManager.getLargestRecoveryWindow()) {
      try {
        idempotentStorageManager.save(currentWindowRecoveryState, operatorId, currentWindowId);
      } catch (IOException e) {
        throw new RuntimeException("saving recovery", e);
      }
    }
    currentWindowRecoveryState.clear();
    if (context != null) {
      pendingFileCount.setValue(pendingFiles.size() + failedFiles.size() + unfinishedFiles.size());

      if (currentFile != null) {
        pendingFileCount.increment();
      }

      context.setCounters(fileCounters);
    }
  }
 public void run() {
   latch = new CountDownLatch(queue.size());
   while (!queue.isEmpty()) {
     final LineCounter counter = queue.remove();
     new Thread(
             new Runnable() {
               public void run() {
                 execute(counter);
                 latch.countDown();
               }
             })
         .start();
   }
   waitOnLatch();
 }
Example #7
0
  int bfs(String[] terrain, int sr, int sc, int er, int ec) {
    Queue<Node> nodes = new PriorityQueue<Node>();
    boolean seen[][] = new boolean[n][m];

    nodes.offer(new Node(sr, sc, 0));

    while (nodes.size() > 0) {
      Node top = nodes.poll();
      int r = top.r;
      int c = top.c;
      int cost = top.cost;

      if (r == er && c == ec) {
        return cost;
      }
      if (seen[r][c]) {
        continue;
      }

      seen[r][c] = true;

      for (int i = 0; i < dx.length; i++) {
        int nr = r + dx[i];
        int nc = c + dy[i];

        if (0 <= nr && nr < n && 0 <= nc && nc < m) {
          char u = terrain[r].charAt(c);
          char v = terrain[nr].charAt(nc);

          if (u == 'X' || u == '$' || v == 'X' || v == '$') {
            nodes.offer(new Node(nr, nc, cost + 2));
          } else {
            int uval = u - '0';
            int vval = v - '0';
            int dv = Math.abs(uval - vval);

            if (dv <= 1) {
              nodes.offer(new Node(nr, nc, cost + (dv == 0 ? 1 : 3)));
            }
          }
        }
      }
    }

    return -1;
  }
Example #8
0
 public void levelOrder(TreeNode root) {
   if (root == null) return;
   Queue<TreeNode> queue = new LinkedList<TreeNode>();
   queue.add(root);
   HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
   int i = 0;
   while (true) {
     int width = queue.size();
     if (width == 0) break;
     i = i + 1;
     ArrayList<Integer> al = new ArrayList<Integer>();
     while (width > 0) {
       root = queue.poll();
       al.add(root.n);
       if (root.left != null) queue.add(root.left);
       if (root.right != null) queue.add(root.right);
       width = width - 1;
     }
     map.put(i, al);
   }
 }
Example #9
0
 public void insert(TreeNode node) {
   if (root == null) {
     root = node;
     return;
   }
   Queue<TreeNode> queue = new LinkedList<TreeNode>();
   queue.add(root);
   while (queue.size() > 0) {
     root = queue.poll();
     if (root.left == null) {
       root.left = node;
       return;
     } else {
       queue.add(root.left);
     }
     if (root.right == null) {
       root.right = node;
       return;
     } else {
       queue.add(root.right);
     }
   }
 }
Example #10
0
 public int getSize() {
   return edits.size();
 }
Example #11
0
  public static void main(String[] args) throws Exception {

    if (args.length != 2) {
      System.err.println("USAGE: java -jar SAMSort.jar <BWA PAIRED SAM file> <configFile>");
    } else {
      System.err.println("Running: java -jar SAMSort.jar " + args[0] + " " + args[1]);
      // args[1]: conf file
      Constants.loadConstants(args[1], false);

      StringBuffer headers = new StringBuffer();

      ArrayList<SAM> list = new ArrayList<SAM>();

      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      int i = 0;
      int count = 0;
      String curline = null;
      String curline2 = null;

      SAM s1 = null;
      SAM s2 = null;

      boolean done = false;

      // int cordantCount = 0;
      // int discordantCount = 0;

      int totalPairCount = 0;
      int discordantPairCount = 0;
      int singleEndMappedPairCount = 0;
      int unmappedPairCount = 0;

      BufferedWriter unmapped_bw = null;
      BufferedWriter se_bw = null;

      while (!done) {
        curline = br.readLine();
        if (curline == null) break;
        else if (!curline.startsWith("@")) {
          s1 = new SAM(curline);
          // skip secondary. Load until primary
          while (s1.isSecondary()) {
            curline = br.readLine();
            if (curline == null) {
              done = true;
              break;
            }
            s1 = new SAM(curline);
          }

          if (done) break;

          curline2 = br.readLine();
          s2 = new SAM(curline2);
          // skip secondary. Load until primary
          while (s2.isSecondary()) {
            curline = br.readLine();
            if (curline == null) {
              done = true;
              break;
            }
            s2 = new SAM(curline);
          }

          if (done) break;

          totalPairCount++;

          // only collect discordant
          SAMPair tmppair = new SAMPair(s1, s2);
          if (tmppair.isDiscordant()) {
            discordantPairCount++;
            list.add(s1);
            list.add(s2);
            s1 = null;
            s2 = null;
            if (list.size() == 200000) {
              Collections.sort(list);
              BufferedWriter bw = new BufferedWriter(new FileWriter(args[0] + ".part_" + i));
              for (SAM s : list) {
                bw.write(s.getSamline() + "\n");
              }

              bw.close();
              bw = null;
              i++;
              list = new ArrayList<SAM>();
            }
          } else if (!tmppair.isBothUnmapped()) { // single-end mapped
            if (se_bw == null) {
              se_bw = new BufferedWriter(new FileWriter(args[0] + ".singleEndMapped"));
              se_bw.write(headers.toString());
            }
            singleEndMappedPairCount++;
            se_bw.write(s1.getSamline() + "\n");
            se_bw.write(s2.getSamline() + "\n");
            s1 = null;
            s2 = null;
          } else { // both unmapped
            if (unmapped_bw == null) {
              unmapped_bw = new BufferedWriter(new FileWriter(args[0] + ".unmapped"));
              unmapped_bw.write(headers.toString());
            }
            unmappedPairCount++;
            unmapped_bw.write(s1.getSamline() + "\n");
            unmapped_bw.write(s2.getSamline() + "\n");
            s1 = null;
            s2 = null;
          }

        } else {
          headers.append(curline + "\n");
        }
      }
      br.close();
      br = null;

      if (se_bw != null) se_bw.close();
      if (unmapped_bw != null) unmapped_bw.close();

      if (list.size() > 0) {
        Collections.sort(list);
        BufferedWriter bw = new BufferedWriter(new FileWriter(args[0] + ".part_" + i));
        // System.err.println(count + "\t" + set.size());
        for (SAM s : list) {
          bw.write(s.getSamline() + "\n");
        }
        bw.close();
        bw = null;
        i++;
        list = null;
      }

      Map<SAM, Queue<SAM>> map = new TreeMap<SAM, Queue<SAM>>();
      // Set<SAM> set = new TreeSet<SAM>();
      // System.err.println("openning " + i + " files");
      BufferedReader[] brArr = new BufferedReader[i];
      for (int j = 0; j < i; j++) {
        brArr[j] = new BufferedReader(new FileReader(args[0] + ".part_" + j));
        String tmpline = brArr[j].readLine();
        SAM tmp = new SAM(tmpline, j);
        if (!map.containsKey(tmp)) map.put(tmp, new LinkedList<SAM>());
        else {
          // System.err.println("here");
          map.get(tmp).add(tmp); // brArr[j].pushBack(tmpline);
        }
        // System.err.println(map.get(tmp).size());
      }
      // System.err.println(map.size());

      BufferedWriter bw = new BufferedWriter(new FileWriter(args[0] + ".discordant.midsorted"));
      bw.write(headers.toString());
      SAM s = null;

      // int counter = 0;
      Queue<SAM> q = null;
      while (!map.isEmpty()) {
        s = map.keySet().iterator().next();
        q = map.get(s);
        // map.remove(s);
        bw.write(s.getSamline());
        bw.write("\n");
        if (q.size() == 0) {
          map.remove(s);
          int tmpIndex = s.getIndex();
          String tmpline = brArr[tmpIndex].readLine();
          if (tmpline != null) {
            s = new SAM(tmpline, tmpIndex);
            if (!map.containsKey(s)) {
              // System.err.print("put b4: " + map.size());
              map.put(s, new LinkedList<SAM>());
              // System.err.println("\ta4: " + map.size());
            } else {
              // System.err.print("here2: ");
              map.get(s).add(s);
              // System.err.println(map.size());
            }
          } // else{
          // System.err.println("reader[" + tmpIndex + "] : NULL");
          // }
        } else {
          // System.err.print("B4 Q size: " + q.size() + " map size: " + map.size() );
          map.remove(s);
          map.put(q.remove(), q);
          // System.err.println("\tA4 Q size: " + q.size() + " map size: " + map.size() );
          int tmpIndex = s.getIndex();
          String tmpline = brArr[tmpIndex].readLine();
          if (tmpline != null) {
            s = new SAM(tmpline, tmpIndex);
            if (!map.containsKey(s)) {
              // System.err.print("put b4: " + map.size());
              map.put(s, new LinkedList<SAM>());
              // System.err.println("\ta4: " + map.size());
            } else {
              // System.err.print("here2: ");
              map.get(s).add(s);
              // System.err.println(map.size());
            }
          } // else{
          //  System.err.println("reader[" + tmpIndex + "] : NULL");
          // }

        }
        // counter++;

      }
      // System.err.println(counter);
      bw.close();

      for (int j = 0; j < brArr.length; j++) {
        brArr[j].close();
        new File(args[0] + ".part_" + j).delete();
      }

      //	int totalPairCount = 0;
      // int discordantPairCount = 0;
      System.out.println("------ DONE WITH REMOVAL OF CONCORDANT & UNMAPPED PAIRS ---------");
      System.out.println("------ Processed a total of\t" + totalPairCount + " read-pairs");
      System.out.println(
          "------ Retained a total of\t"
              + discordantPairCount
              + " read-pairs writtend out to "
              + args[0]
              + ".discordant.midsorted");
      System.out.println(
          "------ Single-end mapped :\t"
              + singleEndMappedPairCount
              + " read-pairs written out to "
              + args[0]
              + ".singleEndMapped");
      System.out.println(
          "------ Unmapped (both ends) : \t"
              + unmappedPairCount
              + " read-pairs written out to "
              + args[0]
              + ".unmapped");
    }
  }
 public int jobsLeft() {
   return jobs.size();
 }