Beispiel #1
0
 private Collection<Throwable> execute_bam(String source) throws IOException {
   SamReader in = null;
   SAMRecordIterator iter = null;
   try {
     SamReaderFactory srf =
         SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT);
     if (source == null) {
       in = srf.open(SamInputResource.of(stdin()));
     } else {
       in = srf.open(SamInputResource.of(source));
     }
     iter = in.iterator();
     bindings.put("header", in.getFileHeader());
     bindings.put("iter", iter);
     bindings.put("format", "sam");
     this.script.eval(bindings);
     return RETURN_OK;
   } catch (Exception e) {
     LOG.error(e);
     return wrapException(e);
   } finally {
     CloserUtil.close(in);
     CloserUtil.close(iter);
     bindings.remove("header");
     bindings.remove("iter");
     bindings.remove("format");
   }
 }
Beispiel #2
0
 public CloseableIterator<PicardAlignment> iterator() {
   try {
     if (reader == null) {
       InputStream is = HttpUtils.getInstance().openConnectionStream(url);
       reader = new SAMFileReader(new BufferedInputStream(is));
     }
     return new WrappedIterator(reader.iterator());
   } catch (IOException e) {
     log.error("Error creating iterator", e);
     throw new RuntimeException(e);
   }
 }
Beispiel #3
0
  @Exec
  public void exec() throws IOException, CommandArgumentException {
    if (filename == null) {
      throw new CommandArgumentException("You must specify an input BAM filename!");
    }
    SamReaderFactory readerFactory = SamReaderFactory.makeDefault();
    if (lenient) {
      readerFactory.validationStringency(ValidationStringency.LENIENT);
    } else if (silent) {
      readerFactory.validationStringency(ValidationStringency.SILENT);
    }

    SamReader reader = null;
    String name;
    FileChannel channel = null;
    if (filename.equals("-")) {
      reader = readerFactory.open(SamInputResource.of(System.in));
      name = "<stdin>";
    } else {
      File f = new File(filename);
      FileInputStream fis = new FileInputStream(f);
      channel = fis.getChannel();
      reader = readerFactory.open(SamInputResource.of(fis));
      name = f.getName();
    }

    Iterator<SAMRecord> it =
        ProgressUtils.getIterator(
            name,
            reader.iterator(),
            (channel == null) ? null : new FileChannelStats(channel),
            new ProgressMessage<SAMRecord>() {
              long i = 0;

              @Override
              public String msg(SAMRecord current) {
                i++;
                return i + " " + current.getReadName();
              }
            },
            new CloseableFinalizer<SAMRecord>());
    long i = 0;
    while (it.hasNext()) {
      i++;
      it.next();
    }
    reader.close();
    System.err.println("Successfully read: " + i + " records.");
  }
Beispiel #4
0
  @Override
  public int doWork(String[] args) {
    boolean compressed = false;
    int maxRecordsInRAM = 100000;
    long count = -1L;
    File fileout = null;
    com.github.lindenb.jvarkit.util.cli.GetOpt opt =
        new com.github.lindenb.jvarkit.util.cli.GetOpt();
    int c;
    while ((c = opt.getopt(args, getGetOptDefault() + "o:n:N:T:b")) != -1) {
      switch (c) {
        case 'b':
          compressed = true;
          break;
        case 'N':
          maxRecordsInRAM = Integer.parseInt(opt.getOptArg());
          break;
        case 'n':
          count = Long.parseLong(opt.getOptArg());
          break;
        case 'o':
          fileout = new File(opt.getOptArg());
          break;
        case 'T':
          this.addTmpDirectory(new File(opt.getOptArg()));
          break;
        default:
          {
            switch (handleOtherOptions(c, opt, null)) {
              case EXIT_FAILURE:
                return -1;
              case EXIT_SUCCESS:
                return 0;
              default:
                break;
            }
          }
      }
    }
    if (count < -1L) // -1 == infinite
    {
      error("Bad count:" + count);
      return -1;
    }
    SamReader samReader = null;
    SAMRecordIterator iter = null;
    SAMFileWriter samWriter = null;
    Random random = new Random();
    CloseableIterator<RandSamRecord> iter2 = null;
    try {
      SamFileReaderFactory.setDefaultValidationStringency(ValidationStringency.SILENT);
      if (opt.getOptInd() == args.length) {
        info("Reading from stdin");
        samReader = SamFileReaderFactory.mewInstance().openStdin();
      } else if (opt.getOptInd() + 1 == args.length) {
        File filename = new File(args[opt.getOptInd()]);
        info("Reading from " + filename);
        samReader = SamFileReaderFactory.mewInstance().open(filename);
      } else {
        error("Illegal number of arguments.");
        return -1;
      }
      SAMFileHeader header = samReader.getFileHeader();

      header = header.clone();
      header.setSortOrder(SortOrder.unsorted);
      header.addComment("Processed with " + getProgramName() + " : " + getProgramCommandLine());
      SAMFileWriterFactory sfw = new SAMFileWriterFactory();
      sfw.setCreateIndex(false);
      sfw.setCreateMd5File(false);
      if (fileout == null) {
        if (compressed) {
          samWriter = sfw.makeBAMWriter(header, true, System.out);
        } else {
          samWriter = sfw.makeSAMWriter(header, true, System.out);
        }
      } else {
        samWriter = sfw.makeSAMOrBAMWriter(header, true, fileout);
        this.addTmpDirectory(fileout);
      }
      iter = samReader.iterator();
      SAMSequenceDictionaryProgress progress =
          new SAMSequenceDictionaryProgress(samReader.getFileHeader().getSequenceDictionary());

      SortingCollection<RandSamRecord> sorter =
          SortingCollection.newInstance(
              RandSamRecord.class,
              new RandSamRecordCodec(header),
              new RandSamRecordComparator(),
              maxRecordsInRAM,
              getTmpDirectories());
      sorter.setDestructiveIteration(true);
      while (iter.hasNext()) {
        RandSamRecord r = new RandSamRecord();
        r.rand_index = random.nextInt();
        r.samRecord = progress.watch(iter.next());

        sorter.add(r);
      }
      iter.close();
      iter = null;

      sorter.doneAdding();
      iter2 = sorter.iterator();
      if (count == -1) {
        while (iter2.hasNext()) {
          samWriter.addAlignment(iter2.next().samRecord);
        }
      } else {
        while (iter2.hasNext() && count > 0) {
          samWriter.addAlignment(iter2.next().samRecord);
          count--;
        }
      }
      iter2.close();
      iter2 = null;
      sorter.cleanup();
      progress.finish();
    } catch (Exception e) {
      error(e);
      return -1;
    } finally {
      CloserUtil.close(iter);
      CloserUtil.close(iter2);
      CloserUtil.close(samReader);
      CloserUtil.close(samWriter);
    }
    return 0;
  }
 private BAMProcessorImplDict(File in) {
   reader = SamReaderFactory.makeDefault().open(in);
   iterator = reader.iterator();
   queue = new LinkedList<SAMRecord>();
 }