Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision 48e26abf0067fa41b92e8f82533fd00121acaec6 authored by Mikhail Kolmogorov on 05 May 2015, 23:21:22 UTC, committed by Mikhail Kolmogorov on 05 May 2015, 23:21:22 UTC
some refactoring
1 parent 8f07fce
  • Files
  • Changes
  • 61fab56
  • /
  • ragout
  • /
  • breakpoint_graph
  • /
  • chimera_detector.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:48e26abf0067fa41b92e8f82533fd00121acaec6
directory badge
swh:1:dir:6fa6fda2502f03aed997127e3750f8604fd6cb48
content badge
swh:1:cnt:2cba879d3daec9f5172fbb3608c25838c023ebe3

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
chimera_detector.py
#(c) 2013-2014 by Authors
#This file is a part of Ragout program.
#Released under the BSD license (see LICENSE file)

"""
This module tries to detect chimerias in
input scaffolds
"""

from __future__ import print_function
import logging
from collections import defaultdict
from copy import copy, deepcopy

from ragout.breakpoint_graph.breakpoint_graph import BreakpointGraph

logger = logging.getLogger()

class ChimeraDetector(object):
    def __init__(self, breakpoint_graph, perm_container):
        self.graph = breakpoint_graph
        self._get_chimeric_adj()
        self._get_contig_cuts(perm_container.target_perms)

    def _get_chimeric_adj(self):
        logger.info("Detecting chimeric sequences")
        chimeric_adj = set()

        subgraphs = self.graph.connected_components()
        for subgr in subgraphs:
            if len(subgr.bp_graph) > 100:
                logger.debug("Processing component of size {0}"
                             .format(len(subgr.bp_graph)))
                continue

            for (u, v) in subgr.bp_graph.edges_iter():
                genomes = subgr.supporting_genomes(u, v)
                if len(genomes) == 1 and genomes[0] == self.graph.target:
                    if not subgr.alternating_cycle(u, v):
                        chimeric_adj.add(tuple(sorted([u, v])))

        self.chimeric_adj = chimeric_adj

    def _get_contig_cuts(self, permutations):
        cuts = defaultdict(list)
        for perm in permutations:
            for pb, nb in perm.iter_pairs():
                adj = tuple(sorted([-pb.signed_id(), nb.signed_id()]))
                if adj in self.chimeric_adj:
                    cut = (nb.start + pb.end) / 2
                    cuts[perm.chr_name].append(cut)

        self.cuts = cuts

    def fix_container(self, perm_container):
        perm_container.target_perms = self._cut_permutations(self.cuts,
                                                perm_container.target_perms)
        perm_container.filter_indels(True)

    #TODO: refactoring
    def _cut_permutations(self, cuts, permutations):
        new_perms = []
        num_chim_perms = 0
        num_cuts = 0
        for perm in permutations:
            if perm.chr_name not in cuts:
                new_perms.append(perm)
                continue

            #logger.debug("Original {0}".format(perm))
            perm_cuts = copy(cuts[perm.chr_name])
            perm_cuts.append(perm.chr_len)
            cur_perm = deepcopy(perm)
            cur_perm.blocks = []
            shift = 0

            num_chim_perms += 1
            num_cuts += len(perm_cuts) - 1

            for block in perm.blocks:
                if block.end < perm_cuts[0]:
                    block.start -= shift
                    block.end -= shift
                    cur_perm.blocks.append(block)
                    continue

                if block.start < perm_cuts[0]:
                    continue

                #we have passed the current cut
                cur_perm.chr_name = (cur_perm.chr_name +
                                   "[{0}:{1}]".format(shift, perm_cuts[0]))
                cur_perm.chr_len = perm_cuts[0] - shift
                new_perms.append(cur_perm)
                #logger.debug(cur_perm)

                shift = perm_cuts[0]
                perm_cuts.pop(0)

                cur_perm = deepcopy(perm)
                block.start -= shift
                block.end -= shift
                cur_perm.blocks = [block]

            cur_perm.chr_name = (cur_perm.chr_name +
                               "[{0}:{1}]".format(shift, perm_cuts[0]))
            cur_perm.chr_len = perm_cuts[0] - shift
            new_perms.append(cur_perm)
            #logger.debug(cur_perm)

        logger.debug("Chimera Detector: {0} cuts made in {1} sequences"
                        .format(num_cuts, num_chim_perms))
        return new_perms
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API