ubic.gemma.datastructure.matrix.ExpressionDataWriterUtils.java Source code

Java tutorial

Introduction

Here is the source code for ubic.gemma.datastructure.matrix.ExpressionDataWriterUtils.java

Source

/*
 * The Gemma project
 * 
 * Copyright (c) 2008 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubic.gemma.datastructure.matrix;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import ubic.gemma.analysis.service.ExpressionDataFileService;
import ubic.gemma.model.common.description.Characteristic;
import ubic.gemma.model.expression.bioAssay.BioAssay;
import ubic.gemma.model.expression.biomaterial.BioMaterial;
import ubic.gemma.model.expression.experiment.ExpressionExperiment;
import ubic.gemma.model.expression.experiment.FactorValue;
import ubic.gemma.util.DateUtil;

/**
 * @author keshav
 * @version $Id: ExpressionDataWriterUtils.java,v 1.13 2012/07/17 23:49:50 tvrossum Exp $
 */
public class ExpressionDataWriterUtils {

    public static final String DELIMITER_BETWEEN_BIOMATERIAL_AND_BIOASSAYS = "___";
    private static final String WEBSITE = "http://chibi.ubc.ca/Gemma";

    /**
     * Appends base header information (about the experiment) to a file.
     * 
     * @param experiment
     * @param design
     * @param buf
     */
    public static void appendBaseHeader(ExpressionExperiment experiment, boolean design, StringBuffer buf) {
        String fileType = "data";

        if (design)
            fileType = "design";

        buf.append("# Expression " + fileType + " file generated by Gemma on "
                + DateUtil.convertDateToString(new Date()) + "\n");
        if (experiment != null) {
            buf.append("# shortName=" + experiment.getShortName() + "\n");
            buf.append("# name=" + experiment.getName() + "\n");
            buf.append("# Experiment details: " + WEBSITE
                    + "/expressionExperiment/showExpressionExperiment.html?id=" + experiment.getId() + "\n");
        }

        buf.append(ExpressionDataFileService.DISCLAIMER);

    }

    /**
     * Constructs a bioassay name. This is useful when writing out data to a file.
     * 
     * @param matrix
     * @param assayColumnIndex The column index in the matrix.
     * @return
     */
    public static String constructBioAssayName(ExpressionDataMatrix<?> matrix, int assayColumnIndex) {

        BioMaterial bioMaterialForColumn = matrix.getBioMaterialForColumn(assayColumnIndex);
        Collection<BioAssay> bioAssaysForColumn = matrix.getBioAssaysForColumn(assayColumnIndex);

        return constructBioAssayName(bioMaterialForColumn, bioAssaysForColumn);

    }

    /**
     * @param bioMaterial
     * @param bioAssays
     * @return
     */
    public static String constructBioAssayName(BioMaterial bioMaterial, Collection<BioAssay> bioAssays) {
        StringBuffer colBuf = new StringBuffer();
        colBuf.append(bioMaterial.getName() + DELIMITER_BETWEEN_BIOMATERIAL_AND_BIOASSAYS);

        colBuf.append(StringUtils.join(bioAssays, "."));

        String colName = StringUtils.deleteWhitespace(colBuf.toString());

        String rCompatibleColName = constructRCompatibleBioAssayName(colName);

        return rCompatibleColName;
    }

    /**
     * @param colName
     * @return
     */
    private static String constructRCompatibleBioAssayName(String colName) {
        String colNameMod = colName;
        colNameMod = StringUtils.replaceChars(colNameMod, ':', '.');
        colNameMod = StringUtils.replaceChars(colNameMod, '|', '.');
        colNameMod = StringUtils.replaceChars(colNameMod, '-', '.');
        return colNameMod;
    }

    /**
     * Replaces spaces and hyphens with underscores.
     * 
     * @param factor
     * @param factorValue
     * @return
     */
    public static String constructFactorValueName(FactorValue factorValue) {

        StringBuilder buf = new StringBuilder();

        if (factorValue.getCharacteristics().size() > 0) {
            for (Characteristic c : factorValue.getCharacteristics()) {
                buf.append(StringUtils.strip(c.getValue()));
                if (factorValue.getCharacteristics().size() > 1)
                    buf.append(" | ");
            }
        } else if (factorValue.getMeasurement() != null) {
            buf.append(factorValue.getMeasurement().getValue());
        } else if (StringUtils.isNotBlank(factorValue.getValue())) {
            buf.append(StringUtils.strip(factorValue.getValue()));
        }

        String matchedFactorValue = buf.toString();

        matchedFactorValue = matchedFactorValue.trim();
        matchedFactorValue = matchedFactorValue.replaceAll("-", "_");
        matchedFactorValue = matchedFactorValue.replaceAll("\\s", "_");
        return matchedFactorValue;
    }

    /**
     * String representing the external identifier of the biomaterial. This will usually be a GEO or ArrayExpression
     * accession id, or else blank.
     * 
     * @param bioMaterial
     * @param bioAssays
     * @return
     */
    public static String getExternalId(BioMaterial bioMaterial, Collection<BioAssay> bioAssays) {
        String name = "";

        if (bioMaterial.getExternalAccession() != null) {
            name = bioMaterial.getExternalAccession().getAccession();
        } else if (StringUtils.isBlank(name) && !bioAssays.isEmpty()) {
            List<String> ids = new ArrayList<String>();
            for (BioAssay ba : bioAssays) {
                if (ba.getAccession() != null) {
                    ids.add(ba.getAccession().getAccession());
                }
            }

            name = StringUtils.join(ids, "/");
        }

        name = StringUtils.isBlank(name) ? "" : name;

        return constructRCompatibleBioAssayName(name);
    }

}