use of ubic.basecode.dataStructure.matrix.ObjectMatrixImpl in project Gemma by PavlidisLab.
the class ExperimentalDesignUtils method sampleInfoMatrix.
/**
* @return Experimental design matrix
*/
public static ObjectMatrix<BioMaterial, ExperimentalFactor, Object> sampleInfoMatrix(List<ExperimentalFactor> factors, List<BioMaterial> samplesUsed, Map<ExperimentalFactor, FactorValue> baselines) {
ObjectMatrix<BioMaterial, ExperimentalFactor, Object> designMatrix = new ObjectMatrixImpl<>(samplesUsed.size(), factors.size());
designMatrix.setColumnNames(factors);
int row = 0;
for (BioMaterial samp : samplesUsed) {
int col = 0;
for (ExperimentalFactor factor : factors) {
Object value = ExperimentalDesignUtils.extractFactorValueForSample(baselines, samp, factor);
designMatrix.set(row, col, value);
col++;
}
row++;
}
designMatrix.setRowNames(samplesUsed);
return designMatrix;
}
use of ubic.basecode.dataStructure.matrix.ObjectMatrixImpl in project Gemma by PavlidisLab.
the class ExperimentalDesignUtils method buildDesignMatrix.
/**
* Convert factors to a matrix usable in R. The rows are in the same order as the columns of our data matrix
* (defined by samplesUsed).
*
* @param factors in the order they will be used
* @return a design matrix
*/
public static ObjectMatrix<String, String, Object> buildDesignMatrix(List<ExperimentalFactor> factors, List<BioMaterial> samplesUsed, Map<ExperimentalFactor, FactorValue> baselines) {
ObjectMatrix<String, String, Object> designMatrix = new ObjectMatrixImpl<>(samplesUsed.size(), factors.size());
Map<ExperimentalFactor, String> factorNamesInR = new LinkedHashMap<>();
for (ExperimentalFactor factor : factors) {
factorNamesInR.put(factor, ExperimentalDesignUtils.nameForR(factor));
}
designMatrix.setColumnNames(new ArrayList<>(factorNamesInR.values()));
List<String> rowNames = new ArrayList<>();
int row = 0;
for (BioMaterial samp : samplesUsed) {
rowNames.add("biomat_" + samp.getId());
int col = 0;
for (ExperimentalFactor factor : factors) {
Object value = ExperimentalDesignUtils.extractFactorValueForSample(baselines, samp, factor);
designMatrix.set(row, col, value);
// if the value is null, we have to skip this factor, actually, but we do it later.
if (value == null) {
throw new IllegalStateException("Missing values not tolerated in design matrix");
}
col++;
}
row++;
}
//
// /*
// * Drop columns that have missing values.
// */
// List<String> toKeep = new ArrayList<String>();
// for ( int i = 0; i < designMatrix.columns(); i++ ) {
// boolean skip = false;
// Object[] column = designMatrix.getColumn( i );
// for ( Object o : column ) {
// if ( o == null ) {
// skip = true;
// }
// }
//
// if ( !skip ) {
// toKeep.add( designMatrix.getColName( i ) );
// }
// }
//
// if ( toKeep.isEmpty() ) {
// throw new IllegalStateException( "Design matrix had no columns without missing values" );
// }
//
// if ( toKeep.size() < designMatrix.columns() ) {
// designMatrix = designMatrix.subsetColumns( toKeep );
// }
designMatrix.setRowNames(rowNames);
return designMatrix;
}
use of ubic.basecode.dataStructure.matrix.ObjectMatrixImpl in project Gemma by PavlidisLab.
the class ExpressionDataBooleanMatrix method createMatrix.
/**
* Fill in the data
*/
private ObjectMatrixImpl<CompositeSequence, Integer, Boolean> createMatrix(Collection<? extends DesignElementDataVector> vectors, int maxSize) {
ObjectMatrixImpl<CompositeSequence, Integer, Boolean> mat = new ObjectMatrixImpl<>(vectors.size(), maxSize);
// initialize the matrix to false
for (int i = 0; i < mat.rows(); i++) {
for (int j = 0; j < mat.columns(); j++) {
mat.set(i, j, Boolean.FALSE);
}
}
for (int j = 0; j < mat.columns(); j++) {
mat.addColumnName(j);
}
ByteArrayConverter bac = new ByteArrayConverter();
Map<Integer, CompositeSequence> rowNames = new TreeMap<>();
for (DesignElementDataVector vector : vectors) {
BioAssayDimension dimension = vector.getBioAssayDimension();
byte[] bytes = vector.getData();
CompositeSequence designElement = vector.getDesignElement();
Integer rowIndex = this.rowElementMap.get(designElement);
assert rowIndex != null;
rowNames.put(rowIndex, designElement);
boolean[] vals = this.getVals(bac, vector, bytes);
Collection<BioAssay> bioAssays = dimension.getBioAssays();
if (bioAssays.size() != vals.length) {
throw new IllegalStateException("Expected " + vals.length + " bioassays at design element " + designElement + ", got " + bioAssays.size());
}
Iterator<BioAssay> it = bioAssays.iterator();
this.setMatBioAssayValues(mat, rowIndex, ArrayUtils.toObject(vals), bioAssays, it);
}
for (int i = 0; i < mat.rows(); i++) {
mat.addRowName(rowNames.get(i));
}
assert mat.getRowNames().size() == mat.rows();
return mat;
}
Aggregations