2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.SequenceIdMatcher;
24 import jalview.datamodel.AlignedCodonFrame;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceDummy;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.schemes.AnnotationColourGradient;
30 import jalview.schemes.GraduatedColor;
31 import jalview.schemes.UserColourScheme;
32 import jalview.util.Format;
33 import jalview.util.MapList;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.Hashtable;
40 import java.util.Iterator;
41 import java.util.List;
43 import java.util.StringTokenizer;
44 import java.util.Vector;
47 * Parse and create Jalview Features files Detects GFF format features files and
48 * parses. Does not implement standard print() - call specific printFeatures or
49 * printGFF. Uses AlignmentI.findSequence(String id) to find the sequence object
50 * for the features annotation - this normally works on an exact match.
55 public class FeaturesFile extends AlignFile
58 * work around for GFF interpretation bug where source string becomes
59 * description rather than a group
61 private boolean doGffSource = true;
63 private int gffversion;
66 * Creates a new FeaturesFile object.
73 * Creates a new FeaturesFile object.
83 public FeaturesFile(String inFile, String type) throws IOException
88 public FeaturesFile(FileParse source) throws IOException
94 * Parse GFF or sequence features file using case-independent matching,
98 * - alignment/dataset containing sequences that are to be annotated
100 * - hashtable to store feature colour definitions
102 * - process html strings into plain text
103 * @return true if features were added
105 public boolean parse(AlignmentI align, Hashtable colours,
108 return parse(align, colours, null, removeHTML, false);
112 * Parse GFF or sequence features file optionally using case-independent
113 * matching, discarding URLs
116 * - alignment/dataset containing sequences that are to be annotated
118 * - hashtable to store feature colour definitions
120 * - process html strings into plain text
121 * @param relaxedIdmatching
122 * - when true, ID matches to compound sequence IDs are allowed
123 * @return true if features were added
125 public boolean parse(AlignmentI align, Map colours, boolean removeHTML,
126 boolean relaxedIdMatching)
128 return parse(align, colours, null, removeHTML, relaxedIdMatching);
132 * Parse GFF or sequence features file optionally using case-independent
136 * - alignment/dataset containing sequences that are to be annotated
138 * - hashtable to store feature colour definitions
140 * - hashtable to store associated URLs
142 * - process html strings into plain text
143 * @return true if features were added
145 public boolean parse(AlignmentI align, Map colours, Map featureLink,
148 return parse(align, colours, featureLink, removeHTML, false);
152 * Parse GFF or sequence features file
155 * - alignment/dataset containing sequences that are to be annotated
157 * - hashtable to store feature colour definitions
159 * - hashtable to store associated URLs
161 * - process html strings into plain text
162 * @param relaxedIdmatching
163 * - when true, ID matches to compound sequence IDs are allowed
164 * @return true if features were added
166 public boolean parse(AlignmentI align, Map colours, Map featureLink,
167 boolean removeHTML, boolean relaxedIdmatching)
173 SequenceI seq = null;
175 * keep track of any sequences we try to create from the data if it is a GFF3 file
177 ArrayList<SequenceI> newseqs = new ArrayList<SequenceI>();
178 String type, desc, token = null;
180 int index, start, end;
184 String featureGroup = null, groupLink = null;
185 Map typeLink = new Hashtable();
187 * when true, assume GFF style features rather than Jalview style.
189 boolean GFFFile = true;
190 while ((line = nextLine()) != null)
192 if (line.startsWith("#"))
197 st = new StringTokenizer(line, "\t");
198 if (st.countTokens() == 1)
200 if (line.trim().equalsIgnoreCase("GFF"))
202 // Start parsing file as if it might be GFF again.
207 if (st.countTokens() > 1 && st.countTokens() < 4)
210 type = st.nextToken();
211 if (type.equalsIgnoreCase("startgroup"))
213 featureGroup = st.nextToken();
214 if (st.hasMoreElements())
216 groupLink = st.nextToken();
217 featureLink.put(featureGroup, groupLink);
220 else if (type.equalsIgnoreCase("endgroup"))
222 // We should check whether this is the current group,
223 // but at present theres no way of showing more than 1 group
230 Object colour = null;
231 String colscheme = st.nextToken();
232 if (colscheme.indexOf("|") > -1
233 || colscheme.trim().equalsIgnoreCase("label"))
235 // Parse '|' separated graduated colourscheme fields:
236 // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]
237 // can either provide 'label' only, first is optional, next two
238 // colors are required (but may be
239 // left blank), next is optional, nxt two min/max are required.
240 // first is either 'label'
241 // first/second and third are both hexadecimal or word equivalent
243 // next two are values parsed as floats.
244 // fifth is either 'above','below', or 'none'.
245 // sixth is a float value and only required when fifth is either
246 // 'above' or 'below'.
247 StringTokenizer gcol = new StringTokenizer(colscheme, "|",
250 int threshtype = AnnotationColourGradient.NO_THRESHOLD;
251 float min = Float.MIN_VALUE, max = Float.MAX_VALUE, threshval = Float.NaN;
252 boolean labelCol = false;
254 String mincol = gcol.nextToken();
258 .println("Expected either 'label' or a colour specification in the line: "
262 String maxcol = null;
263 if (mincol.toLowerCase().indexOf("label") == 0)
266 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip
268 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
270 String abso = null, minval, maxval;
273 // at least four more tokens
274 if (mincol.equals("|"))
280 gcol.nextToken(); // skip next '|'
282 // continue parsing rest of line
283 maxcol = gcol.nextToken();
284 if (maxcol.equals("|"))
290 gcol.nextToken(); // skip next '|'
292 abso = gcol.nextToken();
293 gcol.nextToken(); // skip next '|'
294 if (abso.toLowerCase().indexOf("abso") != 0)
301 minval = gcol.nextToken();
302 gcol.nextToken(); // skip next '|'
304 maxval = gcol.nextToken();
305 if (gcol.hasMoreTokens())
307 gcol.nextToken(); // skip next '|'
311 if (minval.length() > 0)
313 min = new Float(minval).floatValue();
315 } catch (Exception e)
318 .println("Couldn't parse the minimum value for graduated colour for type ("
320 + ") - did you misspell 'auto' for the optional automatic colour switch ?");
325 if (maxval.length() > 0)
327 max = new Float(maxval).floatValue();
329 } catch (Exception e)
332 .println("Couldn't parse the maximum value for graduated colour for type ("
339 // add in some dummy min/max colours for the label-only
346 colour = new jalview.schemes.GraduatedColor(
347 new UserColourScheme(mincol).findColour('A'),
348 new UserColourScheme(maxcol).findColour('A'), min,
350 } catch (Exception e)
353 .println("Couldn't parse the graduated colour scheme ("
359 ((jalview.schemes.GraduatedColor) colour)
360 .setColourByLabel(labelCol);
361 ((jalview.schemes.GraduatedColor) colour)
362 .setAutoScaled(abso == null);
363 // add in any additional parameters
364 String ttype = null, tval = null;
365 if (gcol.hasMoreTokens())
367 // threshold type and possibly a threshold value
368 ttype = gcol.nextToken();
369 if (ttype.toLowerCase().startsWith("below"))
371 ((jalview.schemes.GraduatedColor) colour)
372 .setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
374 else if (ttype.toLowerCase().startsWith("above"))
376 ((jalview.schemes.GraduatedColor) colour)
377 .setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
381 ((jalview.schemes.GraduatedColor) colour)
382 .setThreshType(AnnotationColourGradient.NO_THRESHOLD);
383 if (!ttype.toLowerCase().startsWith("no"))
386 .println("Ignoring unrecognised threshold type : "
391 if (((GraduatedColor) colour).getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
396 tval = gcol.nextToken();
397 ((jalview.schemes.GraduatedColor) colour)
398 .setThresh(new Float(tval).floatValue());
399 } catch (Exception e)
402 .println("Couldn't parse threshold value as a float: ("
407 // parse the thresh-is-min token ?
408 if (gcol.hasMoreTokens())
411 .println("Ignoring additional tokens in parameters in graduated colour specification\n");
412 while (gcol.hasMoreTokens())
414 System.err.println("|" + gcol.nextToken());
416 System.err.println("\n");
422 UserColourScheme ucs = new UserColourScheme(colscheme);
423 colour = ucs.findColour('A');
427 colours.put(type, colour);
429 if (st.hasMoreElements())
431 String link = st.nextToken();
432 typeLink.put(type, link);
433 if (featureLink == null)
435 featureLink = new Hashtable();
437 featureLink.put(type, link);
443 while (st.hasMoreElements())
448 // Still possible this is an old Jalview file,
449 // which does not have type colours at the beginning
450 seqId = token = st.nextToken();
451 seq = findName(align, seqId, relaxedIdmatching, newseqs);
454 desc = st.nextToken();
456 if (doGffSource && desc.indexOf(' ') == -1)
458 // could also be a source term rather than description line
459 group = new String(desc);
461 type = st.nextToken();
464 String stt = st.nextToken();
465 if (stt.length() == 0 || stt.equals("-"))
471 start = Integer.parseInt(stt);
473 } catch (NumberFormatException ex)
479 String stt = st.nextToken();
480 if (stt.length() == 0 || stt.equals("-"))
486 end = Integer.parseInt(stt);
488 } catch (NumberFormatException ex)
492 // TODO: decide if non positional feature assertion for input data
493 // where end==0 is generally valid
496 // treat as non-positional feature, regardless.
501 score = new Float(st.nextToken()).floatValue();
502 } catch (NumberFormatException ex)
507 sf = new SequenceFeature(type, desc, start, end, score, group);
511 sf.setValue("STRAND", st.nextToken());
512 sf.setValue("FRAME", st.nextToken());
513 } catch (Exception ex)
517 if (st.hasMoreTokens())
519 StringBuffer attributes = new StringBuffer();
521 while (st.hasMoreTokens())
523 attributes.append((sep ? "\t" : "") + st.nextElement());
526 // TODO validate and split GFF2 attributes field ? parse out
527 // ([A-Za-z][A-Za-z0-9_]*) <value> ; and add as
528 // sf.setValue(attrib, val);
529 sf.setValue("ATTRIBUTES", attributes.toString());
532 if (processOrAddSeqFeature(align, newseqs, seq, sf, GFFFile,
535 // check whether we should add the sequence feature to any other
536 // sequences in the alignment with the same or similar
537 while ((seq = align.findName(seq, seqId, true)) != null)
539 seq.addSequenceFeature(new SequenceFeature(sf));
546 if (GFFFile && seq == null)
552 desc = st.nextToken();
554 if (!st.hasMoreTokens())
557 .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up.");
558 // in all probability, this isn't a file we understand, so bail
563 token = st.nextToken();
565 if (!token.equals("ID_NOT_SPECIFIED"))
567 seq = findName(align, seqId = token, relaxedIdmatching, null);
575 index = Integer.parseInt(st.nextToken());
576 seq = align.getSequenceAt(index);
577 } catch (NumberFormatException ex)
585 System.out.println("Sequence not found: " + line);
589 start = Integer.parseInt(st.nextToken());
590 end = Integer.parseInt(st.nextToken());
592 type = st.nextToken();
594 if (!colours.containsKey(type))
596 // Probably the old style groups file
597 UserColourScheme ucs = new UserColourScheme(type);
598 colours.put(type, ucs.findColour('A'));
600 sf = new SequenceFeature(type, desc, "", start, end, featureGroup);
601 if (st.hasMoreTokens())
605 score = new Float(st.nextToken()).floatValue();
606 // update colourgradient bounds if allowed to
607 } catch (NumberFormatException ex)
613 if (groupLink != null && removeHTML)
615 sf.addLink(groupLink);
616 sf.description += "%LINK%";
618 if (typeLink.containsKey(type) && removeHTML)
620 sf.addLink(typeLink.get(type).toString());
621 sf.description += "%LINK%";
624 parseDescriptionHTML(sf, removeHTML);
626 seq.addSequenceFeature(sf);
629 && (seq = align.findName(seq, seqId, false)) != null)
631 seq.addSequenceFeature(new SequenceFeature(sf));
633 // If we got here, its not a GFFFile
638 } catch (Exception ex)
640 // should report somewhere useful for UI if necessary
641 warningMessage = ((warningMessage == null) ? "" : warningMessage)
642 + "Parsing error at\n" + line;
643 System.out.println("Error parsing feature file: " + ex + "\n" + line);
644 ex.printStackTrace(System.err);
654 * take a sequence feature and examine its attributes to decide how it should
655 * be added to a sequence
658 * - the destination sequence constructed or discovered in the
661 * - the base feature with ATTRIBUTES property containing any
662 * additional attributes
664 * - true if we are processing a GFF annotation file
665 * @return true if sf was actually added to the sequence, false if it was
666 * processed in another way
668 public boolean processOrAddSeqFeature(AlignmentI align, List<SequenceI> newseqs, SequenceI seq, SequenceFeature sf,
669 boolean gFFFile, boolean relaxedIdMatching)
671 String attr = (String) sf.getValue("ATTRIBUTES");
673 if (gFFFile && attr != null)
677 for (String attset : attr.split("\t"))
679 if (attset==null || attset.trim().length()==0)
684 Map<String, List<String>> set = new HashMap<String, List<String>>();
685 // normally, only expect one column - 9 - in this field
686 // the attributes (Gff3) or groups (gff2) field
687 for (String pair : attset.trim().split(";"))
690 if (pair.length() == 0)
695 // expect either space seperated (gff2) or '=' separated (gff3)
696 // key/value pairs here
698 int eqpos = pair.indexOf('='),sppos = pair.indexOf(' ');
699 String key = null, value = null;
701 if (sppos > -1 && (eqpos == -1 || sppos < eqpos))
703 key = pair.substring(0, sppos);
704 value = pair.substring(sppos + 1);
706 if (eqpos > -1 && (sppos == -1 || eqpos < sppos))
708 key = pair.substring(0, eqpos);
709 value = pair.substring(eqpos + 1);
717 List<String> vals = set.get(key);
720 vals = new ArrayList<String>();
725 vals.add(value.trim());
731 add &= processGffKey(set, nattr, seq, sf, align, newseqs,
732 relaxedIdMatching); // process decides if
733 // feature is actually
735 } catch (InvalidGFF3FieldException ivfe)
737 System.err.println(ivfe);
743 seq.addSequenceFeature(sf);
748 public class InvalidGFF3FieldException extends Exception
752 public InvalidGFF3FieldException(String field,
753 Map<String, List<String>> set, String message)
755 super(message + " (Field was " + field + " and value was "
756 + set.get(field).toString());
758 this.value = set.get(field).toString();
764 * take a set of keys for a feature and interpret them
772 public boolean processGffKey(Map<String, List<String>> set, int nattr,
773 SequenceI seq, SequenceFeature sf, AlignmentI align,
774 List<SequenceI> newseqs, boolean relaxedIdMatching)
775 throws InvalidGFF3FieldException
778 // decide how to interpret according to type
779 if (sf.getType().equals("similarity"))
781 int strand = sf.getStrand();
782 // exonerate cdna/protein map
784 List<SequenceI> querySeq = findNames(align, newseqs,
785 relaxedIdMatching, set.get(attr="Query"));
786 if (querySeq==null || querySeq.size()!=1)
788 throw new InvalidGFF3FieldException( attr, set,
789 "Expecting exactly one sequence in Query field (got "
790 + set.get(attr) + ")");
792 if (set.containsKey(attr="Align"))
794 // process the align maps and create cdna/protein maps
795 // ideally, the query sequences are in the alignment, but maybe not...
797 AlignedCodonFrame alco = new AlignedCodonFrame();
798 MapList codonmapping = constructCodonMappingFromAlign(set, attr,
801 // add codon mapping, and hope!
802 alco.addMap(seq, querySeq.get(0), codonmapping);
803 align.addCodonFrame(alco);
804 // everything that's needed to be done is done
805 // no features to create here !
813 private MapList constructCodonMappingFromAlign(
814 Map<String, List<String>> set,
815 String attr, int strand) throws InvalidGFF3FieldException
819 throw new InvalidGFF3FieldException(attr, set,
820 "Invalid strand for a codon mapping (cannot be 0)");
822 List<Integer> fromrange = new ArrayList<Integer>(), torange = new ArrayList<Integer>();
823 int lastppos = 0, lastpframe = 0;
824 for (String range : set.get(attr))
826 List<Integer> ints = new ArrayList<Integer>();
827 StringTokenizer st = new StringTokenizer(range, " ");
828 while (st.hasMoreTokens())
830 String num = st.nextToken();
833 ints.add(new Integer(num));
834 } catch (NumberFormatException nfe)
836 throw new InvalidGFF3FieldException(attr, set,
837 "Invalid number in field " + num);
840 // Align positionInRef positionInQuery LengthInRef
841 // contig_1146 exonerate:protein2genome:local similarity 8534 11269
842 // 3652 - . alignment_id 0 ;
843 // Query DDB_G0269124
844 // Align 11270 143 120
845 // corresponds to : 120 bases align at pos 143 in protein to 11270 on
846 // dna in strand direction
847 // Align 11150 187 282
848 // corresponds to : 282 bases align at pos 187 in protein to 11150 on
849 // dna in strand direction
851 // Align 10865 281 888
852 // Align 9977 578 1068
853 // Align 8909 935 375
855 if (ints.size() != 3)
857 throw new InvalidGFF3FieldException(attr, set,
858 "Invalid number of fields for this attribute ("
859 + ints.size() + ")");
861 fromrange.add(new Integer(ints.get(0).intValue()));
862 fromrange.add(new Integer(ints.get(0).intValue() + strand
863 * ints.get(2).intValue()));
864 // how are intron/exon boundaries that do not align in codons
866 if (ints.get(1).equals(lastppos) && lastpframe > 0)
868 // extend existing to map
869 lastppos += ints.get(2) / 3;
870 lastpframe = ints.get(2) % 3;
871 torange.set(torange.size() - 1, new Integer(lastppos));
876 torange.add(ints.get(1));
877 lastppos = ints.get(1) + ints.get(2) / 3;
878 lastpframe = ints.get(2) % 3;
879 torange.add(new Integer(lastppos));
882 // from and to ranges must end up being a series of start/end intervals
883 if (fromrange.size() % 2 == 1)
885 throw new InvalidGFF3FieldException(attr, set,
886 "Couldn't parse the DNA alignment range correctly");
888 if (torange.size() % 2 == 1)
890 throw new InvalidGFF3FieldException(attr, set,
891 "Couldn't parse the protein alignment range correctly");
893 // finally, build the map
894 int[] frommap = new int[fromrange.size()], tomap = new int[torange
897 for (Integer ip : fromrange)
899 frommap[p++] = ip.intValue();
902 for (Integer ip : torange)
904 tomap[p++] = ip.intValue();
907 return new MapList(frommap, tomap, 3, 1);
910 private List<SequenceI> findNames(AlignmentI align,
911 List<SequenceI> newseqs, boolean relaxedIdMatching,
914 List<SequenceI> found = new ArrayList<SequenceI>();
915 for (String seqId : list)
917 SequenceI seq = findName(align, seqId, relaxedIdMatching, newseqs);
926 private AlignmentI lastmatchedAl = null;
928 private SequenceIdMatcher matcher = null;
931 * clear any temporary handles used to speed up ID matching
933 private void resetMatcher()
935 lastmatchedAl = null;
939 private SequenceI findName(AlignmentI align, String seqId,
940 boolean relaxedIdMatching, List<SequenceI> newseqs)
942 SequenceI match = null;
943 if (relaxedIdMatching)
945 if (lastmatchedAl != align)
947 matcher = new SequenceIdMatcher(
948 (lastmatchedAl = align).getSequencesArray());
951 matcher.addAll(newseqs);
954 match = matcher.findIdMatch(seqId);
958 match = align.findName(seqId, true);
961 if (match==null && newseqs!=null)
963 match = new SequenceDummy(seqId);
964 if (relaxedIdMatching)
966 matcher.addAll(Arrays.asList(new SequenceI[]
972 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
974 if (sf.getDescription() == null)
978 jalview.util.ParseHtmlBodyAndLinks parsed = new jalview.util.ParseHtmlBodyAndLinks(
979 sf.getDescription(), removeHTML, newline);
981 sf.description = (removeHTML) ? parsed.getNonHtmlContent()
983 for (String link : parsed.getLinks())
991 * generate a features file for seqs includes non-pos features by default.
994 * source of sequence features
996 * hash of feature types and colours
997 * @return features file contents
999 public String printJalviewFormat(SequenceI[] seqs, Map<String,Object> visible)
1001 return printJalviewFormat(seqs, visible, true, true);
1005 * generate a features file for seqs with colours from visible (if any)
1008 * source of features
1010 * hash of Colours for each feature type
1012 * when true only feature types in 'visible' will be output
1014 * indicates if non-positional features should be output (regardless
1016 * @return features file contents
1018 public String printJalviewFormat(SequenceI[] seqs, Map visible,
1019 boolean visOnly, boolean nonpos)
1021 StringBuffer out = new StringBuffer();
1022 SequenceFeature[] next;
1023 boolean featuresGen = false;
1024 if (visOnly && !nonpos && (visible == null || visible.size() < 1))
1026 // no point continuing.
1027 return "No Features Visible";
1030 if (visible != null && visOnly)
1032 // write feature colours only if we're given them and we are generating
1034 // TODO: decide if feature links should also be written here ?
1035 Iterator en = visible.keySet().iterator();
1037 while (en.hasNext())
1039 type = en.next().toString();
1041 if (visible.get(type) instanceof GraduatedColor)
1043 GraduatedColor gc = (GraduatedColor) visible.get(type);
1044 color = (gc.isColourByLabel() ? "label|" : "")
1045 + Format.getHexString(gc.getMinColor()) + "|"
1046 + Format.getHexString(gc.getMaxColor())
1047 + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
1048 + gc.getMax() + "|";
1049 if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
1051 if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
1057 if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
1059 System.err.println("WARNING: Unsupported threshold type ("
1060 + gc.getThreshType() + ") : Assuming 'above'");
1065 color += "|" + gc.getThresh();
1072 else if (visible.get(type) instanceof java.awt.Color)
1074 color = Format.getHexString((java.awt.Color) visible.get(type));
1078 // legacy support for integer objects containing colour triplet values
1079 color = Format.getHexString(new java.awt.Color(Integer
1080 .parseInt(visible.get(type).toString())));
1085 out.append(newline);
1088 // Work out which groups are both present and visible
1089 Vector groups = new Vector();
1091 boolean isnonpos = false;
1093 for (int i = 0; i < seqs.length; i++)
1095 next = seqs[i].getSequenceFeatures();
1098 for (int j = 0; j < next.length; j++)
1100 isnonpos = next[j].begin == 0 && next[j].end == 0;
1101 if ((!nonpos && isnonpos)
1102 || (!isnonpos && visOnly && !visible
1103 .containsKey(next[j].type)))
1108 if (next[j].featureGroup != null
1109 && !groups.contains(next[j].featureGroup))
1111 groups.addElement(next[j].featureGroup);
1117 String group = null;
1121 if (groups.size() > 0 && groupIndex < groups.size())
1123 group = groups.elementAt(groupIndex).toString();
1124 out.append(newline);
1125 out.append("STARTGROUP\t");
1127 out.append(newline);
1134 for (int i = 0; i < seqs.length; i++)
1136 next = seqs[i].getSequenceFeatures();
1139 for (int j = 0; j < next.length; j++)
1141 isnonpos = next[j].begin == 0 && next[j].end == 0;
1142 if ((!nonpos && isnonpos)
1143 || (!isnonpos && visOnly && !visible
1144 .containsKey(next[j].type)))
1146 // skip if feature is nonpos and we ignore them or if we only
1147 // output visible and it isn't non-pos and it's not visible
1152 && (next[j].featureGroup == null || !next[j].featureGroup
1158 if (group == null && next[j].featureGroup != null)
1162 // we have features to output
1164 if (next[j].description == null
1165 || next[j].description.equals(""))
1167 out.append(next[j].type + "\t");
1171 if (next[j].links != null
1172 && next[j].getDescription().indexOf("<html>") == -1)
1174 out.append("<html>");
1177 out.append(next[j].description + " ");
1178 if (next[j].links != null)
1180 for (int l = 0; l < next[j].links.size(); l++)
1182 String label = next[j].links.elementAt(l).toString();
1183 String href = label.substring(label.indexOf("|") + 1);
1184 label = label.substring(0, label.indexOf("|"));
1186 if (next[j].description.indexOf(href) == -1)
1188 out.append("<a href=\"" + href + "\">" + label + "</a>");
1192 if (next[j].getDescription().indexOf("</html>") == -1)
1194 out.append("</html>");
1200 out.append(seqs[i].getName());
1201 out.append("\t-1\t");
1202 out.append(next[j].begin);
1204 out.append(next[j].end);
1206 out.append(next[j].type);
1207 if (next[j].score != Float.NaN)
1210 out.append(next[j].score);
1212 out.append(newline);
1219 out.append("ENDGROUP\t");
1221 out.append(newline);
1229 } while (groupIndex < groups.size() + 1);
1233 return "No Features Visible";
1236 return out.toString();
1240 * generate a gff file for sequence features includes non-pos features by
1247 public String printGFFFormat(SequenceI[] seqs, Map<String,Object> visible)
1249 return printGFFFormat(seqs, visible, true, true);
1252 public String printGFFFormat(SequenceI[] seqs, Map<String,Object> visible,
1253 boolean visOnly, boolean nonpos)
1255 StringBuffer out = new StringBuffer();
1256 SequenceFeature[] next;
1259 for (int i = 0; i < seqs.length; i++)
1261 if (seqs[i].getSequenceFeatures() != null)
1263 next = seqs[i].getSequenceFeatures();
1264 for (int j = 0; j < next.length; j++)
1266 isnonpos = next[j].begin == 0 && next[j].end == 0;
1267 if ((!nonpos && isnonpos)
1268 || (!isnonpos && visOnly && !visible
1269 .containsKey(next[j].type)))
1274 source = next[j].featureGroup;
1277 source = next[j].getDescription();
1280 out.append(seqs[i].getName());
1284 out.append(next[j].type);
1286 out.append(next[j].begin);
1288 out.append(next[j].end);
1290 out.append(next[j].score);
1293 if (next[j].getValue("STRAND") != null)
1295 out.append(next[j].getValue("STRAND"));
1303 if (next[j].getValue("FRAME") != null)
1305 out.append(next[j].getValue("FRAME"));
1311 // TODO: verify/check GFF - should there be a /t here before attribute
1314 if (next[j].getValue("ATTRIBUTES") != null)
1316 out.append(next[j].getValue("ATTRIBUTES"));
1319 out.append(newline);
1325 return out.toString();
1329 * this is only for the benefit of object polymorphism - method does nothing.
1337 * this is only for the benefit of object polymorphism - method does nothing.
1339 * @return error message
1341 public String print()
1343 return "USE printGFFFormat() or printJalviewFormat()";