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.util.ArrayList;
024import java.util.LinkedHashMap;
025
026/**
027 * Data class to represent a single sample where time and event/censor status is required
028 * Additionally each variable and data associated with that variable.
029 * The code handles figuring out if a variables is continuous or categorical. If categorical will
030 * convert to numerical values.
031 *
032 * @author Scooter Willis <willishf at gmail dot com>
033 */
034public class SurvivalInfo implements Comparable<SurvivalInfo> {
035
036        private String id = "";
037        private double time;
038        private int status;
039        private int order = 0; //not really used but included to keep track of original position if sorting.
040        private double offset = 0; //offsets for linear predictor ????
041        private double weight = 1; //used to set weight of survivor for over sampling.
042        private int strata = 0; // this should be a boolean but leaving as an int
043        private double score = 0.0;
044        private double linearPredictor = 0.0;
045        private double residual = 0.0;
046        private String clusterValue = "";
047
048        LinkedHashMap<String,Double> residualVariableMap = new LinkedHashMap<String,Double>();
049
050        LinkedHashMap<String, Double> data = new LinkedHashMap<String, Double>();
051        //    LinkedHashMap<String, Double> discreteData = new LinkedHashMap<String, Double>();
052        LinkedHashMap<String, String> unknownDataType = new LinkedHashMap<String, String>();
053        LinkedHashMap<String, String> originalMetaData = new LinkedHashMap<String,String>();
054
055        /**
056         *
057         * @param t
058         * @param e
059         */
060        public SurvivalInfo(double t, int e) {
061                time = t;
062                status = e;
063
064        }
065
066        /**
067         *
068         * @param t
069         * @param e
070         * @param d
071         */
072        public SurvivalInfo(double t, int e, LinkedHashMap<String, Double> d) {
073                time = t;
074                status = e;
075
076                data = d;
077                for(String key : d.keySet()){
078                        Double value = d.get(key);
079                        originalMetaData.put(key, value + "");
080                }
081        }
082
083        /**
084         *
085         * @param t
086         * @param e
087         * @param variable
088         * @param d
089         */
090        public SurvivalInfo(double t, int e, String variable, double d) {
091                time = t;
092                status = e;
093
094                data.put(variable, d);
095                originalMetaData.put(variable, String.valueOf(d));
096        }
097
098
099        /**
100         * Set the residual value for the variable for this sample. Called from CoxScore.java
101         * @param variable
102         * @param value
103         */
104        public void setResidualVariable(String variable, Double value){
105                residualVariableMap.put(variable, value);
106        }
107
108        /**
109         *
110         * @param variable
111         * @return
112         */
113        public Double getResidualVariable(String variable){
114                return residualVariableMap.get(variable);
115
116        }
117
118        /**
119         *
120         * @param variable
121         * @return
122         */
123        public String getUnknownDataTypeVariable(String variable){
124                return unknownDataType.get(variable);
125        }
126
127        /**
128         *
129         * @param variable
130         * @return
131         */
132        public String getOriginalMetaData(String variable){
133                return originalMetaData.get(variable);
134        }
135
136        /**
137         *
138         * @param variable
139         * @param value
140         */
141        public void addUnknownDataTypeVariable(String variable, String value) {
142                originalMetaData.put(variable, value);
143                unknownDataType.put(variable, value);
144        }
145
146        /**
147         *
148         * @param variable
149         * @param value
150         */
151        public void updateContinousVariable(String variable, Double value){
152                data.put(variable, value);
153        }
154
155        /**
156         *
157         * @param variable
158         * @param value
159         */
160        public void addContinuousVariable(String variable, Double value) {
161                originalMetaData.put(variable, value + "");
162                data.put(variable, value);
163        }
164
165        /**
166         *
167         * @param variable
168         * @return
169         */
170        public Double getContinuousVariable(String variable) {
171                return data.get(variable);
172        }
173
174        /**
175         *
176         * @param groupName
177         * @return
178         */
179        public ArrayList<String> getGroupCategories(String groupName) {
180                ArrayList<String> groupNameList = new ArrayList<String>();
181                for (String key : data.keySet()) {
182                        if (key.startsWith(groupName + "_")) {
183                                groupNameList.add(key);
184                        }
185                }
186                return groupNameList;
187        }
188
189//    public void addDiscreteVariable(String variable, double value) {
190//        discreteData.put(variable, value);
191//    }
192
193//    public Double getDiscreteVariable(String variable) {
194//        return discreteData.get(variable);
195//    }
196
197        /**
198         *
199         * @return
200         */
201        public ArrayList<String> getDataVariables(){
202                ArrayList<String> v = new ArrayList<String>();
203                v.addAll(data.keySet());
204                v.addAll(unknownDataType.keySet());
205
206                return v;
207        }
208
209        /**
210         *
211         * @return
212         */
213        public int getNumberVariables(){
214                return data.size();
215        }
216
217        /**
218         *
219         * @param variable
220         * @return
221         */
222        public Double getVariable(String variable) {
223                Double value = data.get(variable);
224
225                return value;
226        }
227
228        @Override
229        public String toString() {
230                return "t=" + time + " e=" + status + " o=" + order;
231        }
232        //    double CompNum4Sort(double[] a, double[] b) {
233        //(time - time - (status -status) /1024)
234        //    return (a[0] - b[0] - (a[1] - b[1]) / 1024);
235        // }
236
237        @Override
238        public int compareTo(SurvivalInfo o) {
239                //    return (int) (this.time - o.time - (this.status - o.status) / 1024);
240                if (time < o.time) {
241                        return -1;
242                } else if (time > o.time) {
243                        return 1;
244                } else {
245                        if (this.status == o.status) {
246                                return 0;
247                        } else if (status == 1) {
248                                return -1;
249                        } else {
250                                return 1;
251                        }
252                }
253
254        }
255
256        /**
257         * @return the offset
258         */
259        public double getOffset() {
260                return offset;
261        }
262
263        /**
264         * @param offset the offset to set
265         */
266        public void setOffset(double offset) {
267                this.offset = offset;
268        }
269
270        /**
271         * @return the weight
272         */
273        public double getWeight() {
274                return weight;
275        }
276
277        /**
278         * @param weight the weight to set
279         */
280        public void setWeight(double weight) {
281                this.weight = weight;
282        }
283
284        /**
285         * @return the strata
286         */
287        public int getStrata() {
288                return strata;
289        }
290
291        /**
292         * @param strata the strata to set
293         */
294        public void setStrata(int strata) {
295                this.strata = strata;
296        }
297
298        /**
299         * @return the score
300         */
301        public double getScore() {
302                return score;
303        }
304
305        /**
306         * @param score the score to set
307         */
308        public void setScore(double score) {
309                this.score = score;
310        }
311
312        /**
313         * @return the linearPredictor
314         */
315        public double getLinearPredictor() {
316                return linearPredictor;
317        }
318
319        /**
320         * @param linearPredictor the linearPredictor to set
321         */
322        public void setLinearPredictor(double linearPredictor) {
323                this.linearPredictor = linearPredictor;
324        }
325
326        /**
327         * @return the residual
328         */
329        public double getResidual() {
330                return residual;
331        }
332
333        /**
334         * @param residual the residual to set
335         */
336        public void setResidual(double residual) {
337                this.residual = residual;
338        }
339
340        /**
341         * @return the clusterValue
342         */
343        public String getClusterValue() {
344                return clusterValue;
345        }
346
347        /**
348         * @param clusterValue the clusterValue to set
349         */
350        public void setClusterValue(String clusterValue) {
351                this.clusterValue = clusterValue;
352        }
353
354        /**
355         * @return the id
356         */
357        public String getId() {
358                return id;
359        }
360
361        /**
362         * @param id the id to set
363         */
364        public void setId(String id) {
365                this.id = id;
366        }
367
368        /**
369         * @return the order
370         */
371        public int getOrder() {
372                return order;
373        }
374
375        /**
376         * @param order the order to set
377         */
378        public void setOrder(int order) {
379                this.order = order;
380        }
381
382        /**
383         * @return the time
384         */
385        public double getTime() {
386                return time;
387        }
388
389        /**
390         * @param time the time to set
391         */
392        public void setTime(double time) {
393                this.time = time;
394        }
395
396        /**
397         * @return the status
398         */
399        public int getStatus() {
400                return status;
401        }
402
403        /**
404         * @param status the status to set
405         */
406        public void setStatus(int status) {
407                this.status = status;
408        }
409}