File: ImpLS.java

package info (click to toggle)
beagle 220722-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,644 kB
  • sloc: java: 17,045; sh: 55; makefile: 11
file content (85 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Copyright (C) 2014-2021 Brian L. Browning
 *
 * This file is part of Beagle
 *
 * Beagle 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.
 *
 * Beagle 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 <http://www.gnu.org/licenses/>.
 */
package imp;

import blbutil.Utilities;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceArray;

/**
 * <p>Class {@code ImpLS} computes HMM state probabilities
 * at genotyped markers in the target haplotypes.
 * </p>
 * <p>Instances of class {@code ImpLS} are thread-safe.
 * </p>
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public final class ImpLS {

    private ImpLS() {
        // private constructor to prevent instantiation
    }

    /**
     * Returns estimated HMM state probabilities at genotyped markers
     * for each target haplotype.  The returned array maps each target
     * haplotype index to the target haplotype's HMM state probabilities.
     * @param impData the input data for genotype imputation
     * @return a list of HMM state probabilities at genotyped markers
     * for each target haplotype.
     * @throws NullPointerException if {@code impData == null}
     */
    public static AtomicReferenceArray<StateProbs> stateProbs(ImpData impData)  {
        int nThreads = impData.par().nthreads();
        int nTargHaps = impData.targGT().nHaps();
        final AtomicReferenceArray<StateProbs> stProbs
                = new AtomicReferenceArray<>(nTargHaps);
        final AtomicInteger hapIndices = new AtomicInteger(0);
        ExecutorService es = Executors.newFixedThreadPool(nThreads);
        ImpIbs ibsHaps = new ImpIbs(impData);
        try {
            for (int j=0; j<nThreads; ++j) {
                ImpLSBaum baum = new ImpLSBaum(impData, ibsHaps);
                es.submit(() -> {
                    try {
                        int hap = hapIndices.getAndIncrement();
                        while (hap>=0 && hap<nTargHaps) {
                            StateProbs stateProbs = baum.impute(hap);
                            stProbs.set(hap, stateProbs);
                            hap = hapIndices.getAndIncrement();
                        }
                    }
                    catch(Throwable t) {
                        Utilities.exit(t);
                    }
                } );
            }
            es.shutdown();
            es.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        }
        catch (Throwable t) {
            Utilities.exit(t);
        }
        return stProbs;
    }
}