001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.nbio.survival.cox; 022 023import java.text.DecimalFormat; 024import java.util.LinkedHashMap; 025 026/** 027 * 028 * @author Scooter Willis <willishf at gmail dot com> 029 */ 030public class CoxVariables { 031 032 // public GeneSetResults gsr; 033 private String cohortName = ""; 034 private String geneSet = ""; 035 private String genes = ""; 036 037 /** 038 * 039 * @param cohortName 040 * @param geneSet 041 * @param genes 042 */ 043 public CoxVariables(String cohortName, String geneSet, String genes) { 044 this.cohortName = cohortName; 045 this.geneSet = geneSet; 046 this.genes = genes; 047 } 048 049 /** 050 * Need a unique id from String 051 * 052 * @return 053 */ 054 public int getUniqueID() { 055 String link = geneSet + "_" + cohortName; 056 return link.hashCode(); 057 } 058 private LinkedHashMap<String, CoxInfo> coxInfoHashMap = new LinkedHashMap<String, CoxInfo>(); 059 060 /** 061 * 062 * @param name 063 * @param coxInfo 064 */ 065 public void putCoxInfo(String name, CoxInfo coxInfo) { 066 coxInfoHashMap.put(name, coxInfo); 067 068 } 069 070 /** 071 * 072 * @param name 073 * @return 074 */ 075 public CoxInfo getCoxInfo(String name) { 076 return coxInfoHashMap.get(name); 077 078 } 079 080 /** 081 * 082 * @param file 083 * @return 084 */ 085 public String encodeFileURL(String file) { 086 file = file.replaceAll(" ", "%20"); 087 file = file.replaceAll("<", "%3C"); 088 file = file.replaceAll(">", "%3E"); 089 return file; 090 } 091 // static GeneProfiler geneProfiler = null; 092 093 /** 094 * 095 * @param d 096 * @param precision 097 * @param pad 098 * @return 099 */ 100 public static String fmt(Double d, int precision, int pad) { 101 String value = ""; 102 DecimalFormat dfe = new DecimalFormat("0.00E0"); 103 String dpad = "0."; 104 double p = 1.0; 105 for (int i = 0; i < (precision); i++) { 106 dpad = dpad + "0"; 107 p = p / 10.0; 108 } 109 DecimalFormat df = new DecimalFormat(dpad); 110 if (Math.abs(d) >= p) { 111 value = df.format(d); 112 } else { 113 value = dfe.format(d); 114 } 115 int length = value.length(); 116 int extra = pad - length; 117 if (extra > 0) { 118 for (int i = 0; i < extra; i++) { 119 value = " " + value; 120 } 121 } 122 return value; 123 } 124 125 @Override 126 public String toString() { 127 String coxOutput = geneSet + "\r\n"; 128 // co = co + genes + "\r\n"; 129 coxOutput = coxOutput + cohortName + "," + genes.replace(',', ' ') + "\r\n"; 130 coxOutput = coxOutput + ",Coe,StdErr,p-value,HR,HR Lo 95%,HR Hi 95%\r\n"; 131 for (String variables : coxInfoHashMap.keySet()) { 132 CoxInfo ci = coxInfoHashMap.get(variables); 133 134 coxOutput = coxOutput + "Overall Model Fit p-value=" + fmt(ci.getOverallModelFitPvalue(), 5, 0) + "\r\n"; 135 coxOutput = coxOutput + ci.getCoefficientText(false, "", ",", "", ""); 136 coxOutput = coxOutput + "\r\n"; 137 138 139 } 140 141 return coxOutput; 142 } 143 144 /** 145 * @return the cohortName 146 */ 147 public String getCohortName() { 148 return cohortName; 149 } 150 151 /** 152 * @return the geneSet 153 */ 154 public String getGeneSet() { 155 return geneSet; 156 } 157 158 /** 159 * @return the genes 160 */ 161 public String getGenes() { 162 return genes; 163 } 164 165 /** 166 * @return the coxInfoHashMap 167 */ 168 public LinkedHashMap<String, CoxInfo> getCoxInfoHashMap() { 169 return coxInfoHashMap; 170 } 171}