<commit_before>-- lua-npge, Nucleotide PanGenome explorer (Lua module)
-- Copyright (C) 2014-2015 <NAME>
-- See the LICENSE file for terms of use.

describe("npge.algo.Multiply", function()

    it("small example", function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local s2 = model.Sequence("g2&c&c", "ATC")
        local bs1 = model.BlockSet({s1, s2}, {
            model.Block({
                model.Fragment(s1, 0, 2, 1),
                model.Fragment(s2, 0, 1, 1),
            }),
            model.Block({
                model.Fragment(s1, 3, 3, 1),
                model.Fragment(s2, 2, 2, 1),
            }),
        })
        local bs2 = model.BlockSet({s1, s2}, {
            model.Block({
                model.Fragment(s1, 0, 1, 1),
                model.Fragment(s2, 0, 1, 1),
            }),
            model.Block({
                model.Fragment(s1, 2, 3, 1),
                model.Fragment(s2, 2, 2, 1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        local mul = Multiply(bs1, bs2)
        assert.equal(
            mul,
            model.BlockSet({s1, s2}, {
                model.Block({
                    model.Fragment(s1, 0, 1, 1),
                    model.Fragment(s2, 0, 1, 1),
                }),
                model.Block({
                    model.Fragment(s1, 2, 2, 1),
                }),
                model.Block({
                    model.Fragment(s1, 3, 3, 1),
                    model.Fragment(s2, 2, 2, 1),
                }),
            })
        )
        local SM = require 'npge.algo.SplitMultiplication'
        local common, conflicts = SM(bs1, bs2, mul)
        assert.equal(
            common,
            model.BlockSet({s1, s2}, {
                model.Block({
                    model.Fragment(s1, 0, 1, 1),
                    model.Fragment(s2, 0, 1, 1),
                }),
                model.Block({
                    model.Fragment(s1, 3, 3, 1),
                    model.Fragment(s2, 2, 2, 1),
                }),
            })
        )
        assert.equal(
            conflicts,
            model.BlockSet({s1, s2}, {
                model.Block({
                    model.Fragment(s1, 2, 2, 1),
                }),
            })
        )
    end)

    it("respects ori of first blockset", function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local bs1 = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
            }),
        })
        local bs2 = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 3, 0, -1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        assert.equal(
            Multiply(bs1, bs2),
            model.BlockSet({s1}, {
                model.Block({
                    model.Fragment(s1, 0, 3, 1),
                }),
            })
        )
    end)

    it("throws if a blockset is not a partition", function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local partition = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
            }),
        })
        local not_partition = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 2, 1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        assert.has_no_error(function()
            Multiply(partition, partition)
        end)
        assert.has_error(function()
            Multiply(partition, not_partition)
        end)
        assert.has_error(function()
            Multiply(not_partition, partition)
        end)
        assert.has_error(function()
            Multiply(not_partition, not_partition)
        end)
    end)

    it("throws if blocksets use different sets of sequences",
    function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local s2 = model.Sequence("g2&c&c", "ATC")
        local bs1 = model.BlockSet({s1, s2}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
                model.Fragment(s2, 0, 2, 1),
            }),
        })
        local bs2 = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        assert.has_no_error(function()
            Multiply(bs1, bs1)
            Multiply(bs2, bs2)
        end)
        assert.has_error(function()
            Multiply(bs1, bs2)
        end)
    end)

    it("applied to #mosses", function()
        local bs1 = dofile 'spec/sample_pangenome.lua'
        local bs2 = dofile 'spec/sample_pangenome2.lua'
        local Multiply = require 'npge.algo.Multiply'
        local m = Multiply(bs1, bs2)
        assert.equal(m:size(), 737)
        local SM = require 'npge.algo.SplitMultiplication'
        local common, conflicts = SM(bs1, bs2, m)
        assert.equal(common:size(), 494)
        assert.equal(conflicts:size(), 243)
        local Overlapping = require 'npge.algo.Overlapping'
        for b in m:iterBlocks() do
            for f in b:iterFragments() do
                local oo1 = bs1:overlappingFragments(f)
                local oo2 = bs2:overlappingFragments(f)
                assert.equal(#oo1, 1)
                assert.equal(#oo2, 1)
                -- make sure that ori matches ori of bs1
                assert.equal(oo1[1]:ori(), f:ori())
            end
            local bb1 = Overlapping(bs1, b)
            local bb2 = Overlapping(bs2, b)
            assert.equal(#bb1, 1)
            assert.equal(#bb2, 1)
        end
    end)

end)
<commit_msg>test: ensure common + conflicts = multiplication<commit_after>-- lua-npge, Nucleotide PanGenome explorer (Lua module)
-- Copyright (C) 2014-2015 <NAME>
-- See the LICENSE file for terms of use.

describe("npge.algo.Multiply", function()

    it("small example", function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local s2 = model.Sequence("g2&c&c", "ATC")
        local bs1 = model.BlockSet({s1, s2}, {
            model.Block({
                model.Fragment(s1, 0, 2, 1),
                model.Fragment(s2, 0, 1, 1),
            }),
            model.Block({
                model.Fragment(s1, 3, 3, 1),
                model.Fragment(s2, 2, 2, 1),
            }),
        })
        local bs2 = model.BlockSet({s1, s2}, {
            model.Block({
                model.Fragment(s1, 0, 1, 1),
                model.Fragment(s2, 0, 1, 1),
            }),
            model.Block({
                model.Fragment(s1, 2, 3, 1),
                model.Fragment(s2, 2, 2, 1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        local mul = Multiply(bs1, bs2)
        assert.equal(
            mul,
            model.BlockSet({s1, s2}, {
                model.Block({
                    model.Fragment(s1, 0, 1, 1),
                    model.Fragment(s2, 0, 1, 1),
                }),
                model.Block({
                    model.Fragment(s1, 2, 2, 1),
                }),
                model.Block({
                    model.Fragment(s1, 3, 3, 1),
                    model.Fragment(s2, 2, 2, 1),
                }),
            })
        )
        local SM = require 'npge.algo.SplitMultiplication'
        local common, conflicts = SM(bs1, bs2, mul)
        assert.equal(
            common,
            model.BlockSet({s1, s2}, {
                model.Block({
                    model.Fragment(s1, 0, 1, 1),
                    model.Fragment(s2, 0, 1, 1),
                }),
                model.Block({
                    model.Fragment(s1, 3, 3, 1),
                    model.Fragment(s2, 2, 2, 1),
                }),
            })
        )
        assert.equal(
            conflicts,
            model.BlockSet({s1, s2}, {
                model.Block({
                    model.Fragment(s1, 2, 2, 1),
                }),
            })
        )
    end)

    it("respects ori of first blockset", function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local bs1 = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
            }),
        })
        local bs2 = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 3, 0, -1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        assert.equal(
            Multiply(bs1, bs2),
            model.BlockSet({s1}, {
                model.Block({
                    model.Fragment(s1, 0, 3, 1),
                }),
            })
        )
    end)

    it("throws if a blockset is not a partition", function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local partition = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
            }),
        })
        local not_partition = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 2, 1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        assert.has_no_error(function()
            Multiply(partition, partition)
        end)
        assert.has_error(function()
            Multiply(partition, not_partition)
        end)
        assert.has_error(function()
            Multiply(not_partition, partition)
        end)
        assert.has_error(function()
            Multiply(not_partition, not_partition)
        end)
    end)

    it("throws if blocksets use different sets of sequences",
    function()
        local model = require 'npge.model'
        local s1 = model.Sequence("g1&c&c", "ATGC")
        local s2 = model.Sequence("g2&c&c", "ATC")
        local bs1 = model.BlockSet({s1, s2}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
                model.Fragment(s2, 0, 2, 1),
            }),
        })
        local bs2 = model.BlockSet({s1}, {
            model.Block({
                model.Fragment(s1, 0, 3, 1),
            }),
        })
        local Multiply = require 'npge.algo.Multiply'
        assert.has_no_error(function()
            Multiply(bs1, bs1)
            Multiply(bs2, bs2)
        end)
        assert.has_error(function()
            Multiply(bs1, bs2)
        end)
    end)

    it("applied to #mosses", function()
        local bs1 = dofile 'spec/sample_pangenome.lua'
        local bs2 = dofile 'spec/sample_pangenome2.lua'
        local Multiply = require 'npge.algo.Multiply'
        local m = Multiply(bs1, bs2)
        assert.equal(m:size(), 737)
        local SM = require 'npge.algo.SplitMultiplication'
        local common, conflicts = SM(bs1, bs2, m)
        assert.equal(common:size(), 494)
        assert.equal(conflicts:size(), 243)
        local Merge = require 'npge.algo.Merge'
        local m1 = Merge({common, conflicts})
        assert.equal(m1, m)
        local Overlapping = require 'npge.algo.Overlapping'
        for b in m:iterBlocks() do
            for f in b:iterFragments() do
                local oo1 = bs1:overlappingFragments(f)
                local oo2 = bs2:overlappingFragments(f)
                assert.equal(#oo1, 1)
                assert.equal(#oo2, 1)
                -- make sure that ori matches ori of bs1
                assert.equal(oo1[1]:ori(), f:ori())
            end
            local bb1 = Overlapping(bs1, b)
            local bb2 = Overlapping(bs2, b)
            assert.equal(#bb1, 1)
            assert.equal(#bb2, 1)
        end
    end)

end)

{'fraction_non_alphanumeric': 0.1287452790600084, 'fraction_numerical': 0.03289970625262274, 'mean_word_length': 1.3708714683645047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7120985', 'n_tokens_mistral': 3989, 'n_tokens_neox': 3555, 'n_words': 810}