org.cytoscape.MetScape.data.CorrelationData.java Source code

Java tutorial

Introduction

Here is the source code for org.cytoscape.MetScape.data.CorrelationData.java

Source

/*************************************************************************
 * Copyright 2012 Regents of the University of Michigan 
 * 
 * NCIBI - The National Center for Integrative Biomedical Informatics (NCIBI)
 *         http://www.ncib.org.
 * 
 * This product may includes software developed by others; in that case see specific notes in the code. 
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version, along with the following terms:
 * 1.   You may convey a work based on this program in accordance with section 5, 
 *      provided that you retain the above notices.
 * 2.   You may convey verbatim copies of this program code as you receive it, 
 *      in any medium, provided that you retain the above notices.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * See the GNU General Public License for more details, http://www.gnu.org/licenses/.
 * 
 * This work was supported in part by National Institutes of Health Grant #U54DA021519
 *
 ******************************************************************/
package org.cytoscape.MetScape.data;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import org.apache.commons.io.FilenameUtils;
import org.cytoscape.MetScape.app.MetScapeApp;
import org.ncibi.commons.file.DataFile;
import org.ncibi.commons.file.ExcelFile;
import org.ncibi.commons.file.TextFile;

import com.google.common.base.CharMatcher;

public class CorrelationData implements MultiColumnData {

    private static final int NUMCOLS = 3;

    private String[] columns = new String[0];
    private Map<String, Boolean> columnIsSigned = new HashMap<String, Boolean>();
    private List<String> sourceNameOrId = new ArrayList<String>();
    private List<String> targetNameOrId = new ArrayList<String>();
    private List<String> uniqueNameOrId = new ArrayList<String>();
    private Map<String, String> uniqueIdToInputId = new HashMap<String, String>();
    private List<Double> edgeData = new ArrayList<Double>();
    private String name = "(none)";
    private CorrelationMapping mapping = new CorrelationMapping();

    public static CorrelationData parse(File correlationFile) {
        CorrelationData ret = null;
        try {
            CorrelationData ex = new CorrelationData();
            DataFile base;
            if (correlationFile.getName().endsWith(".xls") || correlationFile.getName().endsWith(".xlsx"))
                base = new ExcelFile(correlationFile);
            else
                base = new TextFile(correlationFile);
            if (base.getEndColIndex(0) != NUMCOLS - 1) {
                showFormatErrorMessage();
                return null;
            }
            ex.name = FilenameUtils.removeExtension(correlationFile.getName());
            int startRow = base.getStartRowIndex();
            int endRow = base.getEndRowIndex();
            int edgeDataCol = 0;
            for (int i = 1; i <= base.getEndColIndex(0); i++) {
                if (isDataColumn(i, base)) {
                    edgeDataCol = i;
                    break;
                }
            }

            if (edgeDataCol == 0) {
                showFormatErrorMessage();
                return null;
            } else {
                ex.columns = new String[1];
                String s = base.getString(0, edgeDataCol);
                ex.columns[0] = CharMatcher.WHITESPACE.trimFrom(s);
                ex.columnIsSigned.put(CharMatcher.WHITESPACE.trimFrom(s), false);
            }

            int sourceNodeCol = -1;
            int targetNodeCol = -1;
            for (int i = 0; i < NUMCOLS; i++) {
                if (i == edgeDataCol) {
                    continue;
                }
                if (sourceNodeCol == -1) {
                    sourceNodeCol = i;
                } else {
                    targetNodeCol = i;
                }
            }

            // get remaining rows, the data
            startRow++;
            for (int row = startRow; row < (endRow + 1); row++) {
                String sourceNameOrId = base.getString(row, sourceNodeCol);
                if (sourceNameOrId == null) {
                    continue;
                }
                sourceNameOrId = CharMatcher.WHITESPACE.trimFrom(sourceNameOrId);
                String targetNameOrId = base.getString(row, targetNodeCol);
                if (targetNameOrId == null) {
                    targetNameOrId = "";
                } else {
                    targetNameOrId = CharMatcher.WHITESPACE.trimFrom(targetNameOrId);
                }
                Double data = base.getDouble(row, edgeDataCol);
                if (data == null) {
                    data = Double.NaN;
                } else if (data < 0) {
                    ex.columnIsSigned.put(ex.columns[0], true);
                    // force negative correlations to 0
                    data = 0.0;
                }
                ex.addRecord(sourceNameOrId, targetNameOrId, data);
            }
            ret = ex;
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return ret;
    }

    private static void showFormatErrorMessage() {
        JOptionPane.showMessageDialog(MetScapeApp.getDesktop().getJFrame(),
                "Invalid file format.  Correlation files must consist of two columns of compound names and "
                        + "one column of correlations between the compounds.");
    }

    private static boolean isDataColumn(int col, DataFile base) {
        int numCount = 0;
        for (int row = 1; row <= base.getEndRowIndex(); row++) {
            if (base.getDouble(row, col) != null)
                numCount++;
        }
        return (double) numCount / (double) base.getEndRowIndex() > 0.5;
    }

    private void addRecord(String sourceNameOrId, String targetNameOrId, Double data) {
        this.sourceNameOrId.add(sourceNameOrId);
        this.targetNameOrId.add(targetNameOrId);
        this.edgeData.add(data);
        addUniqueNodeInfo(sourceNameOrId);
        addUniqueNodeInfo(targetNameOrId);
    }

    private void addUniqueNodeInfo(String nodeId) {
        if (nodeId == null || CharMatcher.WHITESPACE.trimFrom(nodeId).isEmpty()) {
            return;
        }
        String lowerCaseId = CharMatcher.WHITESPACE.trimFrom(nodeId).toLowerCase();
        if (!uniqueNameOrId.contains(lowerCaseId)) {
            uniqueNameOrId.add(lowerCaseId);
        }
        if (!uniqueIdToInputId.containsKey(lowerCaseId)) {
            uniqueIdToInputId.put(lowerCaseId, CharMatcher.WHITESPACE.trimFrom(nodeId));
        }
        return;
    }

    public String[] getColumns() {
        return columns;
    }

    public void setColumns(String[] columns) {
        this.columns = columns;
    }

    public Map<String, Boolean> getColumnIsSigned() {
        return columnIsSigned;
    }

    public void setColumnIsSigned(Map<String, Boolean> columnIsSigned) {
        this.columnIsSigned = columnIsSigned;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFullyQualifiedName() {
        return "Correlation." + name;
    }

    public List<String> getSourceNameOrId() {
        return sourceNameOrId;
    }

    public void setSourceNameOrId(List<String> sourceNameOrId) {
        this.sourceNameOrId = sourceNameOrId;
    }

    public List<String> getTargetNameOrId() {
        return targetNameOrId;
    }

    public void setTargetNameOrId(List<String> targetNameOrId) {
        this.targetNameOrId = targetNameOrId;
    }

    public List<String> getUniqueNameOrId() {
        return uniqueNameOrId;
    }

    public void setUniqueNameOrId(List<String> uniqueNameOrId) {
        this.uniqueNameOrId = uniqueNameOrId;
    }

    public Map<String, String> getUniqueIdToInputId() {
        return uniqueIdToInputId;
    }

    public void setUniqueIdToInputId(Map<String, String> uniqueIdToInputId) {
        this.uniqueIdToInputId = uniqueIdToInputId;
    }

    public List<Double> getData() {
        return edgeData;
    }

    public void setData(List<Double> edgeData) {
        this.edgeData = edgeData;
    }

    public CorrelationMapping getMapping() {
        return mapping;
    }

    public void setMapping(CorrelationMapping mapping) {
        this.mapping = mapping;
    }

    public boolean isEmpty() {
        return sourceNameOrId.isEmpty();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((columnIsSigned == null) ? 0 : columnIsSigned.hashCode());
        result = prime * result + Arrays.hashCode(columns);
        result = prime * result + ((edgeData == null) ? 0 : edgeData.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((sourceNameOrId == null) ? 0 : sourceNameOrId.hashCode());
        result = prime * result + ((targetNameOrId == null) ? 0 : targetNameOrId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        CorrelationData other = (CorrelationData) obj;
        if (columnIsSigned == null) {
            if (other.columnIsSigned != null)
                return false;
        } else if (!columnIsSigned.equals(other.columnIsSigned))
            return false;
        if (!Arrays.equals(columns, other.columns))
            return false;
        if (edgeData == null) {
            if (other.edgeData != null)
                return false;
        } else if (!edgeData.equals(other.edgeData))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (sourceNameOrId == null) {
            if (other.sourceNameOrId != null)
                return false;
        } else if (!sourceNameOrId.equals(other.sourceNameOrId))
            return false;
        if (targetNameOrId == null) {
            if (other.targetNameOrId != null)
                return false;
        } else if (!targetNameOrId.equals(other.targetNameOrId))
            return false;
        return true;
    }

    public String toString() {
        return name;
    }
}