All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.milaboratory.util.Math Maven / Gradle / Ivy

Go to download

MiTCR is an open source software package aimed at extraction of information on repertoire of T-cell clones from Next Generation Sequencing (NGS) data. It is designed with the knowledge of the critical challenges arising in everyday processing of immunological data.

There is a newer version: 1.0.3
Show newest version
/*
 * MiTCR 
 *
 * Copyright (c) 2010-2013:
 *     Bolotin Dmitriy     
 *     Chudakov Dmitriy    
 *
 * MiTCR is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see .
 */
package com.milaboratory.util;

public class Math {
    public static float log10Count(int count, int total) {
        if (count > 0)
            return (float) java.lang.Math.log10((double) count / total);
        return (float) (java.lang.Math.log10(1.0 / total) - 1.0 / java.lang.Math.log(10));
    }

    public static byte min(byte[] array) {
        byte min = Byte.MAX_VALUE;
        for (byte b : array)
            if (b < min)
                min = b;
        return min;
    }

    /**
     * Sorts the specified array of integers into ascending order and returns source permutation which acts on the final
     * sorted array and get source array. So it inverse permutation acting on specified array sort him.
     *
     * @param source source array to sort
     * @return inverse permutation, which sorts source array
     */
    public static int[] sort(int[] source) {
        int[] permutation = new int[source.length];
        for (int i = 0; i < permutation.length; ++i)
            permutation[i] = i;
        sort1(source, 0, source.length, permutation);
        return permutation;
    }

    public static void sort(int[] source, int[] permutation) {
        for (int i = 0; i < permutation.length; ++i)
            permutation[i] = i;
        sort1(source, 0, source.length, permutation);
    }

    /**
     * Sorts the specified sub-array of integers into ascending order.
     */
    private static void sort1(int x[], int off, int len, int[] permutation) {

        // Insertion sort on smallest arrays
        if (len < 7) {
            for (int i = off; i < len + off; i++)
                for (int j = i; j > off && x[j - 1] > x[j]; j--)
                    swap(x, j, j - 1, permutation);
            return;
        }

        // Choose a partition element, v
        int m = off + (len >> 1);       // Small arrays, middle element
        if (len > 7) {
            int l = off;
            int n = off + len - 1;
            if (len > 40) {        // Big arrays, pseudomedian of 9
                int s = len / 8;
                l = med3(x, l, l + s, l + 2 * s);
                m = med3(x, m - s, m, m + s);
                n = med3(x, n - 2 * s, n - s, n);
            }
            m = med3(x, l, m, n); // Mid-size, med of 3
        }
        int v = x[m];

        // Establish Invariant: v* (v)* v*
        int a = off, b = a, c = off + len - 1, d = c;
        while (true) {
            while (b <= c && x[b] <= v) {
                if (x[b] == v)
                    swap(x, a++, b, permutation);
                b++;
            }
            while (c >= b && x[c] >= v) {
                if (x[c] == v)
                    swap(x, c, d--, permutation);
                c--;
            }
            if (b > c)
                break;
            swap(x, b++, c--, permutation);
        }

        // Swap partition elements back to middle
        int s, n = off + len;
        s = java.lang.Math.min(a - off, b - a);
        vecswap(x, off, b - s, s, permutation);
        s = java.lang.Math.min(d - c, n - d - 1);
        vecswap(x, b, n - s, s, permutation);

        // Recursively sort non-partition-elements
        if ((s = b - a) > 1)
            sort1(x, off, s, permutation);
        if ((s = d - c) > 1)
            sort1(x, n - s, s, permutation);

    }

    private static void swap(int x[], int a, int b, int[] permutation) {
        swap(x, a, b);
        swap(permutation, a, b);
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static void swap(int x[], int a, int b) {
        int t = x[a];
        x[a] = x[b];
        x[b] = t;
    }

    private static void vecswap(int x[], int a, int b, int n, int[] permutation) {
        for (int i = 0; i < n; i++, a++, b++)
            swap(x, a, b, permutation);
    }

    /**
     * Returns the index of the median of the three indexed integers.
     */
    private static int med3(int x[], int a, int b, int c) {
        return (x[a] < x[b]
                ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
                : (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
    }

    // ------------------------------------

    /**
     * Sorts the specified array of integers into ascending order and returns source permutation which acts on the final
     * sorted array and get source array. So it inverse permutation acting on specified array sort him.
     *
     * @param source source array to sort
     * @return inverse permutation, which sorts source array
     */
    public static int[] sort(long[] source) {
        int[] permutation = new int[source.length];
        for (int i = 0; i < permutation.length; ++i)
            permutation[i] = i;
        sort1(source, 0, source.length, permutation);
        return permutation;
    }

    public static void sort(long[] source, int[] permutation) {
        for (int i = 0; i < permutation.length; ++i)
            permutation[i] = i;
        sort1(source, 0, source.length, permutation);
    }

    /**
     * Sorts the specified sub-array of integers into ascending order.
     */
    private static void sort1(long x[], int off, int len, int[] permutation) {

        // Insertion sort on smallest arrays
        if (len < 7) {
            for (int i = off; i < len + off; i++)
                for (int j = i; j > off && x[j - 1] > x[j]; j--)
                    swap(x, j, j - 1, permutation);
            return;
        }

        // Choose a partition element, v
        int m = off + (len >> 1);       // Small arrays, middle element
        if (len > 7) {
            int l = off;
            int n = off + len - 1;
            if (len > 40) {        // Big arrays, pseudomedian of 9
                int s = len / 8;
                l = med3(x, l, l + s, l + 2 * s);
                m = med3(x, m - s, m, m + s);
                n = med3(x, n - 2 * s, n - s, n);
            }
            m = med3(x, l, m, n); // Mid-size, med of 3
        }
        long v = x[m];

        // Establish Invariant: v* (v)* v*
        int a = off, b = a, c = off + len - 1, d = c;
        while (true) {
            while (b <= c && x[b] <= v) {
                if (x[b] == v)
                    swap(x, a++, b, permutation);
                b++;
            }
            while (c >= b && x[c] >= v) {
                if (x[c] == v)
                    swap(x, c, d--, permutation);
                c--;
            }
            if (b > c)
                break;
            swap(x, b++, c--, permutation);
        }

        // Swap partition elements back to middle
        int s, n = off + len;
        s = java.lang.Math.min(a - off, b - a);
        vecswap(x, off, b - s, s, permutation);
        s = java.lang.Math.min(d - c, n - d - 1);
        vecswap(x, b, n - s, s, permutation);

        // Recursively sort non-partition-elements
        if ((s = b - a) > 1)
            sort1(x, off, s, permutation);
        if ((s = d - c) > 1)
            sort1(x, n - s, s, permutation);

    }

    private static void swap(long x[], int a, int b, int[] permutation) {
        swap(x, a, b);
        swap(permutation, a, b);
    }

    /**
     * Swaps x[a] with x[b].
     */
    private static void swap(long x[], int a, int b) {
        long t = x[a];
        x[a] = x[b];
        x[b] = t;
    }

    private static void vecswap(long x[], int a, int b, int n, int[] permutation) {
        for (int i = 0; i < n; i++, a++, b++)
            swap(x, a, b, permutation);
    }

    /**
     * Returns the index of the median of the three indexed integers.
     */
    private static int med3(long x[], int a, int b, int c) {
        return (x[a] < x[b]
                ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
                : (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy