00001 package org.gel.mauve;
00002
00003 import java.awt.Component;
00004 import java.util.Collections;
00005 import java.util.Comparator;
00006 import java.util.Iterator;
00007 import java.util.LinkedList;
00008 import java.util.StringTokenizer;
00009 import java.util.Vector;
00010
00011 import javax.swing.JOptionPane;
00012
00013 import org.biojava.bio.seq.Feature;
00014 import org.biojava.bio.seq.FeatureFilter;
00015 import org.biojava.bio.seq.FeatureHolder;
00016 import org.biojava.bio.symbol.Location;
00017 import org.gel.mauve.gui.navigation.AnnotationContainsFilter;
00018 import org.gel.mauve.gui.navigation.NavigationPanel;
00019
00027 public class SeqFeatureData implements MauveConstants {
00028
00029 public static FeatureComparator feature_comp = new FeatureComparator ();
00030
00035 private SeqFeatureData () {
00036 }
00037
00048 public static FeatureFilter getFilter (String [][] data) {
00049 FeatureFilter and = null;
00050 for (int i = 0; i < data.length; i++) {
00051 StringTokenizer toke = SeqFeatureData
00052 .separateFields (SeqFeatureData
00053 .readToActual (data[i][NavigationPanel.FIELD]));
00054 FeatureFilter or = null;
00055 while (toke.hasMoreTokens ()) {
00056 FeatureFilter cur = null;
00057 cur = new AnnotationContainsFilter (toke.nextToken (),
00058 data[i][NavigationPanel.VALUE].toLowerCase (), Boolean
00059 .valueOf (data[i][NavigationPanel.EXACT])
00060 .booleanValue ());
00061 if (or != null) {
00062 or = new FeatureFilter.Or (or, cur);
00063 } else
00064 or = cur;
00065 }
00066 if (and != null)
00067 and = new FeatureFilter.And (and, or);
00068 else
00069 and = or;
00070 }
00071 and = new FeatureFilter.And (NULL_AVOIDER, and);
00072
00073 return and;
00074 }
00075
00089 public static LinkedList [] findFeatures (Genome [] nomes, String [][] data) {
00090 LinkedList [] nome_data = new LinkedList [nomes.length];
00091 FeatureFilter filter = getFilter (data);
00092 for (int i = 0; i < nomes.length; i++) {
00093 FeatureHolder hold = nomes[i].getAnnotationSequence ();
00094 hold = hold.filter (filter, true);
00095 LinkedList list = new LinkedList ();
00096 list.add (nomes[i]);
00097 Iterator itty = hold.features ();
00098 while (itty.hasNext ())
00099 list.add (itty.next ());
00100 nome_data[i] = list;
00101 }
00102 return nome_data;
00103 }
00104
00112 public static void removeLocationDuplicates (LinkedList feats) {
00113 if (feats.size () > 1) {
00114 Collections.sort (feats, feature_comp);
00115 Object first = feats.get (0);
00116 Object compare = null;
00117 int index = 1;
00118 do {
00119 compare = feats.get (index);
00120 if (feature_comp.compare (first, compare) == 0)
00121 feats.remove (index);
00122 else {
00123 first = compare;
00124 index++;
00125 }
00126 } while (index < feats.size ());
00127 }
00128 }
00129
00134 static class FeatureComparator implements Comparator {
00144 public int compare (Object a, Object b) {
00145 int one = ((Feature) a).getLocation ().getMin ();
00146 int two = ((Feature) b).getLocation ().getMin ();
00147 return (one == two) ? 0 : (one < two) ? -1 : 1;
00148 }
00149 }
00150
00157 public static StringTokenizer separateFields (String fields) {
00158 return new StringTokenizer (fields, "/", false);
00159 }
00160
00168 public static String readToActual (String field) {
00169 String actual = (String) READ_TO_ACTUAL.get (field);
00170 if (actual != null)
00171 field = actual;
00172 else
00173 field = field.toLowerCase ();
00174 return field.replace (' ', '_');
00175 }
00176
00185 public static long centerOfFeature (Feature feature) {
00186 Location loca = feature.getLocation ();
00187 return loca.getMin () + (loca.getMax () - loca.getMin ()) / 2;
00188 }
00189
00200 public static String getTypeFromFilterSpec (FilterCacheSpec spec) {
00201 if (spec.getFilter () != null)
00202 return getTypeFromFilter (spec.getFilter ());
00203 else
00204 return null;
00205 }
00206
00217 public static String getTypeFromFilter (FeatureFilter filter) {
00218 String type = null;
00219 if (filter instanceof FeatureFilter.ByType)
00220 type = ((FeatureFilter.ByType) filter).getType ();
00221 else if (filter instanceof FeatureFilter.And) {
00222 type = getTypeFromFilter (((FeatureFilter.And) filter).getChild1 ());
00223 if (type == null)
00224 type = getTypeFromFilter (((FeatureFilter.And) filter)
00225 .getChild2 ());
00226 }
00227 return type;
00228 }
00229
00245 public static Genome [] userSelectedGenomes (Component parent, BaseViewerModel model,
00246 boolean all_ok, boolean needs_annotations) {
00247 Vector choices = userSelectableGenomes (model, all_ok, needs_annotations);
00248 Object chosen = JOptionPane.showInputDialog(parent, "Choose Sequence to Navigate",
00249 "Go To. . .", JOptionPane.QUESTION_MESSAGE,
00250 null, choices.toArray(), choices.get (0));
00251 if (chosen == null)
00252 return null;
00253 else
00254 return SeqFeatureData.convertIndexToSequence (choices, choices
00255 .indexOf (chosen));
00256 }
00257
00271 public static Vector userSelectableGenomes (BaseViewerModel model,
00272 boolean all_ok, boolean needs_annotations) {
00273 Vector choices = model.getGenomes ();
00274 if (needs_annotations) {
00275 Iterator itty = choices.iterator ();
00276 while (itty.hasNext ()) {
00277 Genome genome = (Genome) itty.next ();
00278 if (genome.getAnnotationSequence () == null)
00279 itty.remove ();
00280 }
00281 }
00282 if (all_ok && choices.size () > 1)
00283 choices.add (0, ALL_SEQUENCES);
00284 return choices;
00285 }
00286
00300 public static Genome [] convertIndexToSequence (Vector choices, int index) {
00301 Genome [] nomes = null;
00302 if (index == 0 && choices.get (0) instanceof String
00303 && ((String) choices.get (0)).equals (ALL_SEQUENCES)) {
00304 int max = choices.size ();
00305 nomes = new Genome [max - 1];
00306 for (int i = 1; i < max; i++)
00307 nomes[i - 1] = (Genome) choices.get (i);
00308 } else {
00309 nomes = new Genome [1];
00310 nomes[0] = (Genome) choices.get (index);
00311 }
00312 return nomes;
00313 }
00314
00315 }