Beispiel #1
0
    def test_make_read(self):
        bases = 'ACG'
        quals = [30, 40, 50]
        cigar = '3M'
        mapq = 42
        chrom = 'chr10'
        start = 123
        name = 'myname'
        read = test_utils.make_read(bases,
                                    quals=quals,
                                    cigar=cigar,
                                    mapq=mapq,
                                    chrom=chrom,
                                    start=start,
                                    name=name)

        self.assertEqual(read.aligned_sequence, bases)
        self.assertEqual(read.aligned_quality, quals)
        self.assertEqual(list(read.alignment.cigar), [
            cigar_pb2.CigarUnit(operation_length=3,
                                operation=cigar_pb2.CigarUnit.ALIGNMENT_MATCH)
        ])
        self.assertEqual(read.alignment.mapping_quality, mapq)
        self.assertEqual(read.alignment.position.reference_name, chrom)
        self.assertEqual(read.alignment.position.position, start)
        self.assertEqual(read.fragment_name, name)
Beispiel #2
0
def to_cigar_unit(source):
    """Creates a cigar_pb2 CigarUnit from source.

  This function attempts to convert source into a CigarUnit protobuf. If
  source is a string, it must be a single CIGAR string specification like
  '12M'. If source is a tuple or a list, must have exactly two elements
  (operation_length, opstr). operation_length can be a string or int, and must
  be >= 1. opstr should be a single character CIGAR specification (e.g., 'M').
  If source is already a CigarUnit, it is just passed through unmodified.

  Args:
    source: many types allowed. The object we want to convert to a CigarUnit
      proto.

  Returns:
    CigarUnit proto with operation_length and operation set to values from
      source.

  Raises:
    ValueError: if source cannot be converted or is malformed.
  """
    try:
        if isinstance(source, cigar_pb2.CigarUnit):
            return source
        elif isinstance(source, six.string_types):
            l, op = source[:-1], source[-1]
        elif isinstance(source, (tuple, list)):
            l, op = source
        else:
            raise ValueError('Unexpected source', source)

        if isinstance(op, six.string_types):
            op = CHAR_TO_CIGAR_OPS[op]
        l = int(l)
        if l < 1:
            raise ValueError('Length must be >= 1', l)
        return cigar_pb2.CigarUnit(operation=op, operation_length=int(l))
    except (KeyError, IndexError):
        raise ValueError(
            'Failed to convert {} into a CigarUnit'.format(source))
Beispiel #3
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import itertools

from absl.testing import absltest
from absl.testing import parameterized

from nucleus.protos import cigar_pb2
from nucleus.util import cigar

_CIGAR_TUPLES_AND_CIGAR_UNITS = [
    ((1, 'M'),
     cigar_pb2.CigarUnit(operation=cigar_pb2.CigarUnit.ALIGNMENT_MATCH,
                         operation_length=1)),
    ((2, 'I'),
     cigar_pb2.CigarUnit(operation=cigar_pb2.CigarUnit.INSERT,
                         operation_length=2)),
    ((3, 'D'),
     cigar_pb2.CigarUnit(operation=cigar_pb2.CigarUnit.DELETE,
                         operation_length=3)),
    ((4, 'N'),
     cigar_pb2.CigarUnit(operation=cigar_pb2.CigarUnit.SKIP,
                         operation_length=4)),
    ((5, 'S'),
     cigar_pb2.CigarUnit(operation=cigar_pb2.CigarUnit.CLIP_SOFT,
                         operation_length=5)),
    ((6, 'H'),
     cigar_pb2.CigarUnit(operation=cigar_pb2.CigarUnit.CLIP_HARD,
                         operation_length=6)),