Exemplo n.º 1
0
def _serialize_to_link(link_, identifier=DEFAULT_IDENTIFIER):
    identifier = utils._check_identifier(identifier)
    try:
        if isinstance(link_, dict):
            link_dict = copy.deepcopy(link_)
            utils._remove_common_edge_fields(link_dict)
            defined_fields = [ \
                                link_['from_node'], \
                                link_['from_orn'], \
                                link_['to_node'], \
                                link_['to_orn'], \
                                link_['alignment'] \
                             ]
            fields = ["L"]
            fields.append(str(link_['from_node']))
            fields.append(str(link_['from_orn']))
            fields.append(str(link_['to_node']))
            fields.append(str(link_['to_orn']))

            if fv.is_gfa1_cigar(link_['alignment']):
                fields.append(str(link_['alignment']))
            else:
                fields.append("*")
            if not link_['eid'] in (None, '*'):
                fields.append("ID:Z:" + str(link_['eid']))
            fields.extend(utils._serialize_opt_fields(link_dict))
        else:
            defined_fields = [ \
                                link_.from_node, \
                                link_.from_orn, \
                                link_.to_node, \
                                link_.to_orn, \
                                link_.alignment \
                             ]
            fields = ["L"]
            fields.append(str(link_.from_node))
            fields.append(str(link_.from_orn))
            fields.append(str(link_.to_node))
            fields.append(str(link_.to_orn))

            if fv.is_gfa1_cigar(link_.alignment):
                fields.append(str(link_.alignment))
            else:
                fields.append("*")

            if not link_.eid in (None, '*'):
                fields.append("ID:Z:" + str(link_.eid))
            fields.extend(utils._serialize_opt_fields(link_.opt_fields))

        if not utils._are_fields_defined(defined_fields) or \
           not utils._check_fields(fields[1:], LINK_FIELDS):
            raise GFA1SerializationError()

        return str.join("\t", fields)

    except (KeyError, AttributeError, GFA1SerializationError) as e:
        serializer_logger.debug(utils._format_exception(identifier, e))
        return ""
Exemplo n.º 2
0
def _serialize_to_fragment(fragment_, identifier=DEFAULT_IDENTIFIER):
    identifier = utils._check_identifier(identifier)
    try:
        if isinstance(fragment_, dict):

            fragment_dict = copy.deepcopy(fragment_)
            utils._remove_common_edge_fields(fragment_dict)
            defined_fields = [\
                                fragment_['from_node'], \
                                fragment_['to_node'], \
                                fragment_['to_orn'], \
                                fragment_['from_positions'][0], \
                                fragment_['from_positions'][1], \
                                fragment_['to_positions'][0], \
                                fragment_['to_positions'][1], \
                                fragment_['alignment'] \
                            ]
            fields = ["F"]
            fields.append(str(fragment_['from_node']))
            fields.append(str(fragment_['to_node']) + str(fragment_['to_orn']))
            fields.append(str(fragment_['from_positions'][0]))
            fields.append(str(fragment_['from_positions'][1]))
            fields.append(str(fragment_['to_positions'][0]))
            fields.append(str(fragment_['to_positions'][1]))
            fields.append(str(fragment_['alignment']))
            fields.extend(utils._serialize_opt_fields(fragment_dict))
        else:
            defined_fields = [\
                                fragment_.from_node, \
                                fragment_.to_node, \
                                fragment_.to_orn, \
                                fragment_.from_positions[0], \
                                fragment_.from_positions[1], \
                                fragment_.to_positions[0], \
                                fragment_.to_positions[1], \
                                fragment_.alignment \
                             ]
            fields = ["F"]
            fields.append(str(fragment_.from_node))
            fields.append(str(fragment_.to_node) + str(fragment_.to_orn))
            fields.append(str(fragment_.from_positions[0]))
            fields.append(str(fragment_.from_positions[1]))
            fields.append(str(fragment_.to_positions[0]))
            fields.append(str(fragment_.to_positions[1]))
            fields.append(str(fragment_.alignment))
            fields.extend(utils._serialize_opt_fields(fragment_.opt_fields))

        if not utils. _are_fields_defined(defined_fields) or \
           not utils._check_fields(fields[1:], FRAGMENT_FIELDS):
            raise GFA2SerializationError("Required Fragment elements " \
                                        + "missing or invalid.")

        return str.join("\t", fields)

    except (KeyError, AttributeError, GFA2SerializationError) as e:
        serializer_logger.debug(utils._format_exception(identifier, e))
        return ""
Exemplo n.º 3
0
def _serialize_to_gap(gap_, identifier=DEFAULT_IDENTIFIER):
    identifier = utils._check_identifier(identifier)
    try:
        if isinstance(gap_, dict):
            gap_dict = copy.deepcopy(gap_)
            utils._remove_common_edge_fields(gap_dict)
            defined_fields = [\
                                gap_['eid'], \
                                gap_['from_node'], \
                                gap_['from_orn'], \
                                gap_['to_node'], \
                                gap_['to_orn'], \
                                gap_['distance'], \
                                gap_['variance'] \
                            ]
            fields = ["G"]
            fields.append(str(gap_['eid']))
            fields.append(str(gap_['from_node']) + str(gap_['from_orn']))
            fields.append(str(gap_['to_node']) + str(gap_['to_orn']))
            fields.append(str(gap_['distance']))
            fields.append(str(gap_['variance']))

            fields.extend(utils._serialize_opt_fields(gap_dict))
            return str.join("\t", fields)
        else:
            defined_fields = [\
                                gap_.eid, \
                                gap_.from_node, \
                                gap_.from_orn, \
                                gap_.to_node, \
                                gap_.to_orn, \
                                gap_.distance, \
                                gap_.variance \
                            ]
            fields = ["G"]
            fields.append(str(gap_.eid))
            fields.append(str(gap_.from_node) + str(gap_.from_orn))
            fields.append(str(gap_.to_node) + str(gap_.to_orn))
            fields.append(str(gap_.distance))
            fields.append(str(gap_.variance))
            fields.extend(utils._serialize_opt_fields(gap_.opt_fields))

        if not utils. _are_fields_defined(defined_fields) or \
           not utils._check_fields(fields[1:], GAP_FIELDS):
            raise GFA2SerializationError("Required Gap elements " \
                                        + "missing or invalid.")
        return str.join("\t", fields)
    except (AttributeError, KeyError, GFA2SerializationError) as e:
        serializer_logger.debug(utils._format_exception(identifier, e))
        return ""
Exemplo n.º 4
0
def _serialize_to_containment(containment_, identifier=DEFAULT_IDENTIFIER):
    identifier = utils._check_identifier(identifier)
    try:
        if isinstance(containment_, dict):
            containment_dict = copy.deepcopy(containment_)
            utils._remove_common_edge_fields(containment_dict)
            containment_dict.pop('pos')
            defined_fields = [ \
                                containment_['from_node'], \
                                containment_['from_orn'], \
                                containment_['to_node'], \
                                containment_['to_orn'], \
                                containment_['alignment'], \
                                containment_['pos'].value
                             ]
            fields = ["C"]
            fields.append(str(containment_['from_node']))
            fields.append(str(containment_['from_orn']))
            fields.append(str(containment_['to_node']))
            fields.append(str(containment_['to_orn']))
            fields.append(str(containment_['pos'].value))

            if fv.is_gfa1_cigar(containment_['alignment']):
                fields.append(str(containment_['alignment']))
            else:
                fields.append("*")

            if not containment_['eid'] in (None, '*'):
                fields.append("ID:Z:" + str(containment_['eid']))

            fields.extend(utils._serialize_opt_fields(containment_dict))
        else:
            defined_fields = [ \
                                containment_.from_node, \
                                containment_.from_orn, \
                                containment_.to_node, \
                                containment_.to_orn, \
                                containment_.alignment, \
                                containment_.opt_fields['pos'].value \
                             ]
            fields = ["C"]
            opt_fields = copy.deepcopy(containment_.opt_fields)
            opt_fields.pop('pos')
            fields.append(str(containment_.from_node))
            fields.append(str(containment_.from_orn))
            fields.append(str(containment_.to_node))
            fields.append(str(containment_.to_orn))
            fields.append(str(containment_.opt_fields['pos'].value))

            if fv.is_gfa1_cigar(containment_.alignment):
                fields.append(str(containment_.alignment))
            else:
                fields.append("*")
            if not containment_.eid in (None, '*'):
                fields.append("ID:Z:" + str(containment_.eid))
            fields.extend(utils._serialize_opt_fields(opt_fields))

        if not utils._are_fields_defined(defined_fields) or \
           not utils._check_fields(fields[1:], CONTAINMENT_FIELDS):
            raise GFA1SerializationError()

        return str.join("\t", fields)

    except (KeyError, AttributeError, GFA1SerializationError) as e:
        serializer_logger.debug(utils._format_exception(identifier, e))
        return ""