Ejemplo n.º 1
0
def interleaved_phy(msa, add_flag=False, max_name_length=_MAX_NAME_LENGTH):
    validate_msa(msa)
    header = "%i %i" % (len(msa), len(msa.values()[0]))
    if add_flag:
        header += " I"
    result = [header, ""]

    padded_len = min(max_name_length, max(len(name) for name in msa)) + 2
    padded_len -= padded_len % -(_BLOCK_SIZE + _BLOCK_SPACING) + _BLOCK_SPACING

    streams = []
    spacing = " " * _BLOCK_SPACING
    for (name, sequence) in sorted(msa.iteritems()):
        name = name[:max_name_length]
        padding = (padded_len - len(name)) * " "

        lines = []
        line = [name, padding]
        for block in grouper(_BLOCK_SIZE, sequence, fillvalue=""):
            block = "".join(block)
            if sum(len(segment) for segment in line) >= _LINE_SIZE:
                lines.append("".join(line))
                line = [block]
            else:
                line.extend((spacing, block))

        lines.append("".join(line))
        streams.append(lines)

    for rows in zip(*streams):
        result.extend(row for row in rows)
        result.append("")
    result.pop()

    return "\n".join(result)
Ejemplo n.º 2
0
def interleaved_phy(msa, add_flag = False, max_name_length = _MAX_NAME_LENGTH):
    validate_msa(msa)
    header = "%i %i" % (len(msa), len(msa.values()[0]))
    if add_flag:
        header += " I"
    result = [header, ""]

    padded_len  = min(max_name_length, max(len(name) for name in msa)) + 2
    padded_len -= padded_len % -(_BLOCK_SIZE + _BLOCK_SPACING) + _BLOCK_SPACING

    streams = []
    spacing = " " * _BLOCK_SPACING
    for (name, sequence) in sorted(msa.iteritems()):
        name    = name[:max_name_length]
        padding = (padded_len - len(name)) * " "
        
        lines = []
        line  = [name, padding]
        for block in grouper(_BLOCK_SIZE, sequence, fillvalue = ""):
            block = "".join(block)
            if sum(len(segment) for segment in line) >= _LINE_SIZE:
                lines.append("".join(line))
                line = [block]
            else:
                line.extend((spacing, block))

        lines.append("".join(line))
        streams.append(lines)

    for rows in zip(*streams):
        result.extend(row for row in rows)
        result.append("")
    result.pop()

    return "\n".join(result)
Ejemplo n.º 3
0
def sequential_phy(msa, add_flag = False, max_name_length = _MAX_NAME_LENGTH):
    validate_msa(msa)
    header = "%i %i" % (len(msa), len(msa.values()[0]))
    if add_flag:
        header += " S"

    spacing = " " * _BLOCK_SPACING
    result = [header, ""]
    for (name, sequence) in sorted(msa.iteritems()):
        result.append(name[:max_name_length])
        
        blocks = grouper(_BLOCK_SIZE, sequence, fillvalue = "")
        lines  = grouper(_NUM_BLOCKS, blocks)
        for line in lines:
            result.append(spacing.join("".join(block) for block in line if block))
        
    return "\n".join(result)
Ejemplo n.º 4
0
def sequential_phy(msa, add_flag=False, max_name_length=_MAX_NAME_LENGTH):
    MSA.validate(msa)
    header = "%i %i" % (len(msa), msa.seqlen())
    if add_flag:
        header += " S"

    spacing = " " * _BLOCK_SPACING
    result = [header, ""]
    for record in sorted(msa):
        result.append(record.name[:max_name_length])

        blocks = grouper(_BLOCK_SIZE, record.sequence, fillvalue="")
        lines = grouper(_NUM_BLOCKS, blocks)
        for line in lines:
            result.append(
                spacing.join("".join(block) for block in line if block))

    return "\n".join(result)
Ejemplo n.º 5
0
def test_grouper__non_empty_list_with_trailing_fill_value():
    result = utils.grouper(3, range(7), fillvalue=r'\0')
    expected = [(0, 1, 2), (3, 4, 5), (6, r'\0', r'\0')]
    assert_equal(list(result), expected)
Ejemplo n.º 6
0
def test_grouper__non_empty_list_with_trailing():
    result = utils.grouper(3, range(7))
    expected = [(0, 1, 2), (3, 4, 5), (6, None, None)]
    assert_equal(list(result), expected)
Ejemplo n.º 7
0
def test_grouper__non_empty_list():
    result = utils.grouper(3, range(6))
    expected = [(0, 1, 2), (3, 4, 5)]
    assert_equal(list(result), expected)
Ejemplo n.º 8
0
def test_grouper__empty_list():
    result = utils.grouper(3, [])
    assert_equal(list(result), [])
Ejemplo n.º 9
0
def test_grouper__non_empty_list_with_trailing_fill_value():
    result = utils.grouper(3, range(7), fillvalue = r'\0')
    expected = [(0, 1, 2), (3, 4, 5), (6, r'\0', r'\0')]
    assert_equal(list(result), expected)
Ejemplo n.º 10
0
def test_grouper__non_empty_list_with_trailing():
    result = utils.grouper(3, range(7))
    expected = [(0, 1, 2), (3, 4, 5), (6, None, None)]
    assert_equal(list(result), expected)
Ejemplo n.º 11
0
def test_grouper__non_empty_list():
    result = utils.grouper(3, range(6))
    expected = [(0, 1, 2), (3, 4, 5)]
    assert_equal(list(result), expected)
Ejemplo n.º 12
0
def test_grouper__empty_list():
    result = utils.grouper(3, [])
    assert_equal(list(result), [])