<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 */
package org.vetmeduni.utils.fastq;

import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.fastq.FastqRecord;
import htsjdk.samtools.util.FastqQualityFormat;
import org.junit.*;
import org.vetmeduni.utils.record.SAMRecordUtilsTest;

public class StandardizerAndCheckerTest {

	FastqRecord illuminaFASTQ;

	FastqRecord sangerFASTQ;

	SAMRecord illuminaSAM;

	SAMRecord sangerSAM;

	private static final StandardizerAndChecker sangerChecker = new StandardizerAndChecker(FastqQualityFormat.Standard);

	private static final StandardizerAndChecker illuminaChecker = new StandardizerAndChecker(
		FastqQualityFormat.Illumina);

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() {
	}

	@Before
	public void setUp() throws Exception {
		// init FASTQ
		illuminaFASTQ = new FastqRecord("Record1#ACGT/1", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "",
			QualityUtilsTest.illuminaQuality);
		sangerFASTQ = new FastqRecord("Record1#ACGT/1", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "",
			QualityUtilsTest.sangerQuality);
		// init SAMR
		illuminaSAM = SAMRecordUtilsTest.createSamRecord("Read1#ACTG/1", (byte) 'A', QualityUtilsTest.illuminaQuality);
		sangerSAM = SAMRecordUtilsTest.createSamRecord("Read1#ACTG/1", (byte) 'A', QualityUtilsTest.sangerQuality);
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCheckMisencoded() throws Exception {
		// Check exceptions
		checkMisencodedException(illuminaFASTQ, FastqQualityFormat.Standard);
		checkMisencodedException(illuminaSAM, FastqQualityFormat.Standard);
		checkMisencodedException(sangerFASTQ, FastqQualityFormat.Illumina);
		checkMisencodedException(sangerSAM, FastqQualityFormat.Illumina);
		// set to 0 the counters and in that case an exception should not be thrown
		sangerChecker.count.set(0);
		illuminaChecker.count.set(0);
		// this should not thrown an exception because of the counter
		sangerChecker.checkMisencoded(illuminaFASTQ);
		sangerChecker.checkMisencoded(illuminaSAM);
		// this should not thrown an exception because of the counter
		illuminaChecker.checkMisencoded(sangerFASTQ);
		illuminaChecker.checkMisencoded(sangerSAM);
	}

	private void checkMisencodedException(Object read, FastqQualityFormat checkerFormat) {
		final StandardizerAndChecker checker;
		switch (checkerFormat) {
			case Standard:
				checker = sangerChecker;
				break;
			case Illumina:
				checker = illuminaChecker;
				break;
			default:
				throw new RuntimeException("Unreachable code");
		}
		try {
			checker.checkMisencoded(read);
			Assert.fail(checkerFormat
				+ ".checkMisencoded does not throw a QualityException if the quality is not correctly formatted");
		} catch (QualityUtils.QualityException e) {
		}
	}

	@Test
	public void testStandardize() throws Exception {
		// checking standardizer for illumina
		Assert.assertEquals(sangerFASTQ, illuminaChecker.standardize(illuminaFASTQ));
		Assert.assertEquals(sangerSAM, illuminaChecker.standardize(illuminaSAM));
		// checking that the standard is not changed
		Assert.assertEquals(sangerFASTQ, sangerChecker.standardize(sangerFASTQ));
		Assert.assertEquals(sangerSAM, sangerChecker.standardize(sangerSAM));
		// checking that an error is thrown if the quality is misencoded
		try {
			illuminaChecker.standardize(sangerFASTQ);
			Assert.fail("FASTQ Illumina Standardizer does not throw a QualityException if the quality is Sanger");
		} catch (QualityUtils.QualityException e) {
		}
		try {
			illuminaChecker.standardize(sangerSAM);
			Assert.fail("SAM Illumina Standardizer does not throw a QualityException if the quality is Sanger");
		} catch (QualityUtils.QualityException e) {<commit_msg>fixing broken tests because new exception is thrown<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 */
package org.vetmeduni.utils.fastq;

import htsjdk.samtools.SAMException;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.fastq.FastqRecord;
import htsjdk.samtools.util.FastqQualityFormat;
import org.junit.*;
import org.vetmeduni.utils.record.SAMRecordUtilsTest;

public class StandardizerAndCheckerTest {

	FastqRecord illuminaFASTQ;

	FastqRecord sangerFASTQ;

	SAMRecord illuminaSAM;

	SAMRecord sangerSAM;

	private static final StandardizerAndChecker sangerChecker = new StandardizerAndChecker(FastqQualityFormat.Standard);

	private static final StandardizerAndChecker illuminaChecker = new StandardizerAndChecker(FastqQualityFormat.Illumina);

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() {
	}

	@Before
	public void setUp() throws Exception {
		// init FASTQ
		illuminaFASTQ = new FastqRecord("Record1#ACGT/1", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "",
			QualityUtilsTest.illuminaQuality);
		sangerFASTQ = new FastqRecord("Record1#ACGT/1", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "",
			QualityUtilsTest.sangerQuality);
		// init SAMR
		illuminaSAM = SAMRecordUtilsTest.createSamRecord("Read1#ACTG/1", (byte) 'A', QualityUtilsTest.illuminaQuality);
		sangerSAM = SAMRecordUtilsTest.createSamRecord("Read1#ACTG/1", (byte) 'A', QualityUtilsTest.sangerQuality);
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCheckMisencoded() throws Exception {
		// Check exceptions
		checkMisencodedException(illuminaFASTQ, FastqQualityFormat.Standard);
		checkMisencodedException(illuminaSAM, FastqQualityFormat.Standard);
		checkMisencodedException(sangerFASTQ, FastqQualityFormat.Illumina);
		checkMisencodedException(sangerSAM, FastqQualityFormat.Illumina);
		// set to 0 the counters and in that case an exception should not be thrown
		sangerChecker.count.set(0);
		illuminaChecker.count.set(0);
		// this should not thrown an exception because of the counter
		sangerChecker.checkMisencoded(illuminaFASTQ);
		sangerChecker.checkMisencoded(illuminaSAM);
		// this should not thrown an exception because of the counter
		illuminaChecker.checkMisencoded(sangerFASTQ);
		illuminaChecker.checkMisencoded(sangerSAM);
	}

	private void checkMisencodedException(Object read, FastqQualityFormat checkerFormat) {
		final StandardizerAndChecker checker;
		switch (checkerFormat) {
			case Standard:
				checker = sangerChecker;
				break;
			case Illumina:
				checker = illuminaChecker;
				break;
			default:
				throw new RuntimeException("Unreachable code");
		}
		try {
			checker.checkMisencoded(read);
			Assert.fail(checkerFormat
				+ ".checkMisencoded does not throw a QualityException if the quality is not correctly formatted");
		} catch (QualityUtils.QualityException e) {
		}
	}

	@Test
	public void testStandardize() throws Exception {
		// checking standardizer for illumina
		Assert.assertEquals(sangerFASTQ, illuminaChecker.standardize(illuminaFASTQ));
		Assert.assertEquals(sangerSAM, illuminaChecker.standardize(illuminaSAM));
		// checking that the standard is not changed
		Assert.assertEquals(sangerFASTQ, sangerChecker.standardize(sangerFASTQ));
		Assert.assertEquals(sangerSAM, sangerChecker.standardize(sangerSAM));
		// checking that an error is thrown if the quality is misencoded
		try {
			illuminaChecker.standardize(sangerFASTQ);
			Assert.fail("FASTQ Illumina Standardizer does not throw a SAMException if the quality is Sanger");
		} catch (SAMException e) {
		}
		try {
			illuminaChecker.standardize(sangerSAM);
			Assert.fail("SAM Illumina Standardizer does not throw a SAMException if the quality is Sanger");
		} catch (SAMException e) {
{'fraction_non_alphanumeric': 0.075686194182712, 'fraction_numerical': 0.0030725112658746417, 'mean_word_length': 5.271676300578035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, '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': '3404400', 'n_tokens_mistral': 3082, 'n_tokens_neox': 2743, 'n_words': 868}