use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk-protected by broadinstitute.
the class SparkConverter method convertSparkMatrixToRealMatrix.
/**
* Convert a local (not distributed) Spark Matrix to an Apache Commons matrix.
*
* @param r Never {@code null}
* @return Not {@code null}
*/
public static RealMatrix convertSparkMatrixToRealMatrix(final Matrix r) {
final RealMatrix result = new Array2DRowRealMatrix(r.numRows(), r.numCols());
final double[] columnMajorMat = r.toArray();
for (int i = 0; i < r.numRows(); i++) {
result.setRow(i, Arrays.copyOfRange(columnMajorMat, i * r.numCols(), i * r.numCols() + r.numCols()));
}
return result;
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk-protected by broadinstitute.
the class HDF5PCACoveragePoNCreationUtilsUnitTest method simpleEigensampleData.
@DataProvider(name = "singleEigensample")
public Object[][] simpleEigensampleData() {
final List<Object[]> result = new ArrayList<>();
final int NUM_TARGETS = 10;
final int NUM_SAMPLES = 5;
final List<Target> targets = IntStream.range(0, NUM_TARGETS).boxed().map(i -> new Target("target_" + i, new SimpleInterval("1", 100 * i + 1, 100 * i + 5))).collect(Collectors.toList());
final List<String> columnNames = IntStream.range(0, NUM_SAMPLES).boxed().map(i -> "sample_" + i).collect(Collectors.toList());
double[][] countsArray = new double[NUM_TARGETS][NUM_SAMPLES];
final RealMatrix counts = new Array2DRowRealMatrix(countsArray);
// All row data is the same (0,1,2,3,4...)
final double[] rowData = IntStream.range(0, NUM_SAMPLES).boxed().mapToDouble(i -> i).toArray();
for (int i = 0; i < NUM_TARGETS; i++) {
counts.setRow(i, rowData);
}
new ReadCountCollection(targets, columnNames, counts);
result.add(new Object[] { new ReadCountCollection(targets, columnNames, counts) });
return result.toArray(new Object[result.size()][]);
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class HDF5PCACoveragePoNCreationUtilsUnitTest method readCountAndPercentileData.
// this is duplicated from ReadCountCollectionUtilsUnitTest
@DataProvider(name = "readCountAndPercentileData")
public Object[][] readCountAndPercentileData() {
final double[] percentiles = new double[] { 1.0, 2.5, 5.0, 10.0, 25.0 };
final List<Object[]> result = new ArrayList<>();
final Random rdn = new Random(13);
final int columnCount = 100;
final int targetCount = 100;
final List<String> columnNames = IntStream.range(0, columnCount).mapToObj(i -> "sample_" + (i + 1)).collect(Collectors.toList());
final List<Target> targets = IntStream.range(0, targetCount).mapToObj(i -> new Target("target_" + (i + 1))).collect(Collectors.toList());
for (final double percentile : percentiles) {
final double[][] counts = new double[columnCount][targetCount];
for (int i = 0; i < counts.length; i++) {
for (int j = 0; j < counts[0].length; j++) {
counts[i][j] = rdn.nextDouble();
}
}
final ReadCountCollection readCounts = new ReadCountCollection(targets, columnNames, new Array2DRowRealMatrix(counts, false));
result.add(new Object[] { readCounts, percentile });
}
return result.toArray(new Object[result.size()][]);
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class PoNTestUtils method readTsvIntoMatrix.
/**
* Reads a very basic tsv (numbers separated by tabs) into a RealMatrix.
* <p>Very little error checking happens in this method</p>
*
* @param inputFile readable file. Not {@code null}
* @return never {@code null}
*/
public static RealMatrix readTsvIntoMatrix(final File inputFile) {
IOUtils.canReadFile(inputFile);
final List<double[]> allData = new ArrayList<>();
int ctr = 0;
try {
final CSVReader reader = new CSVReader(new FileReader(inputFile), '\t', CSVWriter.NO_QUOTE_CHARACTER);
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
ctr++;
allData.add(Arrays.stream(nextLine).filter(s -> StringUtils.trim(s).length() > 0).map(s -> Double.parseDouble(StringUtils.trim(s))).mapToDouble(d -> d).toArray());
}
} catch (final IOException ioe) {
Assert.fail("Could not open test file: " + inputFile, ioe);
}
final RealMatrix result = new Array2DRowRealMatrix(allData.size(), allData.get(0).length);
for (int i = 0; i < result.getRowDimension(); i++) {
result.setRow(i, allData.get(i));
}
return result;
}
use of org.apache.commons.math3.linear.Array2DRowRealMatrix in project gatk by broadinstitute.
the class HDF5PCACoveragePoNCreationUtilsUnitTest method readCountOnlyData.
@DataProvider(name = "readCountOnlyData")
public Object[][] readCountOnlyData() {
final int repeats = 4;
final List<Object[]> result = new ArrayList<>(repeats);
final Random rdn = new Random(13);
final int columnCount = 100;
final int targetCount = 100;
final List<String> columnNames = IntStream.range(0, columnCount).mapToObj(i -> "sample_" + (i + 1)).collect(Collectors.toList());
final List<Target> targets = IntStream.range(0, targetCount).mapToObj(i -> new Target("target_" + (i + 1))).collect(Collectors.toList());
for (int k = 0; k < repeats; k++) {
final double[][] counts = new double[columnCount][targetCount];
for (int i = 0; i < counts.length; i++) {
for (int j = 0; j < counts[0].length; j++) {
counts[i][j] = rdn.nextDouble();
}
}
final ReadCountCollection readCounts = new ReadCountCollection(targets, columnNames, new Array2DRowRealMatrix(counts, false));
result.add(new Object[] { readCounts });
}
return result.toArray(new Object[result.size()][]);
}
Aggregations