Package net.sf.samtools

Examples of net.sf.samtools.SAMRecord


          params.printSAMHeader);
    }

    MergedIterator mergedIterator = new MergedIterator(list, header);
    while (mergedIterator.hasNext()) {
      SAMRecord record = mergedIterator.next();
      writer.addAlignment(record);
    }

    mergedIterator.close();
    for (RecordSource source : list)
View Full Code Here


    @Override
    public SAMRecord next() {
      if (next == null)
        return null;

      SAMRecord result = next;
      advance();

      return result;
    }
View Full Code Here

    long timeStart = System.nanoTime();

    CloseableIterator<SAMRecord> iterator = reader.queryAlignmentStart(
        sequenceName, position);

    SAMRecord record = null;
    int overhead = 0;
    while (iterator.hasNext()) {
      record = iterator.next();
      if (record.getAlignmentStart() >= position)
        break;
      else
        record = null;
      overhead++;
    }
View Full Code Here

  public Cram2BamRecordFactory(SAMFileHeader header) {
    this.header = header;
  }

  public SAMRecord create(CramRecord cramRecord) {
    SAMRecord samRecord = new SAMRecord(header);

    samRecord.setReadName(cramRecord.getReadName());
    copyFlags(cramRecord, samRecord);

    if (cramRecord.sequenceId == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX) {
      samRecord.setAlignmentStart(SAMRecord.NO_ALIGNMENT_START);
      samRecord.setMappingQuality(SAMRecord.NO_MAPPING_QUALITY);
      samRecord.setReferenceIndex(SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX);
    } else {
      samRecord.setReferenceIndex(cramRecord.sequenceId);
      samRecord.setAlignmentStart(cramRecord.getAlignmentStart());
      samRecord.setMappingQuality(cramRecord.getMappingQuality());
    }

    if (cramRecord.segmentUnmapped)
      samRecord.setCigarString(SAMRecord.NO_ALIGNMENT_CIGAR);
    else
      samRecord.setCigar(getCigar2(cramRecord.getReadFeatures(),
          cramRecord.getReadLength()));

    if (samRecord.getReadPairedFlag()) {
      samRecord.setMateReferenceIndex(cramRecord.mateSequnceID);
      samRecord
          .setMateAlignmentStart(cramRecord.mateSequnceID == SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX ? SAMRecord.NO_ALIGNMENT_START
              : cramRecord.mateAlignmentStart);
      samRecord.setMateNegativeStrandFlag(cramRecord.mateNegativeStrand);
      samRecord.setMateUnmappedFlag(cramRecord.mateUmapped);
    } else {
      samRecord
          .setMateReferenceIndex(SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX);
      samRecord.setMateAlignmentStart(SAMRecord.NO_ALIGNMENT_START);
    }

    samRecord.setInferredInsertSize(cramRecord.templateSize);
    samRecord.setReadBases(cramRecord.getReadBases());
    samRecord.setBaseQualities(cramRecord.getQualityScores());

    if (cramRecord.tags != null)
      for (ReadTag tag : cramRecord.tags)
        samRecord.setAttribute(tag.getKey(), tag.getValue());

    if (cramRecord.getReadGroupID() > -1) {
//    if (cramRecord.getReadGroupID() < header.getReadGroups().size()) {
      SAMReadGroupRecord readGroupRecord = header.getReadGroups().get(
          cramRecord.getReadGroupID());
      samRecord.setAttribute("RG", readGroupRecord.getId());
    }

    return samRecord;
  }
View Full Code Here

   */
  public SAMRecord next() {
    if (!hasNext())
      throw new NoSuchElementException(
          "BAMQueryFilteringIterator: no next element available");
    final SAMRecord currentRead = mNextRecord;
    mNextRecord = advance();
    return currentRead;
  }
View Full Code Here

    while (true) {
      // Pull next record from stream
      if (!wrappedIterator.hasNext())
        return null;

      final SAMRecord record = wrappedIterator.next();
      // If beyond the end of this reference sequence, end iteration
      final int referenceIndex = record.getReferenceIndex();
      if (referenceIndex != mReferenceIndex) {
        if (referenceIndex < 0 || referenceIndex > mReferenceIndex) {
          return null;
        }
        // If before this reference sequence, continue
        continue;
      }
      if (mRegionStart == 0 && mRegionEnd == Integer.MAX_VALUE) {
        // Quick exit to avoid expensive alignment end calculation
        return record;
      }
      final int alignmentStart = record.getAlignmentStart();
      // If read is unmapped but has a coordinate, return it if the
      // coordinate is within
      // the query region, regardless of whether the mapped mate will
      // be returned.
      final int alignmentEnd;
      if (mQueryType == QueryType.STARTING_AT) {
        alignmentEnd = -1;
      } else {
        alignmentEnd = (record.getAlignmentEnd() != SAMRecord.NO_ALIGNMENT_START ? record
            .getAlignmentEnd() : alignmentStart);
      }

      if (alignmentStart > mRegionEnd) {
        // If scanned beyond target region, end iteration
View Full Code Here

    return StringUtil.bytesToString(buf);
  }

  public static void main(String[] args) {
    SAMFileHeader h = new SAMFileHeader();
    SAMRecord r = new SAMRecord(h);
    r.setAttribute("OQ", "A:LKAS:LKASDJKL".getBytes());
    r.setAttribute("XA", 1333123);
    r.setAttribute("XB", (byte) 31);
    r.setAttribute("XB", 'Q');
    r.setAttribute("XC", "A STRING");

    int intValue = 1123123123;
    byte[] data = writeSingleValue((byte) 'i', intValue, false);
    ByteBuffer byteBuffer = ByteBuffer.wrap(data);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
View Full Code Here

    writer.ps = System.out;
    writer.enumerate = params.enumerate;
    long counter = params.skipRecords + 1;

    while (it.hasNext()) {
      SAMRecord record = it.next();
      writer.write(record, counter++);
    }

    it.close();
    reader.close();
View Full Code Here

        params.maxContainerSize);
    int prevSeqId = SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX;
    SAMRecordIterator iterator = samFileReader.iterator();
    {
      String seqName = null;
      SAMRecord samRecord = iterator.next();
      if (samRecord == null)
        throw new RuntimeException("No records found.");
      seqName = samRecord.getReferenceName();
      prevSeqId = samRecord.getReferenceIndex();
      samRecords.add(samRecord);

      // if (samFileReader.getFileHeader().getReadGroups().isEmpty()
      // || samFileReader.getFileHeader().getReadGroup(
      // Sam2CramRecordFactory.UNKNOWN_READ_GROUP_ID) == null) {
      // log.info("Adding default read group.");
      // SAMReadGroupRecord readGroup = new SAMReadGroupRecord(
      // Sam2CramRecordFactory.UNKNOWN_READ_GROUP_ID);
      //
      // readGroup
      // .setSample(Sam2CramRecordFactory.UNKNOWN_READ_GROUP_SAMPLE);
      // samFileReader.getFileHeader().addReadGroup(readGroup);
      // }

      if (SAMRecord.NO_ALIGNMENT_REFERENCE_NAME.equals(seqName))
        sequence = null;
      else
        sequence = Utils.trySequenceNameVariants(referenceSequenceFile,
            seqName);

    }

    QualityScorePreservation preservation;
    if (params.losslessQS)
      preservation = new QualityScorePreservation("*40");
    else
      preservation = new QualityScorePreservation(params.qsSpec);

    byte[] ref = sequence == null ? new byte[0] : sequence.getBases();

    {
      // hack:
      int newLines = 0;
      for (byte b : ref)
        if (b == 10)
          newLines++;
      byte[] ref2 = new byte[ref.length - newLines];
      int j = 0;
      for (int i = 0; i < ref.length; i++)
        if (ref[i] == 10)
          continue;
        else
          ref2[j++] = ref[i];
      ref = ref2;
    }

    OutputStream os;
    if (params.outputCramFile != null) {
      FileOutputStream fos = new FileOutputStream(params.outputCramFile);
      os = new BufferedOutputStream(fos);
    } else {
      log.warn("No output file, writint to STDOUT.");
      os = System.out;
    }

    if (params.encrypt) {
      CipherOutputStream_256 cos = new CipherOutputStream_256(os, pass,
          128);
      os = cos.getCipherOutputStream();
    }

    CramHeader h = new CramHeader(2, 0, params.bamFile == null ? "STDIN"
        : params.bamFile.getName(), samFileReader.getFileHeader());
    long offset = ReadWrite.writeCramHeader(h, os);

    long bases = 0;
    long coreBytes = 0;
    long[] externalBytes = new long[10];
    BLOCK_PROTO.recordsPerSlice = params.maxSliceSize;
    long globalRecordCounter = 0;
    MessageDigest md5_MessageDigest = MessageDigest.getInstance("MD5");

    do {
      if (params.outputCramFile == null && System.out.checkError())
        return;

      SAMRecord samRecord = iterator.next();
      if (samRecord == null)
        // no more records
        break;
      if (samRecord.getReferenceIndex() != prevSeqId
          || samRecords.size() >= params.maxContainerSize) {
        if (!samRecords.isEmpty()) {
          List<CramRecord> records = convert(samRecords,
              samFileReader.getFileHeader(), ref, preservation,
              params.captureAllTags, params.captureTags,
              params.ignoreTags);
          samRecords.clear();

          Container container = BLOCK_PROTO.buildContainer(records,
              samFileReader.getFileHeader(),
              params.preserveReadNames, globalRecordCounter,
              null, true);
          for (Slice s : container.slices) {
            if (s.alignmentStart < 1) {
              s.refMD5 = new byte[16];
              Arrays.fill(s.refMD5, (byte) 0);
              continue;
            }

            md5_MessageDigest.reset();

            int span = Math.min(s.alignmentSpan, ref.length
                - s.alignmentStart);
            if (s.alignmentStart == 0)
              System.out.println("gotcha");

            md5_MessageDigest.update(ref, s.alignmentStart - 1,
                span);

            String sliceRef = new String(ref, s.alignmentStart - 1,
                Math.min(span, 30));
            s.refMD5 = md5_MessageDigest.digest();
            log.debug("Slice ref starts with: " + sliceRef);
            log.debug("Slice ref md5: "
                + (String.format("%032x", new BigInteger(1,
                    s.refMD5))));
          }
          globalRecordCounter += records.size();
          records.clear();
          long len = ReadWrite.writeContainer(container, os);
          container.offset = offset;
          offset += len;

          log.info(String
              .format("CONTAINER WRITE TIMES: header build time %dms, slices build time %dms, io time %dms.",
                  container.buildHeaderTime / 1000000,
                  container.buildSlicesTime / 1000000,
                  container.writeTime / 1000000));

          for (Slice s : container.slices) {
            coreBytes += s.coreBlock.getCompressedContent().length;
            for (Integer i : s.external.keySet())
              externalBytes[i] += s.external.get(i)
                  .getCompressedContent().length;
          }
        }
      }

      if (prevSeqId != samRecord.getReferenceIndex()) {
        if (samRecord.getReferenceIndex() != SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX) {
          sequence = Utils
              .trySequenceNameVariants(referenceSequenceFile,
                  samRecord.getReferenceName());
          ref = sequence.getBases();
          {
            // hack:
            int newLines = 0;
            for (byte b : ref)
              if (b == 10)
                newLines++;
            byte[] ref2 = new byte[ref.length - newLines];
            int j = 0;
            for (int i = 0; i < ref.length; i++)
              if (ref[i] == 10)
                continue;
              else
                ref2[j++] = ref[i];
            ref = ref2;
          }
        } else
          ref = new byte[] {};
        prevSeqId = samRecord.getReferenceIndex();
      }

      samRecords.add(samRecord);
      bases += samRecord.getReadLength();

      if (params.maxRecords-- < 1)
        break;
    } while (iterator.hasNext());

View Full Code Here

    ReferenceSequence sequence = null;
    {
      String seqName = null;
      SAMRecordIterator iterator = samFileReader.iterator();
      SAMRecord samRecord = iterator.next();
      seqName = samRecord.getReferenceName();
      iterator.close();
      sequence = referenceSequenceFile.getSequence(seqName);
    }

    int maxRecords = 100000;
    List<SAMRecord> samRecords = new ArrayList<SAMRecord>(maxRecords);

    int alStart = Integer.MAX_VALUE;
    int alEnd = 0;
    long baseCount = 0;
    SAMRecordIterator iterator = samFileReader.iterator();

    do {
      SAMRecord samRecord = iterator.next();
      if (!samRecord.getReferenceName().equals(sequence.getName()))
        break;

      baseCount += samRecord.getReadLength();
      samRecords.add(samRecord);
      if (samRecord.getAlignmentStart() > 0
          && alStart > samRecord.getAlignmentStart())
        alStart = samRecord.getAlignmentStart();
      if (alEnd < samRecord.getAlignmentEnd())
        alEnd = samRecord.getAlignmentEnd();

      if (samRecords.size() >= maxRecords)
        break;
    } while (iterator.hasNext());

    ReferenceTracks tracks = new ReferenceTracks(sequence.getContigIndex(),
        sequence.getName(), sequence.getBases(), alEnd - alStart + 100);
    tracks.moveForwardTo(alStart);

    Sam2CramRecordFactory f = new Sam2CramRecordFactory(
        sequence.getBases(), samFileReader.getFileHeader());
    f.captureUnmappedBases = true;
    f.captureUnmappedScores = true;
    List<CramRecord> cramRecords = new ArrayList<CramRecord>(maxRecords);
    int prevAlStart = samRecords.get(0).getAlignmentStart();
    int index = 0;
    QualityScorePreservation preservation = new QualityScorePreservation(
        "R8X10-R40X5-N40-U40");
    for (SAMRecord samRecord : samRecords) {
      CramRecord cramRecord = f.createCramRecord(samRecord);
      cramRecord.index = index++;
      cramRecord.alignmentStartOffsetFromPreviousRecord = samRecord
          .getAlignmentStart() - prevAlStart;
      prevAlStart = samRecord.getAlignmentStart();

      cramRecords.add(cramRecord);
      int refPos = samRecord.getAlignmentStart();
      int readPos = 0;
      for (CigarElement ce : samRecord.getCigar().getCigarElements()) {
        if (ce.getOperator().consumesReferenceBases()) {
          for (int i = 0; i < ce.getLength(); i++)
            tracks.addCoverage(refPos + i, 1);
        }
        switch (ce.getOperator()) {
        case M:
        case X:
        case EQ:
          for (int i = readPos; i < ce.getLength(); i++) {
            byte readBase = samRecord.getReadBases()[readPos + i];
            byte refBase = tracks.baseAt(refPos + i);
            if (readBase != refBase)
              tracks.addMismatches(refPos + i, 1);
          }
          break;
View Full Code Here

TOP

Related Classes of net.sf.samtools.SAMRecord

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.