File: Beagle.java

package info (click to toggle)
libhmsbeagle 4.0.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,436 kB
  • sloc: xml: 133,356; cpp: 36,477; ansic: 5,842; java: 2,400; python: 643; sh: 338; makefile: 50
file content (831 lines) | stat: -rw-r--r-- 38,687 bytes parent folder | download | duplicates (2)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/**
 * @file Beagle.java
 *
 * Copyright 2009-2019 Phylogenetic Likelihood Working Group
 *
 * This file is part of BEAGLE.
 *
 * Use of this source code is governed by an MIT-style
 * license that can be found in the LICENSE file or at
 * https://opensource.org/licenses/MIT.
 *
 * @brief This file documents the API as well as header for the
 * Broad-platform Evolutionary Analysis General Likelihood Evaluator
 *
 * KEY CONCEPTS
 *
 * The key to BEAGLE performance lies in delivering fine-scale
 * parallelization while minimizing data transfer and memory copy overhead.
 * To accomplish this, the library lacks the concept of data structure for
 * a tree, in spite of the intended use for phylogenetic analysis. Instead,
 * BEAGLE acts directly on flexibly indexed data storage (called buffers)
 * for observed character states and partial likelihoods. The client
 * program can set the input buffers to reflect the data and can calculate
 * the likelihood of a particular phylogeny by invoking likelihood
 * calculations on the appropriate input and output buffers in the correct
 * order. Because of this design simplicity, the library can support many
 * different tree inference algorithms and likelihood calculation on a
 * variety of models. Arbitrary numbers of states can be used, as can
 * nonreversible substitution matrices via complex eigen decompositions,
 * and mixture models with multiple rate categories and/or multiple eigen
 * decompositions. Finally, BEAGLE application programming interface (API)
 * calls can be asynchronous, allowing the calling program to implement
 * other coarse-scale parallelization schemes such as evaluating
 * independent genes or running concurrent Markov chains.
 *
 * USAGE
 *
 * To use the library, a client program first creates an instance of BEAGLE
 * by calling beagleCreateInstance; multiple instances per client are
 * possible and encouraged. All additional functions are called with a
 * reference to this instance. The client program can optionally request
 * that an instance run on certain hardware (e.g., a GPU) or have
 * particular features (e.g., double-precision math). Next, the client
 * program must specify the data dimensions and specify key aspects of the
 * phylogenetic model. Character state data are then loaded and can be in
 * the form of discrete observed states or partial likelihoods for
 * ambiguous characters. The observed data are usually unchanging and
 * loaded only once at the start to minimize memory copy overhead. The
 * character data can be compressed into unique “site patterns” and
 * associated weights for each. The parameters of the substitution process
 * can then be specified, including the equilibrium state frequencies, the
 * rates for one or more substitution rate categories and their weights,
 * and finally, the eigen decomposition for the substitution process.
 *
 * In order to calculate the likelihood of a particular tree, the client
 * program then specifies a series of integration operations that
 * correspond to steps in Felsenstein’s algorithm. Finite-time transition
 * probabilities for each edge are loaded directly if considering a
 * nondiagonalizable model or calculated in parallel from the eigen
 * decomposition and edge lengths specified. This is performed within
 * BEAGLE’s memory space to minimize data transfers. A single function call
 * will then request one or more integration operations to calculate
 * partial likelihoods over some or all nodes. The operations are performed
 * in the order they are provided, typically dictated by a postorder
 * traversal of the tree topology. The client needs only specify nodes for
 * which the partial likelihoods need updating, but it is up to the calling
 * software to keep track of these dependencies. The final step in
 * evaluating the phylogenetic model is done using an API call that yields
 * a single log likelihood for the model given the data.
 *
 * Aspects of the BEAGLE API design support both maximum likelihood (ML)
 * and Bayesian phylogenetic tree inference. For ML inference, API calls
 * can calculate first and second derivatives of the likelihood with
 * respect to the lengths of edges (branches). In both cases, BEAGLE
 * provides the ability to cache and reuse previously computed partial
 * likelihood results, which can yield a tremendous speedup over
 * recomputing the entire likelihood every time a new phylogenetic model is
 * evaluated.
 *
 * @author Likelihood API Working Group
 *
 * @author Daniel Ayres
 * @author Peter Beerli
 * @author Michael Cummings
 * @author Aaron Darling
 * @author Mark Holder
 * @author John Huelsenbeck
 * @author Paul Lewis
 * @author Michael Ott
 * @author Andrew Rambaut
 * @author Fredrik Ronquist
 * @author Marc Suchard
 * @author David Swofford
 * @author Derrick Zwickl
 *
 */

package beagle;

import java.io.Serializable;

/**
 * Beagle - An interface exposing the BEAGLE likelihood evaluation library.
 *
 * This interface mirrors the beagle.h API but it for a single instance only.
 * It is intended to be used by JNI wrappers of the BEAGLE library and for
 * Java implementations for testing purposes. BeagleFactory handles the creation
 * of specific istances.
 *
 * @author Andrew Rambaut
 * @author Marc A. Suchard
 * @version $Id:$
 */

public interface Beagle extends Serializable {

    public static int OPERATION_TUPLE_SIZE = 7;
    public static int PARTITION_OPERATION_TUPLE_SIZE = 9;
    public static int NONE = -1;


    /**
     * Finalize this instance
     *
     * This function finalizes the instance by releasing allocated memory
     */
    void finalize() throws Throwable;


    /**
     * Set number of threads for native CPU implementation
     *
     * This function sets the number of worker threads to be used with a native
     * CPU implementation. It should only be called after beagleCreateInstance and
     * requires the THREADING_CPP flag to be set. It has no effect on GPU-based
     * implementations. It has no effect with the default THREADING_NONE setting.
     * If THREADING_CPP is set and this function is not called BEAGLE will use
     * a heuristic to set an appropriate number of threads.
     *
     * @param threadCount          Number of threads (input)
     */
    void setCPUThreadCount(int threadCount);

    /**
     * Set the weights for each pattern
     * @param patternWeights    Array containing patternCount weights
     */
    void setPatternWeights(final double[] patternWeights);

    /**
     * Set pattern partition assignments
     *
     * This function sets the vector of pattern partition indices for an instance. It should
     * only be called after setTipPartials.
     *
     * @param partitionCount        Number of partitions
     * @param patternPartitions     Array containing partitionCount partition indices (input)
     */
    void setPatternPartitions(int partitionCount, final int[] patternPartitions);

    /**
     * Set the compressed state representation for tip node
     *
     * This function copies a compact state representation into an instance buffer.
     * Compact state representation is an array of states: 0 to stateCount - 1 (missing = stateCount).
     * The inStates array should be patternCount in length (replication across categoryCount is not
     * required).
     *
     * @param tipIndex   Index of destination partialsBuffer (input)
     * @param inStates   Pointer to compressed states (input)
     */
    void setTipStates(
            int tipIndex,
            final int[] inStates);

    /**
     * Get the compressed state representation for tip node
     *
     * This function copies a compact state representation from an instance buffer.
     * Compact state representation is an array of states: 0 to stateCount - 1 (missing = stateCount).
     * The inStates array should be patternCount in length (replication across categoryCount is not
     * required).
     *
     * @param tipIndex   Index of destination partialsBuffer (input)
     * @param outStates   Pointer to compressed states (input)
     */
    void getTipStates(
            int tipIndex,
            final int[] outStates);

    /**
     * Set an instance partials buffer
     *
     * This function copies an array of partials into an instance buffer. The inPartials array should
     * be stateCount * patternCount in length. For most applications this will be used
     * to set the partial likelihoods for the observed states. Internally, the partials will be copied
     * categoryCount times.
     *
     * @param tipIndex   Index of destination partialsBuffer (input)
     * @param  inPartials   Pointer to partials values to set (input)
     */
    void setTipPartials(
            int tipIndex,
            final double[] inPartials);

    /**
     * Set the pre-order partials for the root node
     *
     * This function copies an array of stateFrequencies into an instance buffer as the
     * pre-order partials for the root node.
     *
     * @param inbufferIndices                List of partialsBuffer indices to set (input)
     * @param instateFrequenciesIndices      List of state frequencies for each partialsBuffer (input).
     *                                       There should be one set for each of parentBufferIndices
     * @param count                          Number of partialsBuffer to integrate(input)
     */
    void setRootPrePartials(
            final int[] inbufferIndices,
            final int[] instateFrequenciesIndices,
            int count
    );

    /**
     * Set an instance partials buffer
     *
     * This function copies an array of partials into an instance buffer. The inPartials array should
     * be stateCount * patternCount * categoryCount in length.
     *
     * @param bufferIndex   Index of destination partialsBuffer (input)
     * @param  inPartials   Pointer to partials values to set (input)
     */
    void setPartials(
            int bufferIndex,
            final double[] inPartials);

    /**
     * Get partials from an instance buffer
     *
     * This function copies an array of partials from an instance buffer. The inPartials array should
     * be stateCount * patternCount * categoryCount in length.
     *
     * @param bufferIndex   Index of destination partialsBuffer (input)
     * @param scaleIndex    Index of scaleBuffer to apply to partials (input)
     * @param  outPartials  Pointer to which to receive partialsBuffer (output)
     */
    void getPartials(
            int bufferIndex,
            int scaleIndex,
            final double[] outPartials);

    /**
     * Get scale factors from instance buffer on log-scale
     *
     * This function copies an array of scale factors from an instance buffer. The outFactors array should
     * be patternCount in length.
     *
     * @param scaleIndex    Index of scaleBuffer to get (input)
     * @param  outFactors  Pointer to which to receive partialsBuffer (output)
     */
    void getLogScaleFactors(
            int scaleIndex,
            final double[] outFactors);

    /**
     * Set an eigen-decomposition buffer
     *
     * This function copies an eigen-decomposition into a instance buffer.
     *
     * @param eigenIndex                Index of eigen-decomposition buffer (input)
     * @param inEigenVectors            Flattened matrix (stateCount x stateCount) of eigen-vectors (input)
     * @param inInverseEigenVectors     Flattened matrix (stateCount x stateCount) of inverse-eigen-vectors (input)
     * @param inEigenValues             Vector of eigenvalues
     */
    void setEigenDecomposition(
            int eigenIndex,
            final double[] inEigenVectors,
            final double[] inInverseEigenVectors,
            final double[] inEigenValues);

    /**
     * Set a set of state frequences. These will probably correspond to an
     * eigen-system.
     *
     * @param stateFrequenciesIndex the index of the frequency buffer
     * @param stateFrequencies the array of frequences (stateCount)
     */
    void setStateFrequencies(int stateFrequenciesIndex,
                             final double[] stateFrequencies);

    /**
     * Set a set of category weights. These will probably correspond to an
     * eigen-system.
     *
     * @param categoryWeightsIndex the index of the buffer
     * @param categoryWeights the array of weights
     */
    void setCategoryWeights(int categoryWeightsIndex,
                            final double[] categoryWeights);

    /**
     * Set default category rates buffer
     *
     * This function sets the default vector of category rates for an instance.
     *
     * @param inCategoryRates       Array containing categoryCount rate scalers (input)
     */
    void setCategoryRates(final double[] inCategoryRates);

    /**
     * Set a category rates buffer
     *
     * This function sets the vector of category rates for a given buffer in an instance.
     *
     * @param categoryRatesIndex    the index of the buffer
     * @param inCategoryRates       Array containing categoryCount rate scalers (input)
     */
    void setCategoryRatesWithIndex(int categoryRatesIndex,
                                   final double[] inCategoryRates);

    /**
     * Convolve lists of transition probability matrices
     *
     * This function convolves two lists of transition probability matrices.
     *
     * @param firstIndices              List of indices of the first transition probability matrices to convolve (input)
     * @param secondIndices             List of indices of the second transition probability matrices to convolve (input)
     * @param resultIndices             List of indices of resulting transition probability matrices (input)
     * @param matrixCount               Lenght of lists
     */
    void convolveTransitionMatrices(
            final int[] firstIndices,
            final int[] secondIndices,
            final int[] resultIndices,
            int matrixCount);


    /**
     * Add lists of transition probability matrices
     *
     * This function add two lists of transition probability matrices.
     *
     * @param firstIndices              List of indices of the first transition probability matrices to convolve (input)
     * @param secondIndices             List of indices of the second transition probability matrices to convolve (input)
     * @param resultIndices             List of indices of resulting transition probability matrices (input)
     * @param matrixCount               Lenght of lists
     */
    void addTransitionMatrices(
            final int[] firstIndices,
            final int[] secondIndices,
            final int[] resultIndices,
            int matrixCount);

    /**
     * Transpose lists of transition probability matrices
     *
     * This function transposes a lists of transition probability matrices.
     *
     * @param inIndices              List of indices of the transition probability matrices to transpose (input)
     * @param outIndices             List of indices of the resulting transition probability matrices (input)
     * @param matrixCount               Lenght of lists
     */
    void transposeTransitionMatrices(
    		final int[] inIndices,
    		final int[] outIndices,
    		int matrixCount);

    /**
     * Calculate a list of transition probability matrices
     *
     * This function calculates a list of transition probabilities matrices and their first and
     * second derivatives (if requested).
     *
     * @param eigenIndex                Index of eigen-decomposition buffer (input)
     * @param probabilityIndices        List of indices of transition probability matrices to update (input)
     * @param firstDerivativeIndices    List of indices of first derivative matrices to update (input, NULL implies no calculation)
     * @param secondDervativeIndices    List of indices of second derivative matrices to update (input, NULL implies no calculation)
     * @param edgeLengths               List of edge lengths with which to perform calculations (input)
     * @param count                     Length of lists
     */
    void updateTransitionMatrices(
            int eigenIndex,
            final int[] probabilityIndices,
            final int[] firstDerivativeIndices,
            final int[] secondDervativeIndices,
            final double[] edgeLengths,
            int count);

    /**
     * Calculate a list of transition probability matrices with multiple models
     *
     * This function calculates a list of transition probabilities matrices and their first and
     * second derivatives (if requested).
     *
     * @param eigenIndices              List of indices of eigen-decomposition buffers (input)
     * @param categoryRateIndices       List of indices of category-rate buffers (input)
     * @param probabilityIndices        List of indices of transition probability matrices to update (input)
     * @param firstDerivativeIndices    List of indices of first derivative matrices to update (input, NULL implies no calculation)
     * @param secondDervativeIndices    List of indices of second derivative matrices to update (input, NULL implies no calculation)
     * @param edgeLengths               List of edge lengths with which to perform calculations (input)
     * @param count                     Length of lists
     */
    void updateTransitionMatricesWithMultipleModels(
            final int[] eigenIndices,
            final int[] categoryRateIndices,
            final int[] probabilityIndices,
            final int[] firstDerivativeIndices,
            final int[] secondDervativeIndices,
            final double[] edgeLengths,
            int count);

    /**
     * This function copies a finite-time transition probability matrix into a matrix buffer. This function
     * is used when the application wishes to explicitly set the transition probability matrix rather than
     * using the setEigenDecomposition and updateTransitionMatrices functions. The inMatrix array should be
     * of size stateCount * stateCount * categoryCount and will contain one matrix for each rate category.
     *
     * This function copies a finite-time transition probability matrix into a matrix buffer.
     * @param matrixIndex   Index of matrix buffer (input)
     * @param inMatrix      Pointer to source transition probability matrix (input)
     * @param paddedValue   Value to be used for padding for ambiguous states (e.g. 1 for probability matrices, 0 for derivative matrices) (input)
     */
    void setTransitionMatrix(
            int matrixIndex,			/**< Index of matrix buffer (input) */
            final double[] inMatrix, 	/**< Pointer to source transition probability matrix (input) */
            double paddedValue);

    /**
     * This function copies a differential transition probability matrix into a matrix buffer.
     * The inMatrix array should be of size stateCount * stateCount * categoryCount and will
     * contain one matrix for each rate category.
     *
     * This function copies a differential transition probability matrix into a matrix buffer.
     * @param matrixIndex   Index of matrix buffer (input)
     * @param inMatrix      Pointer to source transition probability matrix (input)
     */
    void setDifferentialMatrix(
            int matrixIndex,			/**< Index of matrix buffer (input) */
            final double[] inMatrix); 	/**< Pointer to source transition probability matrix (input) */

    /**
     * Get a finite-time transition probability matrix
     *
     * This function copies a finite-time transition matrix buffer into the array outMatrix. The
     * outMatrix array should be of size stateCount * stateCount * categoryCount and will be filled
     * with one matrix for each rate category.
     *
     * @param matrixIndex  Index of matrix buffer (input)
     * @param outMatrix    Pointer to destination transition probability matrix (output)
     *
     */
    void getTransitionMatrix(int matrixIndex,
                             double[] outMatrix);

    /**
     * Calculate or queue for calculation pre-order partials using a list of operations
     *
     * This function either calculates or queues for calculation a list pre-order partials. Implementations
     * supporting ASYNCH may queue these calculations while other implementations perform these
     * operations immediately and in order.
     *
     * Operations list is a list of 7-tuple integer indices, with one 7-tuple per operation.
     * Format of 7-tuple operation: {destinationPartials,
     *                               destinationScaleWrite,
     *                               destinationScaleRead,
     *                               pre-order partials of the parent node,
     *                               Ptr matrices of the current node,
     *                               post-order partials of the sibling node,
     *                               Ptr matrices of the sibling node}
     *
     * ///TODO: consider > 3 degree nodes?
     *
     * @param operations                list of 7-tuples specifying operations (input)
     * @param operationCount            Number of operations (input)
     * @param cumulativeScaleIndex      Index number of scaleBuffer to store accumulated factors (input)
     *
     */
    void updatePrePartials(
            final int[] operations,
            int operationCount,
            int cumulativeScaleIndex);

    void updatePrePartialsByPartition(
            final int[] operations,
            int operationCount);

    /**
     * Calculate gradient or / and diagonal hessian of given edges
     *
     * This function calculates gradient or diagonal hessian of the log likelihood with respect to edge length.
     *
     * @param postBufferIndices                 list of post order buffer indices
     * @param preBufferIndices                  list of pre  order buffer indices
     * @param rootBufferIndex                   root post order buffer index
     * @param firstDerivativeIndices            Q matrix indices
     * @param secondDerivativeIndices           Q^2 matrix indices
     * @param categoryWeightsIndex              category weights index
     * @param categoryRatesIndex                category rates index
     * @param stateFrequenciesIndex             state frequency index
     * @param cumulativeScaleIndices            cumulative scaling factor indices, currently not used
     * @param count                             number of edges
     * @param outFirstDerivative                gradient output array
     * @param outDiagonalSecondDerivative       diagonal hessian output array
     *
     */
    void calculateEdgeDerivative(
    		final int[] postBufferIndices,
	        final int[] preBufferIndices,
	        final int rootBufferIndex,
    	    final int[] firstDerivativeIndices,
	        final int[] secondDerivativeIndices,
	        final int categoryWeightsIndex,
	        final int categoryRatesIndex,
	        final int stateFrequenciesIndex,
	        final int[] cumulativeScaleIndices,
	        int count,
	        double[] outFirstDerivative,
    	    double[] outDiagonalSecondDerivative);

    /**
     * Calculate differential w.r.t. edges
     *
     * This function calculates a derivative of the log likelihood with respect to edge-differentials.
     *
     * @param postBufferIndices                 list of post order buffer indices
     * @param preBufferIndices                  list of pre  order buffer indices
     * @param derivativeMatrixIndices           differential Q matrix indices
     * @param categoryWeightsIndices            category weights indices
     * @param count                             number of edges
     * @param outDerivatives                    derivative-per-site output array
     * @param outSumDerivatives                 sum of derivatives across sites output array
     * @param outSumSquaredDerivatives          sum of squared derivatives output array
     *
     */
    void calculateEdgeDifferentials(
    		final int[] postBufferIndices,
        	final int[] preBufferIndices,
	        final int[] derivativeMatrixIndices,
    	    final int[] categoryWeightsIndices,
        	int count,
	        double[] outDerivatives,
	        double[] outSumDerivatives,
	        double[] outSumSquaredDerivatives);

    /**
     * Calculate differential w.r.t. substitution-model generator elements
     *
     * This function calculates a derivative of the log likelihood with respect to the
     * substitution model generator elements
     *
     * @param postBufferIndices                 list of post order buffer indices
     * @param preBufferIndices                  list of pre  order buffer indices
     * @param categoryWeightsIndices            category weights indices
     * @param count                             number of edges
     * @param outDerivatives                    derivative-per-element output array
     *
     */
    void calculateCrossProductDifferentials(
    		final int[] postBufferIndices,
        	final int[] preBufferIndices,
    	    final int[] categoryRateIndices,
    	    final int[] categoryWeightsIndices,
        	final double[] edgeLengths,
        	int count,
	        double[] outSumDerivatives,
	        double[] outSumSquaredDerivatives);

    /**
     * Calculate or queue for calculation partials using a list of operations
     *
     * This function either calculates or queues for calculation a list partials. Implementations
     * supporting ASYNCH may queue these calculations while other implementations perform these
     * operations immediately and in order.
     *
     * If partitions have been set via setPatternPartitions, operationCount should be a
     * multiple of partitionCount.
     *
     * Operations list is a list of 7-tuple integer indices, with one 7-tuple per operation.
     * Format of 7-tuple operation: {destinationPartials,
     *                               destinationScaleWrite,
     *                               destinationScaleRead,
     *                               child1Partials,
     *                               child1TransitionMatrix,
     *                               child2Partials,
     *                               child2TransitionMatrix}
     *
     * @param operations            List of 7-tuples specifying operations (input)
     * @param operationCount        Number of operations (input)
     * @param cumulativeScaleIndex  Index number of scaleBuffer to store accumulated factors (input)
     *
     */
    void updatePartials(
            final int[] operations,
            int operationCount,
            int cumulativeScaleIndex);

    /**
     * Calculate or queue for calculating partials by partition using a list of operations
     *
     * This function either calculates or queues for calculation a list partials. Implementations
     * supporting ASYNCH may queue these calculations while other implementations perform these
     * operations immediately and in order.
     *
     * If partitions have been set via setPatternPartitions, operationCount should be a
     * multiple of partitionCount.
     *
     * Operations list is a list of 9-tuple integer indices, with one 9-tuple per operation.
     * Format of 9-tuple operation: {destinationPartials,
     *                               destinationScaleWrite,
     *                               destinationScaleRead,
     *                               child1Partials,
     *                               child1TransitionMatrix,
     *                               child2Partials,
     *                               child2TransitionMatrix,
     *                               partition,
     *                               cumulativeScaleIndex}
     *
     * @param operations            List of 9-tuples specifying operations (input)
     * @param operationCount        Number of operations (input)
     *
     */
    void updatePartialsByPartition(
            final int[] operations,
            int operationCount);

    /**
     * Accumulate scale factors
     *
     * This function adds (log) scale factors from a list of scaleBuffers to a cumulative scale
     * buffer. It is used to calculate the marginal scaling at a specific node for each site.
     *
     * @param scaleIndices            	List of scaleBuffers to add (input)
     * @param count                     Number of scaleBuffers in list (input)
     * @param cumulativeScaleIndex      Index number of scaleBuffer to accumulate factors into (input)
     */
    void accumulateScaleFactors(
            final int[] scaleIndices,
            final int count,
            final int cumulativeScaleIndex
    );

    /**
     * Accumulate scale factors by partition
     *
     * This function adds (log) scale factors from a list of scaleBuffers to a cumulative scale
     * buffer. It is used to calculate the marginal scaling at a specific node for each site.
     *
     * @param scaleIndices            	List of scaleBuffers to add (input)
     * @param count                     Number of scaleBuffers in list (input)
     * @param cumulativeScaleIndex      Index number of scaleBuffer to accumulate factors into (input)
     * @param partitionIndex            Index number of partition (input)
     */
    void accumulateScaleFactorsByPartition(
            final int[] scaleIndices,
            int count,
            int cumulativeScaleIndex,
            int partitionIndex
    );

    /**
     * Remove scale factors
     *
     * This function removes (log) scale factors from a cumulative scale buffer. The
     * scale factors to be removed are indicated in a list of scaleBuffers.
     *
     * @param scaleIndices            	List of scaleBuffers to remove (input)
     * @param count                     Number of scaleBuffers in list (input)
     * @param cumulativeScaleIndex    	Index number of scaleBuffer containing accumulated factors (input)
     */
    void removeScaleFactors(
            final int[] scaleIndices,
            final int count,
            final int cumulativeScaleIndex
    );

    /**
     * Remove scale factors by partition
     *
     * This function removes (log) scale factors from a cumulative scale buffer. The
     * scale factors to be removed are indicated in a list of scaleBuffers.
     *
     * @param scaleIndices            	List of scaleBuffers to remove (input)
     * @param count                     Number of scaleBuffers in list (input)
     * @param cumulativeScaleIndex    	Index number of scaleBuffer containing accumulated factors (input)
     * @param partitionIndex            Index number of partition (input)
     */
    void removeScaleFactorsByPartition(
            final int[] scaleIndices,
            final int count,
            final int cumulativeScaleIndex,
            final int partitionIndex
    );

    /**
     * Copy scale factors
     *
     * This function copies scale factors from one buffer to another.
     *
     * @param destScalingIndex          Destination scaleBuffer (input)
     * @param srcScalingIndex           Source scaleBuffer (input)
     */
    void copyScaleFactors(
        int destScalingIndex,
        int srcScalingIndex
    );

    /**
     * Reset scalefactors
     *
     * This function resets a cumulative scale buffer.
     *
     * @param cumulativeScaleIndex    	Index number of cumulative scaleBuffer (input)
     */
    void resetScaleFactors(int cumulativeScaleIndex);

    /**
     * Reset scalefactors by partition
     *
     * This function resets a cumulative scale buffer.
     *
     * @param cumulativeScaleIndex    	Index number of cumulative scaleBuffer (input)
     * @param partitionIndex            Index number of partition (input)
     */
    void resetScaleFactorsByPartition(int cumulativeScaleIndex, int partitionIndex);

    /**
     * Calculate site log likelihoods at a root node
     *
     * This function integrates a list of partials at a node with respect to a set of partials-weights and
     * state frequencies to return the log likelihoods for each site
     *
     * @param bufferIndices             List of partialsBuffer indices to integrate (input)
     * @param categoryWeightsIndices    List of indices of category weights to apply to each partialsBuffer (input)
     *                                      should be one categoryCount sized set for each of
     *                                      parentBufferIndices
     * @param stateFrequenciesIndices   List of indices of state frequencies for each partialsBuffer (input)
     *                                      should be one set for each of parentBufferIndices
     * @param cumulativeScaleIndices    List of scalingFactors indices to accumulate over (input). There
     *                                      should be one set for each of parentBufferIndices
     * @param count                     Number of partialsBuffer to integrate (input)
     * @param outSumLogLikelihood       Pointer to destination for resulting sum of log likelihoods (output)
     */

    void calculateRootLogLikelihoods(int[] bufferIndices,
                                     int[] categoryWeightsIndices,
                                     int[] stateFrequenciesIndices,
                                     int[] cumulativeScaleIndices,
                                     int count,
                                     double[] outSumLogLikelihood);

    /**
     * Calculate site log likelihoods at a root node by partition
     *
     * This function integrates a list of partials at a node with respect to a set of partials-weights and
     * state frequencies to return the log likelihoods for each site
     *
     * @param bufferIndices             List of partialsBuffer indices to integrate (input)
     * @param categoryWeightsIndices    List of indices of category weights to apply to each partialsBuffer (input)
     *                                      should be one categoryCount sized set for each of
     *                                      parentBufferIndices
     * @param stateFrequenciesIndices   List of indices of state frequencies for each partialsBuffer (input)
     *                                      should be one set for each of parentBufferIndices
     * @param cumulativeScaleIndices    List of scalingFactors indices to accumulate over (input). There
     *                                      should be one set for each of parentBufferIndices
     * @param partitionIndices          List of partition indices indicating which sites in each
     *                                  partialsBuffer should be used (input). There should be one
     *                                  index for each of bufferIndices
     * @param partitionCount            Number of partialsBuffer to integrate (input)
     * @param count                     Number of sets of partitions to integrate across (input)
     * @param outSumLogLikelihoodByPartition     Pointer to destination for resulting sum of per partition log likelihoods (output)
     * @param outSumLogLikelihood       Pointer to destination for resulting sum of log likelihoods (output)
     */

    void calculateRootLogLikelihoodsByPartition(int[] bufferIndices,
                                     int[] categoryWeightsIndices,
                                     int[] stateFrequenciesIndices,
                                     int[] cumulativeScaleIndices,
                                     int[] partitionIndices,
                                     int partitionCount,
                                     int count,
                                     double[] outSumLogLikelihoodByPartition,
                                     double[] outSumLogLikelihood);

    /**
     * Calculate site log likelihoods and derivatives along an edge
     *
     * This function integrates at list of partials at a parent and child node with respect
     * to a set of partials-weights and state frequencies to return the log likelihoods
     * and first and second derivatives for each site
     *
     * @param parentBufferIndices       List of indices of parent partialsBuffers (input)
     * @param childBufferIndices        List of indices of child partialsBuffers (input)
     * @param probabilityIndices        List indices of transition probability matrices for this edge (input)
     * @param firstDerivativeIndices    List indices of first derivative matrices (input)
     * @param secondDerivativeIndices   List indices of second derivative matrices (input)
     * @param categoryWeightsIndices    List of indices of category weights to apply to each partialsBuffer (input)
     * @param stateFrequenciesIndices   List of indices of state frequencies for each partialsBuffer (input)
     *                                      There should be one set for each of parentBufferIndices
     * @param cumulativeScaleIndices    List of scalingFactors indices to accumulate over (input). There
     *                                      There should be one set for each of parentBufferIndices
     * @param count                     Number of partialsBuffers (input)
     * @param outSumLogLikelihood       Pointer to destination for resulting sum of log likelihoods (output)
     * @param outSumFirstDerivative     Pointer to destination for resulting sum of first derivatives (output)
     * @param outSumSecondDerivative    Pointer to destination for resulting sum of second derivatives (output)
     */

    /*void calculateEdgeLogLikelihoods(int[] parentBufferIndices,
                                     int[] childBufferIndices,
                                     int[] probabilityIndices,
                                     int[] firstDerivativeIndices,
                                     int[] secondDerivativeIndices,
                                     int[] categoryWeightsIndices,
                                     int[] stateFrequenciesIndices,
                                     int[] cumulativeScaleIndices,
                                     int count,
                                     double[] outSumLogLikelihood,
                                     double[] outSumFirstDerivative,
                                     double[] outSumSecondDerivative);*/

    /**
     * Return the individual log likelihoods for each site pattern.
     *
     * @param outLogLikelihoods an array in which the likelihoods will be put
     */
    void getSiteLogLikelihoods(double[] outLogLikelihoods);

    /**
     * Get a details class for this instance
     * @return
     */
    public InstanceDetails getDetails();
}