Java tutorial
/************************************************************************* * 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.List; import org.apache.commons.io.FilenameUtils; import org.ncibi.commons.file.DataFile; import org.ncibi.commons.file.ExcelFile; import org.ncibi.commons.file.TextFile; import org.ncibi.commons.lang.NumUtils; import org.ncibi.commons.lang.StrUtils; import com.google.common.base.CharMatcher; public class ConceptData { private List<Concept> allConcepts = new ArrayList<Concept>(); private String name = "(none)"; private ConceptMapping mapping = new ConceptMapping(); public static ConceptData parse(File conceptFile) { ConceptData ret = null; try { ConceptData ex = new ConceptData(); DataFile base; if (conceptFile.getName().endsWith(".xls") || conceptFile.getName().endsWith(".xlsx")) base = new ExcelFile(conceptFile); else base = new TextFile(conceptFile); ex.name = FilenameUtils.removeExtension(conceptFile.getName()); int startRow = base.getStartRowIndex(); int endRow = base.getEndRowIndex(); // first row - labels; ignore for now startRow++; for (int row = startRow; row < (endRow + 1); row++) { if (base.getString(row, 0) == null || base.getString(row, 0).equals("") || StrUtils.splitCommaOrSpaceSeparatedString(base.getString(row, 8)) == null || StrUtils.splitCommaOrSpaceSeparatedString(base.getString(row, 8)).isEmpty() || CharMatcher.WHITESPACE.trimFrom(base.getString(row, 8)).equals("")) continue; Concept rec = ex.makeRecord(); rec.setConceptName(base.getString(row, 0)); rec.setConceptType(base.getString(row, 1)); rec.setNumUniqueGenes(base.getInteger(row, 2)); rec.setCoeff(base.getDouble(row, 3)); rec.setOddsRatio(base.getDouble(row, 4)); rec.setPvalue(base.getDouble(row, 5)); rec.setFdr(base.getDouble(row, 6)); rec.setDirection(base.getString(row, 7)); rec.setGeneIdsOrSymbols( StrUtils.splitCommaOrSpaceSeparatedString(makeIdValue(base.getString(row, 8)))); ex.addRecord(rec); } ret = ex; } catch (Throwable t) { t.printStackTrace(); } return ret; } private static String makeIdValue(String string) { Integer probe = NumUtils.toInteger(string); if (probe != null) return probe.toString(); return string; } private Concept makeRecord() { return new Concept(); } private void addRecord(Concept rec) { allConcepts.add(rec); } public List<Concept> getAllConcepts() { return allConcepts; } public void setAllConcepts(List<Concept> allConcepts) { this.allConcepts = allConcepts; } public void setName(String name) { this.name = name; } public String getName() { return name; } public ConceptMapping getMapping() { return mapping; } public void setMapping(ConceptMapping mapping) { this.mapping = mapping; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((allConcepts == null) ? 0 : allConcepts.hashCode()); result = prime * result + ((name == null) ? 0 : name.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; ConceptData other = (ConceptData) obj; if (allConcepts == null) { if (other.allConcepts != null) return false; } else if (!allConcepts.equals(other.allConcepts)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public boolean isEmpty() { return allConcepts.isEmpty(); } public String toString() { return name; } }