示例#1
0
 public static String prettyPrintSequenceRecords(final SAMSequenceDictionary sequenceDictionary) {
   final String[] sequenceRecordNames = new String[sequenceDictionary.size()];
   int sequenceRecordIndex = 0;
   for (final SAMSequenceRecord sequenceRecord : sequenceDictionary.getSequences()) {
     sequenceRecordNames[sequenceRecordIndex++] = sequenceRecord.getSequenceName();
   }
   return Arrays.deepToString(sequenceRecordNames);
 }
 private SAMFileHeader makeHeader() {
   final SAMFileHeader header = new SAMFileHeader();
   final SAMSequenceDictionary dict = header.getSequenceDictionary();
   dict.addSequence(new SAMSequenceRecord("chr20", 62435964));
   return header;
 }
示例#3
0
 private boolean isSequenceRecognized(final String sequence) {
   return (sequenceDictionary.getSequence(sequence) != null);
 }
示例#4
0
  @Override
  public int doWork(String[] args) {

    com.github.lindenb.jvarkit.util.cli.GetOpt opt =
        new com.github.lindenb.jvarkit.util.cli.GetOpt();
    int c;
    while ((c = opt.getopt(args, getGetOptDefault() + "k:")) != -1) {
      switch (c) {
        case 'k':
          this.ucscKnownGene = opt.getOptArg();
          break;
        default:
          {
            switch (handleOtherOptions(c, opt, null)) {
              case EXIT_FAILURE:
                return -1;
              case EXIT_SUCCESS:
                return 0;
              default:
                break;
            }
          }
      }
    }

    VcfIterator iter = null;
    BufferedReader r = null;
    try {
      if (opt.getOptInd() == args.length) {
        info("Reading from stdin");
        iter = new VcfIterator(System.in);
      } else if (opt.getOptInd() + 1 == args.length) {
        String uri = args[opt.getOptInd()];
        info("Reading from " + uri);
        iter = new VcfIterator(IOUtils.openURIForReading(uri));
      } else {
        error("Illegal number of arguments.");
        return -1;
      }
      info("Reading " + this.ucscKnownGene);
      SAMSequenceDictionary dict = iter.getHeader().getSequenceDictionary();

      int nKG = 0;
      r = IOUtils.openURIForBufferedReading(ucscKnownGene);
      Pattern tab = Pattern.compile("[\t]");
      String line;
      while ((line = r.readLine()) != null) {
        String tokens[] = tab.split(line);
        KnownGene g = new KnownGene(tokens);
        if (dict != null && dict.getSequence(g.getChr()) == null) continue;

        List<KnownGene> kg = this.chrom2knownGenes.get(g.getChr());
        if (kg == null) {
          kg = new ArrayList<KnownGene>();
          this.chrom2knownGenes.put(g.getChr(), kg);
        }
        kg.add(g);
        ++nKG;
      }
      r.close();
      info("Done Reading knownGenes. N=" + nKG);

      final double ticksH = (fHeight / 2.0f) * 0.6f;
      final double ticksx = 20;

      System.out.print(
          "%!PS\n"
              + "%%Creator: Pierre Lindenbaum PhD [email protected] http://plindenbaum.blogspot.com\n"
              + "%%Title: "
              + getClass().getSimpleName()
              + "\n"
              + "%%CreationDate: "
              + new Date()
              + "\n"
              + "%%EndComments\n"
              + "%%BoundingBox: 0 0 "
              + (this.pageDef.width + margin.left + margin.right)
              + " "
              + (this.pageDef.height + margin.top + margin.bottom)
              + "\n"
              + "/Courier findfont 9 scalefont setfont\n"
              + "/circle { 10 0 360 arc} bind def\n"
              + "/ticksF {\n"
              + (-ticksH)
              + " "
              + (ticksH)
              + " rmoveto\n"
              + ticksH
              + " "
              + (-ticksH)
              + " rlineto\n"
              + (-ticksH)
              + " "
              + (-ticksH)
              + " rlineto\n"
              + ticksH
              + " "
              + ticksH
              + " rmoveto\n"
              + "} bind def\n"
              + "/ticksR {\n"
              + (ticksH)
              + " "
              + (ticksH)
              + " rmoveto\n"
              + (-ticksH)
              + " "
              + (-ticksH)
              + " rlineto\n"
              + (ticksH)
              + " "
              + (-ticksH)
              + " rlineto\n"
              + (-ticksH)
              + " "
              + (ticksH)
              + " rmoveto\n"
              + "} bind def\n"
              + "/forticksF {2 dict begin\n"
              + "/x2 exch def\n"
              + "/x1 exch def\n"
              + "0 1 x2 x1 sub "
              + ticksx
              + " div {\n"
              + "ticksF "
              + ticksx
              + " 0 rmoveto\n"
              + "}for\n"
              + "} bind def\n"
              + "/forticksR {2 dict begin\n"
              + "/x2 exch def\n"
              + "/x1 exch def\n"
              + "0 1 x2 x1 sub "
              + ticksx
              + " div {\n"
              + " ticksR  "
              + ticksx
              + " 0 rmoveto\n"
              + "}for\n"
              + "} bind def\n"
              + "/box\n"
              + "{\n"
              + "4 dict begin\n"
              + "/height exch def\n"
              + "/width exch def\n"
              + "/y exch def\n"
              + "/x exch def\n"
              + "x y moveto\n"
              + "width 0 rlineto\n"
              + "0 height rlineto\n"
              + "width -1 mul 0 rlineto\n"
              + "0 height -1 mul rlineto\n"
              + "end\n"
              + "} bind def\n"
              + "/gradient\n"
              + "{\n"
              + "4 dict begin\n"
              + "/height exch def\n"
              + "/width exch def\n"
              + "/y exch def\n"
              + "/x exch def\n"
              + "/i 0 def\n"
              + "height 2 div /i exch def\n"
              + "\n"
              + "0 1 height 2 div {\n"
              + "	1 i height 2.0 div div sub setgray\n"
              + "	newpath\n"
              + "	x  \n"
              + "	y height 2 div i sub  add\n"
              + "	width\n"
              + "	i 2 mul\n"
              + "	box\n"
              + "	closepath\n"
              + "	fill\n"
              + "	i 1 sub /i exch def\n"
              + "	}for\n"
              + "newpath\n"
              + "0 setgray\n"
              + "0.4 setlinewidth\n"
              + "x y width height box\n"
              + "closepath\n"
              + "stroke\n"
              + "end\n"
              + "} bind def\n");

      run(iter);
      System.out.print("\n%%Trailer\n%%EOF\n");
      return 0;
    } catch (Exception err) {
      error(err);
      return -1;
    } finally {
      CloserUtil.close(r);
      CloserUtil.close(iter);
    }
  }
示例#5
0
  /** Combines multiple SAM/BAM files into one. */
  @Override
  protected int doWork() {
    boolean matchedSortOrders = true;

    // read interval list if it is defined
    final List<Interval> intervalList =
        (INTERVALS == null ? null : IntervalList.fromFile(INTERVALS).uniqued().getIntervals());
    // map reader->iterator used if INTERVALS is defined
    final Map<SamReader, CloseableIterator<SAMRecord>> samReaderToIterator =
        new HashMap<SamReader, CloseableIterator<SAMRecord>>(INPUT.size());

    // Open the files for reading and writing
    final List<SamReader> readers = new ArrayList<SamReader>();
    final List<SAMFileHeader> headers = new ArrayList<SAMFileHeader>();
    {
      SAMSequenceDictionary dict = null; // Used to try and reduce redundant SDs in memory

      for (final File inFile : INPUT) {
        IOUtil.assertFileIsReadable(inFile);
        final SamReader in =
            SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(inFile);
        if (INTERVALS != null) {
          if (!in.hasIndex())
            throw new PicardException(
                "Merging with interval but Bam file is not indexed " + inFile);
          final CloseableIterator<SAMRecord> samIterator =
              new SamRecordIntervalIteratorFactory()
                  .makeSamRecordIntervalIterator(in, intervalList, true);
          samReaderToIterator.put(in, samIterator);
        }

        readers.add(in);
        headers.add(in.getFileHeader());

        // A slightly hackish attempt to keep memory consumption down when merging multiple files
        // with
        // large sequence dictionaries (10,000s of sequences). If the dictionaries are identical,
        // then
        // replace the duplicate copies with a single dictionary to reduce the memory footprint.
        if (dict == null) {
          dict = in.getFileHeader().getSequenceDictionary();
        } else if (dict.equals(in.getFileHeader().getSequenceDictionary())) {
          in.getFileHeader().setSequenceDictionary(dict);
        }

        matchedSortOrders = matchedSortOrders && in.getFileHeader().getSortOrder() == SORT_ORDER;
      }
    }

    // If all the input sort orders match the output sort order then just merge them and
    // write on the fly, otherwise setup to merge and sort before writing out the final file
    IOUtil.assertFileIsWritable(OUTPUT);
    final boolean presorted;
    final SAMFileHeader.SortOrder headerMergerSortOrder;
    final boolean mergingSamRecordIteratorAssumeSorted;

    if (matchedSortOrders
        || SORT_ORDER == SAMFileHeader.SortOrder.unsorted
        || ASSUME_SORTED
        || INTERVALS != null) {
      log.info(
          "Input files are in same order as output so sorting to temp directory is not needed.");
      headerMergerSortOrder = SORT_ORDER;
      mergingSamRecordIteratorAssumeSorted = ASSUME_SORTED;
      presorted = true;
    } else {
      log.info("Sorting input files using temp directory " + TMP_DIR);
      headerMergerSortOrder = SAMFileHeader.SortOrder.unsorted;
      mergingSamRecordIteratorAssumeSorted = false;
      presorted = false;
    }
    final SamFileHeaderMerger headerMerger =
        new SamFileHeaderMerger(headerMergerSortOrder, headers, MERGE_SEQUENCE_DICTIONARIES);
    final MergingSamRecordIterator iterator;
    // no interval defined, get an iterator for the whole bam
    if (intervalList == null) {
      iterator =
          new MergingSamRecordIterator(headerMerger, readers, mergingSamRecordIteratorAssumeSorted);
    } else {
      // show warning related to https://github.com/broadinstitute/picard/pull/314/files
      log.info(
          "Warning: merged bams from different interval lists may contain the same read in both files");
      iterator = new MergingSamRecordIterator(headerMerger, samReaderToIterator, true);
    }
    final SAMFileHeader header = headerMerger.getMergedHeader();
    for (final String comment : COMMENT) {
      header.addComment(comment);
    }
    header.setSortOrder(SORT_ORDER);
    final SAMFileWriterFactory samFileWriterFactory = new SAMFileWriterFactory();
    if (USE_THREADING) {
      samFileWriterFactory.setUseAsyncIo(true);
    }
    final SAMFileWriter out = samFileWriterFactory.makeSAMOrBAMWriter(header, presorted, OUTPUT);

    // Lastly loop through and write out the records
    final ProgressLogger progress = new ProgressLogger(log, PROGRESS_INTERVAL);
    while (iterator.hasNext()) {
      final SAMRecord record = iterator.next();
      out.addAlignment(record);
      progress.record(record);
    }

    log.info("Finished reading inputs.");
    for (final CloseableIterator<SAMRecord> iter : samReaderToIterator.values())
      CloserUtil.close(iter);
    CloserUtil.close(readers);
    out.close();
    return 0;
  }