/*
 * Copyright (c) 2014-2015, Bolotin Dmitry, Chudakov Dmitry, Shugay Mikhail
 * (here and after addressed as Inventors)
 * All Rights Reserved
 *
 * Permission to use, copy, modify and distribute any part of this program for
 * educational, research and non-profit purposes, by non-profit institutions
 * only, without fee, and without a written agreement is hereby granted,
 * provided that the above copyright notice, this paragraph and the following
 * three paragraphs appear in all copies.
 *
 * Those desiring to incorporate this work into commercial products or use for
 * commercial purposes should contact the Inventors using one of the following
 * email addresses: chudakovdm@mail.ru, chudakovdm@gmail.com
 *
 * IN NO EVENT SHALL THE INVENTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 * ARISING OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE INVENTORS HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE INVENTORS HAS
 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
 * MODIFICATIONS. THE INVENTORS MAKES NO REPRESENTATIONS AND EXTENDS NO
 * WARRANTIES OF ANY KIND, EITHER IMPLIED OR EXPRESS, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
 * PARTICULAR PURPOSE, OR THAT THE USE OF THE SOFTWARE WILL NOT INFRINGE ANY
 * PATENT, TRADEMARK OR OTHER RIGHTS.
 */

package com.milaboratory.mageri.core.genomic;

import com.milaboratory.mageri.FastTests;
import com.milaboratory.mageri.TestUtil;
import com.milaboratory.mageri.pipeline.input.ResourceIOProvider;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.IOException;

public class ReferenceLibraryTest {
    @Test
    @Category(FastTests.class)
    public void basicTest() throws IOException {

        ReferenceLibrary referenceLibrary = ReferenceLibrary.fromInput(
                ResourceIOProvider.INSTANCE.getWrappedStream("pipeline/refs.fa"),
                new BasicGenomicInfoProvider());

        System.out.println(referenceLibrary);

        Assert.assertTrue(!referenceLibrary.getReferences().isEmpty());

        for (Reference reference : referenceLibrary.getReferences()) {
            Assert.assertEquals(reference.getSequence().size(),
                    reference.getGenomicInfo().getContig().getLength());
            Assert.assertEquals(0,
                    reference.getGenomicInfo().getStart());
            Assert.assertEquals(reference.getSequence().size(),
                    reference.getGenomicInfo().getEnd() + 1);
        }

        TestUtil.serializationCheck(referenceLibrary);
    }

    @Test
    @Category(FastTests.class)
    public void genomicTest() throws IOException {

        ReferenceLibrary referenceLibrary = ReferenceLibrary.fromInput(
                ResourceIOProvider.INSTANCE.getWrappedStream("pipeline/refs.fa"),
                new BedGenomicInfoProvider(
                        ResourceIOProvider.INSTANCE.getWrappedStream("pipeline/refs.bed"),
                        ResourceIOProvider.INSTANCE.getWrappedStream("pipeline/contigs.txt")
                ));

        System.out.println(referenceLibrary);

        Assert.assertTrue(!referenceLibrary.getReferences().isEmpty());

        Reference braf = referenceLibrary.getByName("BRAF_E15");
        Assert.assertNotNull(braf);
        Assert.assertEquals("chr7", braf.getGenomicInfo().getChrom());
        Assert.assertEquals(140453124, braf.getGenomicInfo().getStart());
        Assert.assertEquals(140453232, braf.getGenomicInfo().getEnd());

        TestUtil.serializationCheck(referenceLibrary);
    }

    @Test
    @Category(FastTests.class)
    public void cgcGenomicTest() throws IOException {
        
        GenomicInfoProvider giProvider = new BedGenomicInfoProvider(
                ResourceIOProvider.INSTANCE.getWrappedStream("genomic/cgc_exons_flank50.bed"),
                ResourceIOProvider.INSTANCE.getWrappedStream("genomic/contigs_grch38.txt")
        );

        ReferenceLibrary referenceLibrary = ReferenceLibrary.fromInput(
                ResourceIOProvider.INSTANCE.getWrappedStream("genomic/cgc_exons_flank50.fa"),
                giProvider);

        Assert.assertTrue(!referenceLibrary.getReferences().isEmpty());
        Assert.assertEquals(referenceLibrary.size(), giProvider.size());

        TestUtil.serializationCheck(referenceLibrary);
    }
}

@@ -3860,17 +3860,9 @@
 n %7B%0A
-        
 %0A
+
     
@@ -4103,12 +4103,10 @@
 igs_
-grc
 h
+g
 38.t
@@ -4460,24 +4460,24 @@
 r.size());%0A%0A
-
         Test
@@ -4524,11 +4524,585 @@
 );%0A    %7D
+%0A%0A    @Test(expected = RuntimeException.class)%0A    @Category(FastTests.class)%0A    public void cgcGenomicBadContigsTest() throws IOException %7B%0A%0A        GenomicInfoProvider giProvider = new BedGenomicInfoProvider(%0A                ResourceIOProvider.INSTANCE.getWrappedStream(%22genomic/cgc_exons_flank50.bed%22),%0A                ResourceIOProvider.INSTANCE.getWrappedStream(%22genomic/contigs_grch38.txt%22)%0A        );%0A%0A        ReferenceLibrary.fromInput(%0A                ResourceIOProvider.INSTANCE.getWrappedStream(%22genomic/cgc_exons_flank50.fa%22),%0A                giProvider);%0A    %7D
 %0A%7D%0A

Fix existing reference library test, add test case for wrong contigs file