use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class ExpressionDataMatrixColumnSort method orderByExperimentalDesign.
public static <R> DoubleMatrix<R, BioAssay> orderByExperimentalDesign(DoubleMatrix<R, BioAssay> mat) {
List<BioAssay> bioAssays = mat.getColNames();
List<BioMaterial> start = new ArrayList<>();
Map<BioMaterial, BioAssay> bm2ba = new HashMap<>();
for (BioAssay bioAssay : bioAssays) {
start.add(bioAssay.getSampleUsed());
bm2ba.put(bioAssay.getSampleUsed(), bioAssay);
}
List<BioMaterial> bm = ExpressionDataMatrixColumnSort.orderByExperimentalDesign(start, null);
List<BioAssay> newBioAssayOrder = new ArrayList<>();
for (BioMaterial bioMaterial : bm) {
assert bm2ba.containsKey(bioMaterial);
newBioAssayOrder.add(bm2ba.get(bioMaterial));
}
return mat.subsetColumns(newBioAssayOrder);
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class MatrixWriter method writeWithStringifiedGeneAnnotations.
/**
* @param geneAnnotations Map of composite sequences to an array of delimited strings: [probe name,genes symbol,
* gene Name] -- these include the "|" to indicate multiple genes, and originate in the platform annotation
* files.
* @param writeHeader the writer header
* @param matrix the matrix
* @param orderByDesign if true, the columns are in the order defined by
* ExpressionDataMatrixColumnSort.orderByExperimentalDesign
* @param writeGeneInfo whether to write gene info
* @param writer the writer to use
* @param writeSequence whether to write sequence
* @throws IOException when the write failed
*/
// Possible external use
@SuppressWarnings({ "unused", "WeakerAccess" })
public void writeWithStringifiedGeneAnnotations(Writer writer, ExpressionDataMatrix<?> matrix, Map<CompositeSequence, String[]> geneAnnotations, boolean writeHeader, boolean writeSequence, boolean writeGeneInfo, boolean orderByDesign) throws IOException {
int rows = matrix.rows();
List<BioMaterial> orderedBioMaterials = this.getBioMaterialsInRequestedOrder(matrix, orderByDesign);
StringBuffer buf = new StringBuffer();
if (writeHeader) {
this.writeHeader(orderedBioMaterials, matrix, geneAnnotations, writeSequence, writeGeneInfo, buf);
}
for (int j = 0; j < rows; j++) {
CompositeSequence probeForRow = matrix.getDesignElementForRow(j);
buf.append(probeForRow.getName()).append("\t");
this.writeSequence(writeSequence, buf, probeForRow);
if (writeGeneInfo) {
this.addGeneInfoFromStrings(buf, probeForRow, geneAnnotations);
}
int orderedBioMLastIndex = orderedBioMaterials.size() - 1;
for (BioMaterial bioMaterial : orderedBioMaterials) {
int i = matrix.getColumnIndex(bioMaterial);
Object val = matrix.get(j, i);
// Don't want line to contain a trailing unnecessary tab
if (orderedBioMaterials.indexOf(bioMaterial) == orderedBioMLastIndex) {
buf.append(val);
} else {
buf.append(val).append("\t");
}
}
buf.append("\n");
}
writer.write(buf.toString());
writer.flush();
Log.debug("Done writing");
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class MatrixWriter method writeHeader.
/**
* @param geneAnnotations just passed in to check it is there.
* @see ubic.gemma.core.analysis.service.ArrayDesignAnnotationServiceImpl#readAnnotationFileAsString(ArrayDesign)
*/
private void writeHeader(List<BioMaterial> orderedBioMaterials, ExpressionDataMatrix<?> matrix, Map<CompositeSequence, ?> geneAnnotations, boolean writeSequence, boolean writeGeneInfo, StringBuffer buf) {
ExpressionDataWriterUtils.appendBaseHeader(matrix.getExpressionExperiment(), false, buf);
buf.append("Probe");
if (writeSequence)
buf.append("\tSequence");
if (writeGeneInfo && geneAnnotations != null && !geneAnnotations.isEmpty()) {
buf.append("\tGeneSymbol\tGeneName");
Object o = geneAnnotations.values().iterator().next();
if (o instanceof Collection || /* genes */
((String[]) o).length > 4) {
buf.append("\tGemmaId\tNCBIid");
}
}
for (BioMaterial bioMaterial : orderedBioMaterials) {
int i = matrix.getColumnIndex(bioMaterial);
buf.append("\t");
String colName = ExpressionDataWriterUtils.constructBioAssayName(matrix, i);
buf.append(colName);
}
buf.append("\n");
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class MatrixWriter method write.
/**
* @param orderByDesign if true, the columns are in the order defined by
* ExpressionDataMatrixColumnSort.orderByExperimentalDesign
* @param writeSequence whether to write sequence
* @param writer the writer to use
* @param writeGeneInfo whether to write gene info
* @param matrix the matrix
* @param writeHeader the writer header
* @param geneAnnotations Map of composite sequences to an array of delimited strings: [probe name,genes symbol,
* gene Name] -- these include the "|" to indicate multiple genes, and originate in the platform annotation
* files.
* @throws IOException when the write failed
*/
// Possible external use
@SuppressWarnings({ "unused", "WeakerAccess" })
public void write(Writer writer, ExpressionDataMatrix<?> matrix, Map<CompositeSequence, Collection<Gene>> geneAnnotations, boolean writeHeader, boolean writeSequence, boolean writeGeneInfo, boolean orderByDesign) throws IOException {
int rows = matrix.rows();
List<BioMaterial> bioMaterials = this.getBioMaterialsInRequestedOrder(matrix, orderByDesign);
StringBuffer buf = new StringBuffer();
if (writeHeader) {
this.writeHeader(bioMaterials, matrix, geneAnnotations, writeSequence, writeGeneInfo, buf);
}
for (int j = 0; j < rows; j++) {
CompositeSequence probeForRow = matrix.getDesignElementForRow(j);
buf.append(probeForRow.getName()).append("\t");
this.writeSequence(writeSequence, buf, probeForRow);
if (writeGeneInfo) {
this.addGeneInfo(buf, probeForRow, geneAnnotations);
}
// print the data.
for (BioMaterial bioMaterial : bioMaterials) {
buf.append("\t");
int i = matrix.getColumnIndex(bioMaterial);
Object val = matrix.get(j, i);
if (val == null || (val instanceof Double && Double.isNaN((Double) val))) {
buf.append("");
} else if (val instanceof Double) {
buf.append(String.format("%.3g", (Double) val));
} else {
buf.append(val);
}
}
buf.append("\n");
}
writer.write(buf.toString());
writer.flush();
Log.debug("Done writing");
}
use of ubic.gemma.model.expression.biomaterial.BioMaterial in project Gemma by PavlidisLab.
the class DataUpdater method matchBioMaterialsToColNames.
private void matchBioMaterialsToColNames(ExpressionExperiment ee, DoubleMatrix<String, String> rawMatrix, DoubleMatrix<CompositeSequence, BioMaterial> finalMatrix) {
// match column names to the samples. can have any order so be careful.
List<String> colNames = rawMatrix.getColNames();
Map<String, BioMaterial> bmMap = this.makeBioMaterialNameMap(ee);
List<BioMaterial> newColNames = new ArrayList<>();
for (String colName : colNames) {
BioMaterial bm = bmMap.get(colName);
if (bm == null) {
throw new IllegalStateException("Could not match a column name to a biomaterial: " + colName + "; Available keys were:\n" + StringUtils.join(bmMap.keySet(), "\n"));
}
newColNames.add(bm);
}
finalMatrix.setColumnNames(newColNames);
}
Aggregations