Example #1
0
    def add_record(self, record: GFF3Record) -> None:
        """ """

        # Attributes can be None
        id_: Optional[str] = fmap(lambda a: getattr(a, "id"),
                                  record.attributes)

        if id_ is not None:
            self.index[id_].append(record)
            children = self.missing_parents.pop(id_, [])

            record.add_children(children)

        parent_ids: List[str] = or_else([],
                                        fmap(lambda a: getattr(a, "parent"),
                                             record.attributes))

        for parent_id in parent_ids:
            parents = self.index.get(parent_id, [])

            if len(parents) == 0:
                self.missing_parents[parent_id].append(record)
            else:
                record.add_parents(parents)

        self.inner.append(record)
        return
Example #2
0
    def parse(
        cls,
        string: str,
        strip_quote: bool = False,
        unescape: bool = False,
    ) -> "GFF3Attributes":
        if string.strip() in (".", ""):
            return cls()

        fields = (f.split("=", maxsplit=1)
                  for f in string.strip(" ;").split(";"))

        if strip_quote:
            kvpairs: Dict[str, str] = {
                k.strip(): v.strip(" '\"")
                for k, v in fields
            }
        else:
            kvpairs = {k.strip(): v.strip() for k, v in fields}

        id = kvpairs.pop("ID", None)
        name = kvpairs.pop("Name", None)

        alias = cls._parse_list_of_strings(kvpairs.pop("Alias", ""),
                                           strip_quote, unescape)

        parent = cls._parse_list_of_strings(kvpairs.pop("Parent", ""),
                                            strip_quote, unescape)

        target: Optional[Target] = fmap(
            Target.parse,
            fmap(lambda x: cls._attr_unescape(x) if unescape else x,
                 kvpairs.pop("Target", None)))

        gap = fmap(
            Gap.parse,
            fmap(lambda x: cls._attr_unescape(x) if unescape else x,
                 kvpairs.pop("Gap", None)))

        derives_from = cls._parse_list_of_strings(
            kvpairs.pop("Derives_from", ""), strip_quote, unescape)

        note = cls._parse_list_of_strings(kvpairs.pop("Note", ""), strip_quote,
                                          unescape)

        dbxref = cls._parse_list_of_strings(kvpairs.pop("Dbxref", ""),
                                            strip_quote, unescape)

        ontology_term = cls._parse_list_of_strings(
            kvpairs.pop("Ontology_term", ""), strip_quote, unescape)

        is_circular = fmap(parse_bool, kvpairs.pop("Is_circular", None))

        return cls(id, name, alias, parent, target, gap, derives_from, note,
                   dbxref, ontology_term, is_circular, kvpairs)
Example #3
0
def hints(args: argparse.Namespace) -> None:
    gff_to_hints = get_hints_map(args)

    type_to_trim = get_trim_map(args)
    type_to_priority = get_priority_map(args)

    gff = GFF.parse(args.infile)
    for parent in gff.select_type(args.group_level):
        group_name = fmap(lambda a: getattr(a, "id"), parent.attributes)

        if group_name is None:
            raise GPMissingID(
                "One of the selected records doesn't have an ID. "
                f"The offending line is {parent}.")

        for feature in gff.traverse_children([parent]):
            hint_feature = transform_child(
                feature,
                group_name,
                gff_to_hints,
                type_to_trim,
                type_to_priority,
                args.source,
                args.priority,
            )

            if hint_feature is not None:
                print(hint_feature, file=args.outfile)
    return