<commit_before>package com.milaboratory.core.sequence;

import com.milaboratory.core.Range;
import com.milaboratory.core.io.sequence.fastq.QualityFormat;
import com.milaboratory.core.io.sequence.fastq.WrongQualityFormat;
import com.milaboratory.primitivio.annotations.Serializable;
import com.milaboratory.util.ArraysUtils;

import java.util.Arrays;

/**
 * Representation of nucleotide sequence quality based on phred quality scores.
 *
 * @author <NAME>
 * @author <NAME>
 */
@Serializable(by = IO.SequenceQualitySerializer.class)
public final class SequenceQuality extends AbstractSeq<SequenceQuality> {
    /**
     * Default value of good quality
     */
    public static final byte GOOD_QUALITY_VALUE = (byte) 34;
    /**
     * Default value of bad quality
     */
    public static final byte BAD_QUALITY_VALUE = (byte) 0;

    private static final long serialVersionUID = 1L;
    final byte[] data;

    /**
     * Creates a phred sequence quality from a Sanger formatted quality string (33 based).
     *
     * @param string
     */
    public SequenceQuality(String string) {
        this(string, 33);
    }

    /**
     * Creates a phred sequence quality from a string formatted with corresponding offset.
     *
     * @param string string
     */
    public SequenceQuality(String string, int offset) {
        this.data = string.getBytes();
        for (int i = this.data.length - 1; i >= 0; --i)
            this.data[i] -= offset;
    }

    /**
     * Creates a phred sequence quality from a string formatted with corresponding offset.
     *
     * @param string string
     */
    public SequenceQuality(String string, QualityFormat format) {
        this(string, format.getOffset());
    }


    /**
     * Creates quality object from raw quality score values.
     *
     * @param data raw quality score values
     */
    public SequenceQuality(byte[] data) {
        this.data = data.clone();
    }

    /**
     * Constructor for factory method.
     */
    SequenceQuality(byte[] data, boolean unsafe) {
        assert unsafe;
        this.data = data;
    }


    /**
     * Returns an underlying array of bytes.
     *
     * @return underlying array of bytes
     */
    public byte[] asArray() {
        return data.clone();
    }

    /**
     * Get the log10 of probability of error (e.g. nucleotide substitution) at given sequence point
     *
     * @param position positioninate in sequence
     * @return log10 of probability of error
     */
    public float log10ProbabilityOfErrorAt(int position) {
        return -((float) data[position]) / 10;
    }

    /**
     * Get probability of error (e.g. nucleotide substitution) at given sequence point
     *
     * @param position positioninate in sequence
     * @return probability of error
     */
    public float probabilityOfErrorAt(int position) {
        return (float) Math.pow(10.0, -(data[position]) / 10);
    }

    /**
     * Get the raw sequence quality value (in binary format) at given sequence point
     *
     * @param position positioninate in sequence
     * @return raw sequence quality value
     */
    public byte value(int position) {
        return data[position];
    }

    /**
     * Returns the worst sequence quality value
     *
     * @return worst sequence quality value
     */
    public byte minValue() {
        byte min = Byte.MAX_VALUE;
        for (byte b : data)
            if (b < min)
                min = b;
        return min;
    }

    /**
     * Gets quality values in reverse order
     *
     * @return quality values in reverse order
     */
    public SequenceQuality reverse() {
        return new SequenceQuality(reverseCopy(data), true);
    }

    /**<commit_msg>Average SequenceQuality.<commit_after>package com.milaboratory.core.sequence;

import com.milaboratory.core.Range;
import com.milaboratory.core.io.sequence.fastq.QualityFormat;
import com.milaboratory.core.io.sequence.fastq.WrongQualityFormat;
import com.milaboratory.primitivio.annotations.Serializable;
import com.milaboratory.util.ArraysUtils;

import java.util.Arrays;

/**
 * Representation of nucleotide sequence quality based on phred quality scores.
 *
 * @author <NAME>
 * @author <NAME>
 */
@Serializable(by = IO.SequenceQualitySerializer.class)
public final class SequenceQuality extends AbstractSeq<SequenceQuality> {
    /**
     * Default value of good quality
     */
    public static final byte GOOD_QUALITY_VALUE = (byte) 34;
    /**
     * Default value of bad quality
     */
    public static final byte BAD_QUALITY_VALUE = (byte) 0;

    private static final long serialVersionUID = 1L;
    final byte[] data;

    /**
     * Creates a phred sequence quality from a Sanger formatted quality string (33 based).
     *
     * @param string
     */
    public SequenceQuality(String string) {
        this(string, 33);
    }

    /**
     * Creates a phred sequence quality from a string formatted with corresponding offset.
     *
     * @param string string
     */
    public SequenceQuality(String string, int offset) {
        this.data = string.getBytes();
        for (int i = this.data.length - 1; i >= 0; --i)
            this.data[i] -= offset;
    }

    /**
     * Creates a phred sequence quality from a string formatted with corresponding offset.
     *
     * @param string string
     */
    public SequenceQuality(String string, QualityFormat format) {
        this(string, format.getOffset());
    }


    /**
     * Creates quality object from raw quality score values.
     *
     * @param data raw quality score values
     */
    public SequenceQuality(byte[] data) {
        this.data = data.clone();
    }

    /**
     * Constructor for factory method.
     */
    SequenceQuality(byte[] data, boolean unsafe) {
        assert unsafe;
        this.data = data;
    }


    /**
     * Returns an underlying array of bytes.
     *
     * @return underlying array of bytes
     */
    public byte[] asArray() {
        return data.clone();
    }

    /**
     * Get the log10 of probability of error (e.g. nucleotide substitution) at given sequence point
     *
     * @param position positioninate in sequence
     * @return log10 of probability of error
     */
    public float log10ProbabilityOfErrorAt(int position) {
        return -((float) data[position]) / 10;
    }

    /**
     * Get probability of error (e.g. nucleotide substitution) at given sequence point
     *
     * @param position positioninate in sequence
     * @return probability of error
     */
    public float probabilityOfErrorAt(int position) {
        return (float) Math.pow(10.0, -(data[position]) / 10);
    }

    /**
     * Get the raw sequence quality value (in binary format) at given sequence point
     *
     * @param position positioninate in sequence
     * @return raw sequence quality value
     */
    public byte value(int position) {
        return data[position];
    }

    /**
     * Returns the worst sequence quality value
     *
     * @return worst sequence quality value
     */
    public byte minValue() {
        byte min = Byte.MAX_VALUE;
        for (byte b : data)
            if (b < min)
                min = b;
        return min;
    }

    /**
     * Returns average sequence quality value
     *
     * @return average sequence quality value
     */
    public byte avrgValue() {
        int sum = 0;
        for (byte b : data)
            sum += b;
        return (byte) (sum / data.length);
    }

    /**
     * Gets quality values in reverse order
     *
     * @return quality values in reverse order
     */
    public SequenceQuality reverse() {
        return new SequenceQuality(reverseCopy(data), true);
    }

    /**
{'fraction_non_alphanumeric': 0.09619628716084642, 'fraction_numerical': 0.006101518888744645, 'mean_word_length': 2.290901324220419, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4669894', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2057, 'n_words': 683}