Beispiel #1
0
    def _get_connection_end(self, node, bp_name):
        node_end_index = -1

        if bp_name != "":
            ei = node.data.get_end_index(bp_name)
            if not node.available_pos(ei):
                raise exceptions.MotifGraphException(
                    "cannot add connection with " + str(node.index) + " and "
                    "end name " + bp_name + " as this end is not available")

            node_end_index = ei
        else:
            node_indexes = node.available_children_pos()

            if len(node_indexes) > 1:
                raise exceptions.MotifGraphException("cannot connect nodes " +
                                                     str(node.index) +
                                                     " its unclear "
                                                     " which ends to attach")

            if len(node_indexes) == 0:
                raise exceptions.MotifGraphException(
                    "cannot connect nodes " + str(node.index) + " there are "
                    "no ends free ends to attach too")

            node_end_index = node_indexes[0]
        return node_end_index
Beispiel #2
0
    def _validate_arguments_to_add_motif(self, m, m_name):
        """
        makes sure the add_motif function is called correctly

        :param m: motif to add to graph
        :type m: Motif object

        :param m_name: name of motif to add
        :type m_name: str

        :return: None
        """

        if m is not None and m_name is not None:
            raise exceptions.MotifGraphException(
                "cannot supply both a motif and motif name to add a motif to "
                "a motif graph")

        if m is None and m_name is None:
            raise exceptions.MotifGraphException(
                "must supply a motif object or motif name to add_motif")

        if m is not None:
            for n in self.graph.nodes:
                if n.data.id == m.id:
                    raise exceptions.MotifGraphException(
                        "cannot add motif: " + m.name +
                        " to graph as its uuid is " +
                        "already present in the graph")
Beispiel #3
0
    def _get_motif_from_manager(self, m_name, m_end_name):
        """
        helper function for add_motif should not be called directly. calls
        resource manager to get motif to be added to tree by the name of
        the motif.

        :param m_name: name of the motif to add to the tree
        :type m_name: str

        :param m_end_name: name of the basepair end of the motif to align by.
        :type m_end_name: str

        """

        try:
            if m_end_name is not None:
                m = rm.manager.get_motif(name=m_name, end_name=m_end_name)
            else:
                m = rm.manager.get_motif(name=m_name)
        except exceptions.ResourceManagerException as e:
            raise exceptions.MotifGraphException(
                "cannot add motif to graph, motif cannot be found in resource "
                "manager")

        return m
Beispiel #4
0
    def replace_motif(self, pos, new_motif):
        node = self.get_node(pos)
        if len(new_motif.ends) != len(node.data.ends):
            raise exceptions.MotifGraphException(
                "attempted to replace a motif with a different number of ends")

        node.data = new_motif.copy()

        self.update_merger = 1
        self._align_motifs_all_motifs()
Beispiel #5
0
    def get_node(self, i=None, uuid=None, m_name=None):
        if i is not None:
            return self.graph.get_node(i)

        node = None
        for n in self.graph.nodes:
            if n.data.id == uuid:
                return n
            if n.data.name == m_name:
                if node is not None:
                    raise exceptions.MotifGraphException(
                        "cannot get node with motif name: " + m_name +
                        " there "
                        "are more then one")
                node = n
        if node is None:
            raise exceptions.MotifGraphException(
                "could not find node with uuid " + str(uuid) +
                " and m_name: " + m_name)

        return node
Beispiel #6
0
    def _get_parent_node(self, parent_index):
        """
        gets node that serve as the parent of the current motif being added
        to the tree.

        :param parent_index: the tree index corresponding to the requested motif
        :type parent_index: int

        :return: tree node of parent
        :rtype: TreeNode
        """

        parent = self.graph.last_node

        if parent_index != -1:
            try:
                parent = self.graph.get_node(parent_index)
            except exceptions.GraphIndexException:
                raise exceptions.MotifGraphException(
                    "parent_index supplied: " + str(parent_index) +
                    " does not " + "exist in current motif graph")

        return parent
Beispiel #7
0
        def _assign_node_levels(self):
            """
            calculates and stores the node level of each node in the mt. The
            head node has a level of 1 and its children have a level of 2 and
            so on.
            """

            nodes = self.mg.get_not_aligned_nodes()

            if len(nodes) == 0:
                raise exceptions.MotifGraphException(
                    "cannot find a place to start printing in motif_graph"
                    " to_pretty_str")

            start = nodes[0].index
            i = 0
            for n in graph.transverse_graph(self.mg.graph, start, directed=0):
                if len(self.levels) == 0:
                    self.levels[n.index] = 1
                else:
                    c = n.connections[0]
                    parent = c.partner(n.index)
                    self.levels[n.index] = self.levels[parent.index] + 1
                self.nodes.append(n)
Beispiel #8
0
    def _get_parent_available_ends(self, parent, parent_end_index,
                                   parent_end_name):
        """
        Gets the available ends of the parent that the current motif can align
        to. If either parent_end_index or parent_end_name are specified, checks
        to see if that position is available.

        :param parent: the GraphNode of parent
        :type parent: GraphNode

        :param parent_end_index: which end this motif will be aligned to on
            parent
        :type parent_end_index: int

        :param parent_end_name: the name instead of the index of the end the
            current motif will align to
        :type parent_end_name: str

        :return: list of available parent end indexes that meet the specified
            constraints
        :rtype: list of ints
        """

        if parent is None:
            return []

        if parent_end_index != -1 and parent_end_name is not None:
            raise exceptions.MotifGraphException(
                "cannot supply parent_end_index and parent_end_name together")

        elif parent_end_name is not None:
            parent_ends = parent.data.get_basepair(name=parent_end_name)
            if len(parent_ends) == 0:
                raise exceptions.MotifGraphException(
                    "cannot find parent_end_name: " + parent_end_name + " in "
                    "parent motif: " + parent.data.name)
            if len(parent_ends) > 1:
                raise exceptions.MotifGraphException(
                    "more then one end was found with parent_end_name: " +
                    parent_end_name + " in parent motif: " + parent.data.name)

            parent_end = parent_ends[0]
            parent_end_index = parent.data.ends.index(parent_end)

            if parent_end_index == parent.data.block_end_add:
                raise exceptions.MotifGraphException(
                    "cannot add motif: to graph as the parent_end_name" +
                    " supplied is blocked see class Motif")

            available = parent.available_pos(parent_end_index)
            if not available:
                raise exceptions.MotifGraphException(
                    "cannot add motif to tree as the end " +
                    "you are trying to add it to is already filled or does "
                    "not exist")

            return [parent_end_index]

        elif parent_end_index != -1:
            if parent_end_index == parent.data.block_end_add:
                raise exceptions.MotifGraphException(
                    "cannot add motif: to tree as the parent_end_index" +
                    " supplied is blocked see class Motif")

            available = parent.available_pos(parent_end_index)
            if not available:
                raise exceptions.MotifGraphException(
                    "cannot add motif to tree as the end " +
                    "you are trying to add it to is already filled or does "
                    "not exist")

            return [parent_end_index]

        else:
            avail_pos = parent.available_children_pos()

            final_avail_pos = []
            for p in avail_pos:
                if p == parent.data.block_end_add:
                    continue
                final_avail_pos.append(p)

            return final_avail_pos