Search in sources :

Example 51 with BioMaterial

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);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) BioAssay(ubic.gemma.model.expression.bioAssay.BioAssay)

Example 52 with BioMaterial

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");
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence)

Example 53 with BioMaterial

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");
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) Collection(java.util.Collection)

Example 54 with BioMaterial

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");
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence)

Example 55 with BioMaterial

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);
}
Also used : BioMaterial(ubic.gemma.model.expression.biomaterial.BioMaterial) DoubleArrayList(cern.colt.list.DoubleArrayList)

Aggregations

BioMaterial (ubic.gemma.model.expression.biomaterial.BioMaterial)132 BioAssay (ubic.gemma.model.expression.bioAssay.BioAssay)67 FactorValue (ubic.gemma.model.expression.experiment.FactorValue)27 ExperimentalFactor (ubic.gemma.model.expression.experiment.ExperimentalFactor)22 CompositeSequence (ubic.gemma.model.expression.designElement.CompositeSequence)19 BioAssayDimension (ubic.gemma.model.expression.bioAssayData.BioAssayDimension)15 HashSet (java.util.HashSet)13 Test (org.junit.Test)13 ExpressionDataDoubleMatrix (ubic.gemma.core.datastructure.matrix.ExpressionDataDoubleMatrix)12 ArrayDesign (ubic.gemma.model.expression.arrayDesign.ArrayDesign)12 ExpressionExperiment (ubic.gemma.model.expression.experiment.ExpressionExperiment)10 InputStream (java.io.InputStream)7 DenseDoubleMatrix (ubic.basecode.dataStructure.matrix.DenseDoubleMatrix)7 BaseSpringContextTest (ubic.gemma.core.testing.BaseSpringContextTest)7 QuantitationType (ubic.gemma.model.common.quantitationtype.QuantitationType)7 Characteristic (ubic.gemma.model.common.description.Characteristic)6 RawExpressionDataVector (ubic.gemma.model.expression.bioAssayData.RawExpressionDataVector)6 DoubleArrayList (cern.colt.list.DoubleArrayList)5 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)5 ArrayList (java.util.ArrayList)5