org.ensembl.gti.seqstore.GeneHashTest.java Source code

Java tutorial

Introduction

Here is the source code for org.ensembl.gti.seqstore.GeneHashTest.java

Source

/*
 * Copyright 2015 EMBL-European Bioinformatics Institute
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ensembl.gti.seqstore;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;

import java.io.InputStream;

import org.ensembl.gti.seqstore.encoding.GeneDelegate;
import org.ensembl.gti.seqstore.encoding.GeneProtos;
import org.ensembl.gti.seqstore.utils.SeqStoreHashUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.util.StringUtils;

/**
 * Test for hashing of sequences etc. by {@link SeqStoreHashUtils}
 * 
 * @author dstaines
 *
 */
@Category(UnitTest.class)
public class GeneHashTest {

    Gene gene;
    Gene gene2;

    @Before
    public void setUp() throws Exception {
        InputStream stream = getClass().getResourceAsStream("/gene.dat");
        gene = new GeneDelegate(GeneProtos.Gene.parseFrom(stream));
        stream.close();
        stream = getClass().getResourceAsStream("/gene.dat");
        gene2 = new GeneDelegate(GeneProtos.Gene.parseFrom(stream));
        stream.close();
    }

    @Test
    public void testStringHash() {

        String hash1 = SeqStoreHashUtils.hashSequence("ATGTGATAG");
        String hash2 = SeqStoreHashUtils.hashSequence("ATGTGATAG");
        String hash3 = SeqStoreHashUtils.hashSequence("ATGTGATAGC");

        assertFalse("hash1 is empty or null", StringUtils.isEmpty(hash1));
        assertFalse("hash2 is empty or null", StringUtils.isEmpty(hash2));
        assertFalse("hash3 is empty or null", StringUtils.isEmpty(hash3));

        assertEquals("hash1 does not equal hash2", hash1, hash2);
        assertNotEquals("hash1 equals hash3", hash1, hash3);

    }

    @Test
    public void testLocationToString() {

        String locG = SeqStoreHashUtils.locationToString(gene);
        String locTr = SeqStoreHashUtils.locationToString(gene.getTranscripts().get(0));
        String locE = SeqStoreHashUtils.locationToString(gene.getTranscripts().get(0).getExons().get(0));

        assertFalse("locG is empty or null", StringUtils.isEmpty(locG));
        assertFalse("locTr is empty or null", StringUtils.isEmpty(locTr));
        assertFalse("locE is empty or null", StringUtils.isEmpty(locE));

        String locG2 = SeqStoreHashUtils.locationToString(gene2);
        String locTr2 = SeqStoreHashUtils.locationToString(gene2.getTranscripts().get(0));
        String locE2 = SeqStoreHashUtils.locationToString(gene2.getTranscripts().get(0).getExons().get(0));

        assertEquals("Gene models not equal", locG, locG2);
        assertEquals("Transcript models not equal", locTr, locTr2);
        assertEquals("Exon models not equal", locE, locE2);

    }

    @Test
    public void testModelToHash() {

        String hashG = SeqStoreHashUtils.hashGeneModel(gene);
        String hashTr = SeqStoreHashUtils.hashTranscriptModel(gene.getTranscripts().get(0));
        String hashE = SeqStoreHashUtils.hashExonModel(gene.getTranscripts().get(0).getExons().get(0));

        assertFalse("hashG is empty or null", StringUtils.isEmpty(hashG));
        assertFalse("hashTr is empty or null", StringUtils.isEmpty(hashTr));
        assertFalse("hashE is empty or null", StringUtils.isEmpty(hashE));

        String hashG2 = SeqStoreHashUtils.hashGeneModel(gene2);
        String hashTr2 = SeqStoreHashUtils.hashTranscriptModel(gene2.getTranscripts().get(0));
        String hashE2 = SeqStoreHashUtils.hashExonModel(gene2.getTranscripts().get(0).getExons().get(0));

        assertEquals("Gene hashes not equal", hashG, hashG2);
        assertEquals("Transcript hashes not equal", hashTr, hashTr2);
        assertEquals("Exon hashes not equal", hashE, hashE2);
    }

}