2 * BioJava development code
4 * This code may be freely distributed and modified under the
5 * terms of the GNU Lesser General Public Licence. This should
6 * be distributed with the code. If you do not have a copy,
9 * http://www.gnu.org/copyleft/lesser.html
11 * Copyright for this code is held jointly by the individual
12 * authors. These should be listed in @author doc comments.
14 * For more information on the BioJava project and its aims,
15 * or to join the biojava-l mailing list, visit the home page
18 * http://www.biojava.org/
20 * Created on Dec 5, 2007
24 package org.biojava.dasobert.feature;
26 import java.awt.Color;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
33 * converts the features from their "raw" representation as a Map into a Feature
36 * @author Andreas Prlic
39 public class FeatureTrackConverter
42 public static final Color HELIX_COLOR = new Color(255, 51, 51);
44 public static final Color STRAND_COLOR = new Color(255, 204, 51);
46 public static final Color TURN_COLOR = new Color(204, 204, 204);
48 // some annotation types, for which there is a special treatment
49 public static final String DISULFID_TYPE = "DISULFID";
51 public static final String SECSTRUC_TYPE = "SECSTRUC";
53 public static final String METAL_TYPE = "METAL";
55 public static final String MSD_SITE_TYPE = "MSD_SITE";
59 public static final String TYPE_HISTOGRAM = "histogram";
61 public static final String TYPE_DEFAULT = "default";
63 // rotate between these colors
64 public static final Color[] entColors = new Color[]
65 { new Color(51, 51, 255), // blue
66 new Color(102, 255, 255), // cyan
67 new Color(153, 255, 153), // green
68 new Color(153, 255, 153), // green
69 new Color(255, 153, 153), // pink
70 new Color(255, 51, 51), // red
71 new Color(255, 51, 255) // pink
74 public static final String[] txtColors = new String[]
75 { "blue", "pink", "green", "yellow", "red", "cyan", "pink" };
79 boolean isHistogram = false;
81 public FeatureTrackConverter()
84 stylesheet = new Map[0];
87 public FeatureTrackConverter(Map[] stylesheet)
90 if (stylesheet == null)
91 stylesheet = new Map[0];
93 this.stylesheet = stylesheet;
97 public FeatureTrackConverter(Map[] stylesheet, boolean isHistogram)
100 this.isHistogram = isHistogram;
102 type = TYPE_HISTOGRAM;
105 public String getType()
110 public void setType(String type)
112 if (type.equals(TYPE_HISTOGRAM))
117 public boolean isHistogram()
122 public void setHistogram(boolean isHistogram)
124 this.isHistogram = isHistogram;
127 public FeatureTrack[] convertMap2Features(Map[] mapfeatures)
129 List features = new ArrayList();
131 boolean first = true;
132 boolean secstruc = false;
133 boolean isGroup = false;
135 FeatureTrack feat = null;
136 Segment segment = null;
138 int featuresCounter = 0;
139 String prevGroup = null;
141 for (int i = 0; i < mapfeatures.length; i++)
144 Map currentFeatureMap = mapfeatures[i];
145 String type = (String) currentFeatureMap.get("TYPE");
147 String group = (String) currentFeatureMap.get("GROUP");
150 if (prevGroup != null)
152 if (group.equals(prevGroup))
172 // we are skipping literature references for the moment
173 // TODO: add a display to spice for non-positional features
175 if (type.equals("reference") || type.equals("GOA"))
182 // if not first feature
184 if ((!secstruc) && (!isGroup))
187 // if not secondary structure and not in a group ...
188 features = testAddFeatures(features, feat);
191 else if (!(type.equals("HELIX") || type.equals("STRAND") || type
194 // end of secondary structure
196 if (feat != null && (!isGroup))
198 features = testAddFeatures(features, feat);
202 } // end of not first
205 if ((!secstruc) && (!isGroup))
207 featuresCounter += 1;
208 feat = getNewFeat(currentFeatureMap);
211 if (type.equals("STRAND"))
214 currentFeatureMap.put("colorTxt", "yellow");
215 feat.setName("SECSTRUC");
216 feat.setType("SECSTRUC");
219 else if (type.equals("HELIX"))
222 currentFeatureMap.put("colorTxt", "red");
223 feat.setName("SECSTRUC");
224 feat.setType("SECSTRUC");
227 else if (type.equals("TURN"))
230 currentFeatureMap.put("colorTxt", "white");
232 feat.setName("SECSTRUC");
233 feat.setType("SECSTRUC");
238 currentFeatureMap.put("colorTxt", txtColors[featuresCounter
239 % txtColors.length]);
246 } catch (NullPointerException e)
248 // e.printStackTrace();
249 feat.setName("null");
254 segment = getNewSegment(currentFeatureMap);
256 feat.addSegment(segment);
261 features = testAddFeatures(features, feat);
263 return (FeatureTrack[]) features.toArray(new FeatureTrack[features
268 * test if this features is added as a new feature to the features list, or if
269 * it is joint with an already existing one...
273 * @return a List of FeatureTrack objects
275 protected List testAddFeatures(List features, FeatureTrack newFeature)
278 // System.out.println("testing " + newFeature + " " +
279 // newFeature.getScore());
280 Iterator iter = features.iterator();
282 if (isHistogramFeatureType(newFeature))
285 // return histogram type features
286 type = TYPE_HISTOGRAM;
288 Segment seg = getHistogramSegmentFromFeature(newFeature);
290 while (iter.hasNext())
292 FeatureTrack knownFeature = (FeatureTrack) iter.next();
293 String knownType = knownFeature.getType();
295 // System.out.println("found histogram style " + feat);
296 // set type of this DAS source to being HISTOGRAM style
298 if (knownType.equals(newFeature.getType()))
300 // convert the feature into a HistogramSegment and add to the already
303 knownFeature.addSegment(seg);
309 // we could not link this to any existing feature
310 // convert it to a new HistogramFeature
311 HistogramFeature hfeat = new HistogramFeature();
313 hfeat.setLink(newFeature.getLink());
314 hfeat.setMethod(newFeature.getMethod());
315 hfeat.setName(newFeature.getName());
316 hfeat.setNote(newFeature.getNote());
318 hfeat.setSource(newFeature.getSource());
319 hfeat.addSegment(seg);
320 hfeat.setType(newFeature.getType());
323 features.add(newFeature);
327 while (iter.hasNext())
329 FeatureTrack knownFeature = (FeatureTrack) iter.next();
330 // this only compares method source and type ...
331 boolean sameFeat = false;
332 if (knownFeature.equals(newFeature))
335 if ((knownFeature.getSource().equals(newFeature.getSource()))
336 && (knownFeature.getMethod().equals(newFeature.getMethod()))
337 && (knownFeature.getNote().equals(newFeature.getNote()))
338 && isSecondaryStructureFeat(knownFeature)
339 && isSecondaryStructureFeat(newFeature))
345 // seems to be of same type, method and source, so check if the segments
348 List tmpsegs = knownFeature.getSegments();
349 Iterator segiter = tmpsegs.iterator();
350 List newsegs = newFeature.getSegments();
351 Iterator newsegsiter = newsegs.iterator();
352 boolean overlap = false;
353 while (newsegsiter.hasNext())
355 Segment newseg = (Segment) newsegsiter.next();
357 while (segiter.hasNext())
359 Segment tmpseg = (Segment) segiter.next();
361 if (tmpseg.overlaps(newseg))
368 // add all new segments to old features...
369 newsegsiter = newsegs.iterator();
370 while (newsegsiter.hasNext())
372 Segment newseg = (Segment) newsegsiter.next();
373 knownFeature.addSegment(newseg);
382 // if we get here, the features could not be joint with any other one, so
383 // there is always some overlap
384 // add to the list of known features
385 features.add(newFeature);
389 private FeatureTrack getNewFeat(Map currentFeatureMap)
391 FeatureTrack feat = new FeatureTrackImpl();
392 // logger.finest(currentFeatureMap);
393 // System.out.println("DrawableDasSource " + currentFeatureMap);
394 feat.setSource((String) currentFeatureMap.get("dassource"));
395 feat.setName((String) currentFeatureMap.get("NAME"));
396 feat.setType((String) currentFeatureMap.get("TYPE"));
397 feat.setLink((String) currentFeatureMap.get("LINK"));
398 feat.setNote((String) currentFeatureMap.get("NOTE"));
400 String typeID = (String) currentFeatureMap.get("TYPE_ID");
401 String typeCategory = (String) currentFeatureMap.get("TYPE_CATEGORY");
402 feat.setTypeID(typeID);
403 feat.setTypeCategory(typeCategory);
405 String method = (String) currentFeatureMap.get("METHOD");
410 feat.setMethod(method);
411 feat.setScore((String) currentFeatureMap.get("SCORE"));
415 private Segment getNewSegment(Map featureMap)
417 Segment s = new SegmentImpl();
418 String sstart = (String) featureMap.get("START");
419 String send = (String) featureMap.get("END");
420 int start = Integer.parseInt(sstart);
421 int end = Integer.parseInt(send);
424 s.setName((String) featureMap.get("TYPE"));
425 s.setTxtColor((String) featureMap.get("colorTxt"));
426 s.setColor((Color) featureMap.get("color"));
427 s.setNote((String) featureMap.get("NOTE"));
432 private boolean isSecondaryStructureFeat(FeatureTrack feat)
434 String type = feat.getType();
435 if (type.equals("HELIX") || type.equals("STRAND")
436 || type.equals("TURN"))
441 private boolean isHistogramFeatureType(FeatureTrack feat)
443 String ftype = feat.getType();
445 Map[] style = stylesheet;
447 // System.out.println("is HistogramFeature type " + ftype + " " + style );
449 // todo : move this info into a config file...
451 if (ftype.equals("hydrophobicity"))
455 if (getType().equals(TYPE_HISTOGRAM))
461 for (int i = 0; i < style.length; i++)
465 // make sure the stylesheet is for this feature type
466 String styleType = (String) m.get("type");
467 if (styleType != null)
469 if (!styleType.equals(ftype))
479 String type = (String) m.get("style");
482 // System.out.println("stylesheet type " + type);
483 if (type.equals("gradient") || (type.equals("lineplot"))
484 || (type.equals("histogram")))
496 private HistogramSegment getHistogramSegmentFromFeature(FeatureTrack feat)
498 HistogramSegment s = new HistogramSegment();
504 score = Double.parseDouble(feat.getScore());
506 } catch (Exception e)
508 // e.printStackTrace();
511 List segments = feat.getSegments();
512 if (segments.size() > 0)
514 Segment seg = (Segment) segments.get(0);
515 s.setName(seg.getName());
516 s.setStart(seg.getStart());
517 s.setEnd(seg.getEnd());
518 s.setNote(seg.getNote());
519 s.setColor(seg.getColor());
520 s.setTxtColor(seg.getTxtColor());