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

https://github.com/fenderglass/Ragout
22 June 2021, 01:27:41 UTC
  • Code
  • Branches (21)
  • Releases (0)
  • Visits
    • Branches
    • Releases
    • HEAD
    • refs/heads/chr_map
    • refs/heads/devel
    • refs/heads/gh-pages
    • refs/heads/ismb_2014
    • refs/heads/master
    • refs/heads/path_cover
    • refs/heads/py3
    • refs/heads/rr_devel
    • refs/heads/tree_infer
    • refs/remotes/origin/devel
    • refs/tags/1.0
    • refs/tags/1.1
    • refs/tags/2.0
    • refs/tags/2.1
    • refs/tags/2.1.1
    • refs/tags/2.2
    • refs/tags/2.3
    • refs/tags/v0.1b
    • refs/tags/v0.2b
    • refs/tags/v0.3b
    • refs/tags/v1.2
    No releases to show
  • eb905b6
  • /
  • scripts
  • /
  • test.py
Raw File Download Save again
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

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.

  • content
  • directory
  • revision
  • snapshot
origin badgecontent badge
swh:1:cnt:c72086db06a1be836c741fe6853e96095d5bfde0
origin badgedirectory badge
swh:1:dir:764b779386231c33cc22c428cab1840bb329b5ec
origin badgerevision badge
swh:1:rev:54ec8318ee615f6aa28b3188cfd95ee5679cc317
origin badgesnapshot badge
swh:1:snp:049c406b15a9a28e76d942bfc1fe2a04f4142f61

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.

  • content
  • directory
  • revision
  • snapshot
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
Tip revision: 54ec8318ee615f6aa28b3188cfd95ee5679cc317 authored by fenderglass on 15 January 2014, 22:20:24 UTC
sibelia path fix
Tip revision: 54ec831
test.py
#!/usr/bin/env python

import sys
from collections import namedtuple, defaultdict
from itertools import product


Entry = namedtuple("Entry", ["s_ref", "e_ref", "s_qry", "e_qry",
                            "len_ref", "len_qry", "ref_id", "contig_id"])

Scaffold = namedtuple("Scaffold", ["name", "contigs"])
Contig = namedtuple("Contig", ["name", "sign", "gap"])
class Hit:
    def __init__(self, pos, chr):
        self.pos = pos
        self.chr = chr

    def __str__(self):
        return str(self.pos) + " : " + str(self.chr)


def parse_nucmer_output(filename):
    chr_alias = {}
    chr_num = 1

    entries = []
    for line in open(filename, "r"):
        line = line.strip()
        if not len(line) or not line[0].isdigit():
            continue

        vals = line.split(" | ")
        s_ref, e_ref = map(int, vals[0].split())
        s_qry, e_qry = map(int, vals[1].split())
        len_ref, len_qry = map(int, vals[2].split())
        ref_id, contig_id = vals[4].split("\t")

        if ref_id not in chr_alias:
            chr_alias[ref_id] = "chr{0}".format(chr_num)
            chr_num += 1
        entries.append(Entry(s_ref, e_ref, s_qry, e_qry,
                            len_ref, len_qry, chr_alias[ref_id], contig_id))

    return entries


def parse_contigs_order(filename):
    scaffolds = []
    for line in open(filename, "r"):
        if line.startswith(">"):
            scaffolds.append(Scaffold(line.strip()[1:], []))
        else:
            name = line.strip("\n").replace("=", "_") #fix for nucmer
            without_sign = name[1:].strip()
            sign = 1 if name[0] == "+" else -1
            scaffolds[-1].contigs.append(Contig(without_sign, sign, 0))
    return scaffolds


def get_order(entries):
    MIN_HIT = 0.45

    chr_len = {}
    contig_len = defaultdict(int)

    by_name = defaultdict(list)
    for entry in entries:
        by_name[entry.contig_id].append(entry)

    for name in by_name:
        by_name[name].sort(key=lambda e: e.len_qry, reverse=True)
        by_name[name] = filter(lambda e: e.len_qry > MIN_HIT * by_name[name][0].len_qry, by_name[name])

    filtered_entries = []
    for ent_lst in by_name.itervalues():
        filtered_entries.extend(ent_lst)

    for entry in filtered_entries:
        contig_len[entry.contig_id] = max(entry.len_qry, contig_len[entry.contig_id])

    by_chr = defaultdict(list)
    for entry in filtered_entries:
        by_chr[entry.ref_id].append(entry)
    for chr_id in by_chr:
        by_chr[chr_id].sort(key=lambda e: e.s_ref)
        chr_len[chr_id] = len(by_chr[chr_id])

    entry_ord = defaultdict(list)
    for chr_id, entries in by_chr.iteritems():
        for i, e in enumerate(entries):
            entry_ord[e.contig_id].append(Hit(i, chr_id))

    return entry_ord, chr_len, contig_len


def gap_count(lst_1, lst_2):
    if not lst_1 or not lst_2:
        return 0

    gaps = 99999
    for i, j in product(lst_1, lst_2):
        gaps = min(gaps, abs(i.pos - j.pos) - 1)
    return gaps


def main():
    if len(sys.argv) < 3:
        print "Usage: test.py nucmer_coords scaffolds_ord"
        return

    entries = parse_nucmer_output(sys.argv[1])
    scaffolds = parse_contigs_order(sys.argv[2])
    entry_ord, chr_len, contig_len = get_order(entries)

    #TODO: check signs
    total_breaks = 0
    total_gaps = 0
    total_contigs = 0
    for s in scaffolds:
        print ">" + s.name

        prev = None
        increasing = None
        breaks = []
        for contig in s.contigs:
            if prev:
                if increasing is not None:
                    if not agreement(increasing, prev, entry_ord[contig.name], chr_len):
                        increasing = None
                        breaks.append(contig.name)
                        total_breaks += 1
                        sys.stdout.write("$")
                        #print map(lambda p: p.pos, prev)
                else:
                    if len(entry_ord[contig.name]) == 1 and len(prev) == 1:
                        increasing = entry_ord[contig.name][0].pos > prev[0].pos

            print "{0}\t{1}\t{2}".format(contig.name, contig_len[contig.name],
                                        map(str, entry_ord[contig.name]))
            #print increasing

            total_contigs += 1

            if gap_count(prev, entry_ord[contig.name]) > 0:
                total_gaps += 1
            #total_gaps += gap_count(prev, entry_ord[contig.name])

            #only if this contig has alignments
            if entry_ord[contig.name]:
                prev = entry_ord[contig.name]

        print "miss-ordered: ", breaks
    print "Total miss-ordered:", total_breaks
    print "Total gaps:", total_gaps
    print "Total contigs:", total_contigs
    print "Total scaffolds:", len(scaffolds)


def agreement(increasing, lst_1, lst_2, chr_len_dict):
    if not lst_1 or not lst_2:
        return True

    for i, j in product(lst_1, lst_2):
        same_chr = i.chr == j.chr
        if not same_chr:
            continue

        chr_len = chr_len_dict[i.chr]
        over_oric = ((increasing and i.pos > chr_len * 4 / 5 and j.pos < chr_len / 5) or
                    (not increasing and i.pos < chr_len / 5 and j.pos > chr_len * 4 / 5))

        if ((j.pos > i.pos) == increasing and abs(i.pos - j.pos) < chr_len / 3) or over_oric:
            return True
    return False


if __name__ == "__main__":
    main()

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