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.AlignmentUtils;
24 import jalview.analysis.SequenceIdMatcher;
25 import jalview.api.AlignViewportI;
26 import jalview.api.FeaturesSourceI;
27 import jalview.datamodel.AlignedCodonFrame;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.SequenceDummy;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.gff.GffHelperBase;
34 import jalview.io.gff.GffHelperFactory;
35 import jalview.io.gff.GffHelperI;
36 import jalview.schemes.AnnotationColourGradient;
37 import jalview.schemes.GraduatedColor;
38 import jalview.schemes.UserColourScheme;
39 import jalview.util.Format;
40 import jalview.util.MapList;
41 import jalview.util.ParseHtmlBodyAndLinks;
42 import jalview.util.StringUtils;
44 import java.awt.Color;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Arrays;
48 import java.util.HashMap;
49 import java.util.Iterator;
50 import java.util.List;
52 import java.util.Map.Entry;
53 import java.util.StringTokenizer;
56 * Parses and writes features files, which may be in Jalview, GFF2 or GFF3
57 * format. These are tab-delimited formats but with differences in the use of
60 * A Jalview feature file may define feature colours and then declare that the
61 * remainder of the file is in GFF format with the line 'GFF'.
63 * GFF3 files may include alignment mappings for features, which Jalview will
64 * attempt to model, and may include sequence data following a ##FASTA line.
71 public class FeaturesFile extends AlignFile implements FeaturesSourceI
73 private static final String ID_NOT_SPECIFIED = "ID_NOT_SPECIFIED";
75 private static final String NOTE = "Note";
77 protected static final String FRAME = "FRAME";
79 protected static final String TAB = "\t";
81 protected static final String GFF_VERSION = "##gff-version";
83 private AlignmentI lastmatchedAl = null;
85 private SequenceIdMatcher matcher = null;
87 protected AlignmentI dataset;
89 protected int gffVersion;
92 * Creates a new FeaturesFile object.
99 * Constructor which does not parse the file immediately
103 * @throws IOException
105 public FeaturesFile(String inFile, String type) throws IOException
107 super(false, inFile, type);
112 * @throws IOException
114 public FeaturesFile(FileParse source) throws IOException
120 * Constructor that optionally parses the file immediately
122 * @param parseImmediately
125 * @throws IOException
127 public FeaturesFile(boolean parseImmediately, String inFile, String type)
130 super(parseImmediately, inFile, type);
134 * Parse GFF or sequence features file using case-independent matching,
138 * - alignment/dataset containing sequences that are to be annotated
140 * - hashtable to store feature colour definitions
142 * - process html strings into plain text
143 * @return true if features were added
145 public boolean parse(AlignmentI align, Map<String, Object> colours,
148 return parse(align, colours, removeHTML, false);
152 * Extends the default addProperties by also adding peptide-to-cDNA mappings
153 * (if any) derived while parsing a GFF file
156 public void addProperties(AlignmentI al)
158 super.addProperties(al);
159 if (dataset != null && dataset.getCodonFrames() != null)
161 AlignmentI ds = (al.getDataset() == null) ? al : al.getDataset();
162 for (AlignedCodonFrame codons : dataset.getCodonFrames())
164 ds.addCodonFrame(codons);
170 * Parse GFF or Jalview format sequence features file
173 * - alignment/dataset containing sequences that are to be annotated
175 * - hashtable to store feature colour definitions
177 * - process html strings into plain text
178 * @param relaxedIdmatching
179 * - when true, ID matches to compound sequence IDs are allowed
180 * @return true if features were added
182 public boolean parse(AlignmentI align, Map<String, Object> colours,
183 boolean removeHTML, boolean relaxedIdmatching)
185 Map<String, String> gffProps = new HashMap<String, String>();
187 * keep track of any sequences we try to create from the data
189 List<SequenceI> newseqs = new ArrayList<SequenceI>();
195 String featureGroup = null;
197 while ((line = nextLine()) != null)
199 // skip comments/process pragmas
200 if (line.length() == 0 || line.startsWith("#"))
202 if (line.toLowerCase().startsWith("##"))
204 processGffPragma(line, gffProps, align, newseqs);
209 gffColumns = line.split("\\t"); // tab as regex
210 if (gffColumns.length == 1)
212 if (line.trim().equalsIgnoreCase("GFF"))
215 * Jalview features file with appended GFF
216 * assume GFF2 (though it may declare ##gff-version 3)
223 if (gffColumns.length > 1 && gffColumns.length < 4)
226 * if 2 or 3 tokens, we anticipate either 'startgroup', 'endgroup' or
227 * a feature type colour specification
229 String ft = gffColumns[0];
230 if (ft.equalsIgnoreCase("startgroup"))
232 featureGroup = gffColumns[1];
234 else if (ft.equalsIgnoreCase("endgroup"))
236 // We should check whether this is the current group,
237 // but at present theres no way of showing more than 1 group
242 parseFeatureColour(line, ft, gffColumns, colours);
248 * if not a comment, GFF pragma, startgroup, endgroup or feature
249 * colour specification, that just leaves a feature details line
250 * in either Jalview or GFF format
254 parseJalviewFeature(line, gffColumns, align, colours, removeHTML,
255 relaxedIdmatching, featureGroup);
259 parseGff(gffColumns, align, relaxedIdmatching, newseqs);
263 } catch (Exception ex)
265 // should report somewhere useful for UI if necessary
266 warningMessage = ((warningMessage == null) ? "" : warningMessage)
267 + "Parsing error at\n" + line;
268 System.out.println("Error parsing feature file: " + ex + "\n" + line);
269 ex.printStackTrace(System.err);
275 * experimental - add any dummy sequences with features to the alignment
276 * - we need them for Ensembl feature extraction - though maybe not otherwise
278 for (SequenceI newseq : newseqs)
280 if (newseq.getSequenceFeatures() != null)
282 align.addSequence(newseq);
289 * Try to parse a Jalview format feature specification and add it as a
290 * sequence feature to any matching sequences in the alignment. Returns true
291 * if successful (a feature was added), or false if not.
296 * @param featureColours
298 * @param relaxedIdmatching
299 * @param featureGroup
301 protected boolean parseJalviewFeature(String line, String[] gffColumns,
302 AlignmentI alignment, Map<String, Object> featureColours,
303 boolean removeHTML, boolean relaxedIdMatching, String featureGroup)
306 * tokens: description seqid seqIndex start end type [score]
308 if (gffColumns.length < 6)
310 System.err.println("Ignoring feature line '" + line
311 + "' with too few columns (" + gffColumns.length + ")");
314 String desc = gffColumns[0];
315 String seqId = gffColumns[1];
316 SequenceI seq = findSequence(seqId, alignment, null, relaxedIdMatching);
318 if (!ID_NOT_SPECIFIED.equals(seqId))
320 seq = findSequence(seqId, alignment, null, relaxedIdMatching);
326 String seqIndex = gffColumns[2];
329 int idx = Integer.parseInt(seqIndex);
330 seq = alignment.getSequenceAt(idx);
331 } catch (NumberFormatException ex)
333 System.err.println("Invalid sequence index: " + seqIndex);
339 System.out.println("Sequence not found: " + line);
343 int startPos = Integer.parseInt(gffColumns[3]);
344 int endPos = Integer.parseInt(gffColumns[4]);
346 String ft = gffColumns[5];
348 if (!featureColours.containsKey(ft))
351 * Perhaps an old style groups file with no colours -
352 * synthesize a colour from the feature type
354 UserColourScheme ucs = new UserColourScheme(ft);
355 featureColours.put(ft, ucs.findColour('A'));
357 SequenceFeature sf = new SequenceFeature(ft, desc, "", startPos,
358 endPos, featureGroup);
359 if (gffColumns.length > 6)
361 float score = Float.NaN;
364 score = new Float(gffColumns[6]).floatValue();
365 // update colourgradient bounds if allowed to
366 } catch (NumberFormatException ex)
373 parseDescriptionHTML(sf, removeHTML);
375 seq.addSequenceFeature(sf);
378 && (seq = alignment.findName(seq, seqId, false)) != null)
380 seq.addSequenceFeature(new SequenceFeature(sf));
386 * Process a feature type colour specification
389 * the current input line (for error messages only)
391 * the first token on the line
393 * holds tokens on the line
395 * map to which to add derived colour specification
397 protected void parseFeatureColour(String line, String featureType,
398 String[] gffColumns, Map<String, Object> colours)
400 Object colour = null;
401 String colscheme = gffColumns[1];
402 if (colscheme.indexOf("|") > -1
403 || colscheme.trim().equalsIgnoreCase("label"))
405 colour = parseGraduatedColourScheme(line, colscheme);
409 UserColourScheme ucs = new UserColourScheme(colscheme);
410 colour = ucs.findColour('A');
414 colours.put(featureType, colour);
419 * Parse a Jalview graduated colour descriptor
422 * @param colourDescriptor
425 protected GraduatedColor parseGraduatedColourScheme(String line,
426 String colourDescriptor)
428 // Parse '|' separated graduated colourscheme fields:
429 // [label|][mincolour|maxcolour|[absolute|]minvalue|maxvalue|thresholdtype|thresholdvalue]
430 // can either provide 'label' only, first is optional, next two
431 // colors are required (but may be
432 // left blank), next is optional, nxt two min/max are required.
433 // first is either 'label'
434 // first/second and third are both hexadecimal or word equivalent
436 // next two are values parsed as floats.
437 // fifth is either 'above','below', or 'none'.
438 // sixth is a float value and only required when fifth is either
439 // 'above' or 'below'.
440 StringTokenizer gcol = new StringTokenizer(colourDescriptor, "|", true);
442 float min = Float.MIN_VALUE, max = Float.MAX_VALUE;
443 boolean labelCol = false;
445 String mincol = gcol.nextToken();
449 .println("Expected either 'label' or a colour specification in the line: "
453 String maxcol = null;
454 if (mincol.toLowerCase().indexOf("label") == 0)
457 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null); // skip '|'
458 mincol = (gcol.hasMoreTokens() ? gcol.nextToken() : null);
460 String abso = null, minval, maxval;
463 // at least four more tokens
464 if (mincol.equals("|"))
470 gcol.nextToken(); // skip next '|'
472 // continue parsing rest of line
473 maxcol = gcol.nextToken();
474 if (maxcol.equals("|"))
480 gcol.nextToken(); // skip next '|'
482 abso = gcol.nextToken();
483 gcol.nextToken(); // skip next '|'
484 if (abso.toLowerCase().indexOf("abso") != 0)
491 minval = gcol.nextToken();
492 gcol.nextToken(); // skip next '|'
494 maxval = gcol.nextToken();
495 if (gcol.hasMoreTokens())
497 gcol.nextToken(); // skip next '|'
501 if (minval.length() > 0)
503 min = Float.valueOf(minval);
505 } catch (Exception e)
508 .println("Couldn't parse the minimum value for graduated colour for type ("
510 + ") - did you misspell 'auto' for the optional automatic colour switch ?");
515 if (maxval.length() > 0)
517 max = Float.valueOf(maxval);
519 } catch (Exception e)
522 .println("Couldn't parse the maximum value for graduated colour for type ("
523 + colourDescriptor + ")");
529 // add in some dummy min/max colours for the label-only
535 GraduatedColor colour = null;
538 colour = new GraduatedColor(
539 new UserColourScheme(mincol).findColour('A'),
540 new UserColourScheme(maxcol).findColour('A'), min, max);
541 } catch (Exception e)
543 System.err.println("Couldn't parse the graduated colour scheme ("
544 + colourDescriptor + ")");
549 colour.setColourByLabel(labelCol);
550 colour.setAutoScaled(abso == null);
551 // add in any additional parameters
552 String ttype = null, tval = null;
553 if (gcol.hasMoreTokens())
555 // threshold type and possibly a threshold value
556 ttype = gcol.nextToken();
557 if (ttype.toLowerCase().startsWith("below"))
559 colour.setThreshType(AnnotationColourGradient.BELOW_THRESHOLD);
561 else if (ttype.toLowerCase().startsWith("above"))
563 colour.setThreshType(AnnotationColourGradient.ABOVE_THRESHOLD);
567 colour.setThreshType(AnnotationColourGradient.NO_THRESHOLD);
568 if (!ttype.toLowerCase().startsWith("no"))
570 System.err.println("Ignoring unrecognised threshold type : "
575 if (colour.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
580 tval = gcol.nextToken();
581 colour.setThresh(new Float(tval).floatValue());
582 } catch (Exception e)
584 System.err.println("Couldn't parse threshold value as a float: ("
589 // parse the thresh-is-min token ?
590 if (gcol.hasMoreTokens())
593 .println("Ignoring additional tokens in parameters in graduated colour specification\n");
594 while (gcol.hasMoreTokens())
596 System.err.println("|" + gcol.nextToken());
598 System.err.println("\n");
605 * clear any temporary handles used to speed up ID matching
607 protected void resetMatcher()
609 lastmatchedAl = null;
614 * Returns a sequence matching the given id, as follows
616 * <li>strict matching is on exact sequence name</li>
617 * <li>relaxed matching allows matching on a token within the sequence name,
619 * <li>first tries to find a match in the alignment sequences</li>
620 * <li>else tries to find a match in the new sequences already generated while
621 * parsing the features file</li>
622 * <li>else creates a new placeholder sequence, adds it to the new sequences
623 * list, and returns it</li>
629 * @param relaxedIdMatching
633 protected SequenceI findSequence(String seqId, AlignmentI align,
634 List<SequenceI> newseqs, boolean relaxedIdMatching)
636 // TODO encapsulate in SequenceIdMatcher, share the matcher
637 // with the GffHelper (removing code duplication)
638 SequenceI match = null;
639 if (relaxedIdMatching)
641 if (lastmatchedAl != align)
643 lastmatchedAl = align;
644 matcher = new SequenceIdMatcher(align.getSequencesArray());
647 matcher.addAll(newseqs);
650 match = matcher.findIdMatch(seqId);
654 match = align.findName(seqId, true);
655 if (match == null && newseqs != null)
657 for (SequenceI m : newseqs)
659 if (seqId.equals(m.getName()))
667 if (match == null && newseqs != null)
669 match = new SequenceDummy(seqId);
670 if (relaxedIdMatching)
672 matcher.addAll(Arrays.asList(new SequenceI[] { match }));
674 // add dummy sequence to the newseqs list
680 public void parseDescriptionHTML(SequenceFeature sf, boolean removeHTML)
682 if (sf.getDescription() == null)
686 ParseHtmlBodyAndLinks parsed = new ParseHtmlBodyAndLinks(
687 sf.getDescription(), removeHTML, newline);
689 sf.description = (removeHTML) ? parsed.getNonHtmlContent()
691 for (String link : parsed.getLinks())
699 * generate a features file for seqs includes non-pos features by default.
702 * source of sequence features
704 * hash of feature types and colours
705 * @return features file contents
707 public String printJalviewFormat(SequenceI[] sequences,
708 Map<String, Object> visible)
710 return printJalviewFormat(sequences, visible, true, true);
714 * generate a features file for seqs with colours from visible (if any)
719 * hash of Colours for each feature type
721 * when true only feature types in 'visible' will be output
723 * indicates if non-positional features should be output (regardless
725 * @return features file contents
727 public String printJalviewFormat(SequenceI[] sequences,
728 Map<String, Object> visible, boolean visOnly, boolean nonpos)
730 StringBuilder out = new StringBuilder(256);
731 boolean featuresGen = false;
732 if (visOnly && !nonpos && (visible == null || visible.size() < 1))
734 // no point continuing.
735 return "No Features Visible";
738 if (visible != null && visOnly)
740 // write feature colours only if we're given them and we are generating
742 // TODO: decide if feature links should also be written here ?
743 Iterator<String> en = visible.keySet().iterator();
744 String featureType, color;
747 featureType = en.next().toString();
749 if (visible.get(featureType) instanceof GraduatedColor)
751 GraduatedColor gc = (GraduatedColor) visible.get(featureType);
752 color = (gc.isColourByLabel() ? "label|" : "")
753 + Format.getHexString(gc.getMinColor()) + "|"
754 + Format.getHexString(gc.getMaxColor())
755 + (gc.isAutoScale() ? "|" : "|abso|") + gc.getMin() + "|"
757 if (gc.getThreshType() != AnnotationColourGradient.NO_THRESHOLD)
759 if (gc.getThreshType() == AnnotationColourGradient.BELOW_THRESHOLD)
765 if (gc.getThreshType() != AnnotationColourGradient.ABOVE_THRESHOLD)
767 System.err.println("WARNING: Unsupported threshold type ("
768 + gc.getThreshType() + ") : Assuming 'above'");
773 color += "|" + gc.getThresh();
780 else if (visible.get(featureType) instanceof Color)
782 color = Format.getHexString((Color) visible.get(featureType));
786 // legacy support for integer objects containing colour triplet values
787 color = Format.getHexString(new Color(Integer.parseInt(visible
788 .get(featureType).toString())));
790 out.append(featureType);
796 // Work out which groups are both present and visible
797 List<String> groups = new ArrayList<String>();
799 boolean isnonpos = false;
801 SequenceFeature[] features;
802 for (int i = 0; i < sequences.length; i++)
804 features = sequences[i].getSequenceFeatures();
805 if (features != null)
807 for (int j = 0; j < features.length; j++)
809 isnonpos = features[j].begin == 0 && features[j].end == 0;
810 if ((!nonpos && isnonpos)
811 || (!isnonpos && visOnly && !visible
812 .containsKey(features[j].type)))
817 if (features[j].featureGroup != null
818 && !groups.contains(features[j].featureGroup))
820 groups.add(features[j].featureGroup);
829 if (groups.size() > 0 && groupIndex < groups.size())
831 group = groups.get(groupIndex);
833 out.append("STARTGROUP").append(TAB);
842 for (int i = 0; i < sequences.length; i++)
844 features = sequences[i].getSequenceFeatures();
845 if (features != null)
847 for (int j = 0; j < features.length; j++)
849 isnonpos = features[j].begin == 0 && features[j].end == 0;
850 if ((!nonpos && isnonpos)
851 || (!isnonpos && visOnly && !visible
852 .containsKey(features[j].type)))
854 // skip if feature is nonpos and we ignore them or if we only
855 // output visible and it isn't non-pos and it's not visible
860 && (features[j].featureGroup == null || !features[j].featureGroup
866 if (group == null && features[j].featureGroup != null)
870 // we have features to output
872 if (features[j].description == null
873 || features[j].description.equals(""))
875 out.append(features[j].type).append(TAB);
879 if (features[j].links != null
880 && features[j].getDescription().indexOf("<html>") == -1)
882 out.append("<html>");
885 out.append(features[j].description + " ");
886 if (features[j].links != null)
888 for (int l = 0; l < features[j].links.size(); l++)
890 String label = features[j].links.elementAt(l).toString();
891 String href = label.substring(label.indexOf("|") + 1);
892 label = label.substring(0, label.indexOf("|"));
894 if (features[j].description.indexOf(href) == -1)
896 out.append("<a href=\"" + href + "\">" + label + "</a>");
900 if (features[j].getDescription().indexOf("</html>") == -1)
902 out.append("</html>");
908 out.append(sequences[i].getName());
909 out.append("\t-1\t");
910 out.append(features[j].begin);
912 out.append(features[j].end);
914 out.append(features[j].type);
915 if (!Float.isNaN(features[j].score))
918 out.append(features[j].score);
927 out.append("ENDGROUP").append(TAB);
937 } while (groupIndex < groups.size() + 1);
941 return "No Features Visible";
944 return out.toString();
948 * Parse method that is called when a GFF file is dragged to the desktop
953 AlignViewportI av = getViewport();
956 if (av.getAlignment() != null)
958 dataset = av.getAlignment().getDataset();
962 // working in the applet context ?
963 dataset = av.getAlignment();
968 dataset = new Alignment(new SequenceI[] {});
971 boolean parseResult = parse(dataset, null, false, true);
974 // pass error up somehow
978 // update viewport with the dataset data ?
982 setSeqs(dataset.getSequencesArray());
987 * Implementation of unused abstract method
989 * @return error message
992 public String print()
994 return "Use printGffFormat() or printJalviewFormat()";
998 * Returns features output in GFF2 format, including hidden and non-positional
1002 * the sequences whose features are to be output
1004 * a map whose keys are the type names of visible features
1007 public String printGffFormat(SequenceI[] sequences,
1008 Map<String, Object> visible)
1010 return printGffFormat(sequences, visible, true, true);
1014 * Returns features output in GFF2 format
1017 * the sequences whose features are to be output
1019 * a map whose keys are the type names of visible features
1020 * @param outputVisibleOnly
1021 * @param includeNonPositionalFeatures
1024 public String printGffFormat(SequenceI[] sequences,
1025 Map<String, Object> visible, boolean outputVisibleOnly,
1026 boolean includeNonPositionalFeatures)
1028 StringBuilder out = new StringBuilder(256);
1029 out.append(String.format("%s %d\n", GFF_VERSION, gffVersion));
1032 for (SequenceI seq : sequences)
1034 SequenceFeature[] features = seq.getSequenceFeatures();
1035 if (features != null)
1037 for (SequenceFeature sf : features)
1039 isnonpos = sf.begin == 0 && sf.end == 0;
1040 if (!includeNonPositionalFeatures && isnonpos)
1043 * ignore non-positional features if not wanted
1047 // TODO why the test !isnonpos here?
1048 // what about not visible non-positional features?
1049 if (!isnonpos && outputVisibleOnly
1050 && !visible.containsKey(sf.type))
1053 * ignore not visible features if not wanted
1058 source = sf.featureGroup;
1061 source = sf.getDescription();
1064 out.append(seq.getName());
1068 out.append(sf.type);
1070 out.append(sf.begin);
1074 out.append(sf.score);
1077 int strand = sf.getStrand();
1078 out.append(strand == 1 ? "+" : (strand == -1 ? "-" : "."));
1081 out.append(sf.getValue(FRAME, "."));
1083 // miscellaneous key-values (GFF column 9)
1084 String attributes = sf.getAttributes();
1085 if (attributes != null)
1087 out.append(TAB).append(attributes);
1090 out.append(newline);
1095 return out.toString();
1099 * Returns a mapping given list of one or more Align descriptors (exonerate
1102 * @param alignedRegions
1103 * a list of "Align fromStart toStart fromCount"
1104 * @param mapIsFromCdna
1105 * if true, 'from' is dna, else 'from' is protein
1107 * either 1 (forward) or -1 (reverse)
1109 * @throws IOException
1111 protected MapList constructCodonMappingFromAlign(
1112 List<String> alignedRegions, boolean mapIsFromCdna, int strand)
1117 throw new IOException(
1118 "Invalid strand for a codon mapping (cannot be 0)");
1120 int regions = alignedRegions.size();
1121 // arrays to hold [start, end] for each aligned region
1122 int[] fromRanges = new int[regions * 2]; // from dna
1123 int[] toRanges = new int[regions * 2]; // to protein
1124 int fromRangesIndex = 0;
1125 int toRangesIndex = 0;
1127 for (String range : alignedRegions)
1130 * Align mapFromStart mapToStart mapFromCount
1131 * e.g. if mapIsFromCdna
1132 * Align 11270 143 120
1134 * 120 bases from pos 11270 align to pos 143 in peptide
1135 * if !mapIsFromCdna this would instead be
1136 * Align 143 11270 40
1138 String[] tokens = range.split(" ");
1139 if (tokens.length != 3)
1141 throw new IOException("Wrong number of fields for Align");
1148 fromStart = Integer.parseInt(tokens[0]);
1149 toStart = Integer.parseInt(tokens[1]);
1150 fromCount = Integer.parseInt(tokens[2]);
1151 } catch (NumberFormatException nfe)
1153 throw new IOException("Invalid number in Align field: "
1154 + nfe.getMessage());
1158 * Jalview always models from dna to protein, so adjust values if the
1159 * GFF mapping is from protein to dna
1164 int temp = fromStart;
1165 fromStart = toStart;
1168 fromRanges[fromRangesIndex++] = fromStart;
1169 fromRanges[fromRangesIndex++] = fromStart + strand * (fromCount - 1);
1172 * If a codon has an intron gap, there will be contiguous 'toRanges';
1173 * this is handled for us by the MapList constructor.
1174 * (It is not clear that exonerate ever generates this case)
1176 toRanges[toRangesIndex++] = toStart;
1177 toRanges[toRangesIndex++] = toStart + (fromCount - 1) / 3;
1180 return new MapList(fromRanges, toRanges, 3, 1);
1184 * Parse a GFF format feature. This may include creating a 'dummy' sequence to
1185 * hold the feature, or for its mapped sequence, or both, to be resolved
1186 * either later in the GFF file (##FASTA section), or when the user loads
1187 * additional sequences.
1191 * @param relaxedIdMatching
1195 protected SequenceI parseGff(String[] gffColumns, AlignmentI alignment,
1196 boolean relaxedIdMatching, List<SequenceI> newseqs)
1199 * GFF: seqid source type start end score strand phase [attributes]
1201 if (gffColumns.length < 5)
1203 System.err.println("Ignoring GFF feature line with too few columns ("
1204 + gffColumns.length + ")");
1209 * locate referenced sequence in alignment _or_
1210 * as a forward or external reference (SequenceDummy)
1212 String seqId = gffColumns[0];
1213 SequenceI seq = findSequence(seqId, alignment, newseqs,
1216 SequenceFeature sf = null;
1217 GffHelperI helper = GffHelperFactory.getHelper(gffColumns);
1222 sf = helper.processGff(seq, gffColumns, alignment, newseqs,
1226 seq.addSequenceFeature(sf);
1227 while ((seq = alignment.findName(seq, seqId, true)) != null)
1229 seq.addSequenceFeature(new SequenceFeature(sf));
1232 } catch (IOException e)
1234 System.err.println("GFF parsing failed with: " + e.getMessage());
1243 * Process the 'column 9' data of the GFF file. This is less formally defined,
1244 * and its interpretation will vary depending on the tool that has generated
1250 protected void processGffColumnNine(String attributes, SequenceFeature sf)
1252 sf.setAttributes(attributes);
1255 * Parse attributes in column 9 and add them to the sequence feature's
1256 * 'otherData' table; use Note as a best proxy for description
1258 char nameValueSeparator = gffVersion == 3 ? '=' : ' ';
1259 // TODO check we don't break GFF2 values which include commas here
1260 Map<String, List<String>> nameValues = GffHelperBase
1261 .parseNameValuePairs(attributes, ";", nameValueSeparator, ",");
1262 for (Entry<String, List<String>> attr : nameValues.entrySet())
1264 String values = StringUtils.listToDelimitedString(attr.getValue(),
1266 sf.setValue(attr.getKey(), values);
1267 if (NOTE.equals(attr.getKey()))
1269 sf.setDescription(values);
1275 * After encountering ##fasta in a GFF3 file, process the remainder of the
1276 * file as FAST sequence data. Any placeholder sequences created during
1277 * feature parsing are updated with the actual sequences.
1281 * @throws IOException
1283 protected void processAsFasta(AlignmentI align, List<SequenceI> newseqs)
1289 } catch (IOException q)
1292 FastaFile parser = new FastaFile(this);
1293 List<SequenceI> includedseqs = parser.getSeqs();
1295 SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
1298 * iterate over includedseqs, and replacing matching ones with newseqs
1299 * sequences. Generic iterator not used here because we modify
1300 * includedseqs as we go
1302 for (int p = 0, pSize = includedseqs.size(); p < pSize; p++)
1304 // search for any dummy seqs that this sequence can be used to update
1305 SequenceI includedSeq = includedseqs.get(p);
1306 SequenceI dummyseq = smatcher.findIdMatch(includedSeq);
1307 if (dummyseq != null && dummyseq instanceof SequenceDummy)
1309 // probably have the pattern wrong
1310 // idea is that a flyweight proxy for a sequence ID can be created for
1311 // 1. stable reference creation
1312 // 2. addition of annotation
1313 // 3. future replacement by a real sequence
1314 // current pattern is to create SequenceDummy objects - a convenience
1315 // constructor for a Sequence.
1316 // problem is that when promoted to a real sequence, all references
1317 // need to be updated somehow. We avoid that by keeping the same object.
1318 ((SequenceDummy) dummyseq).become(includedSeq);
1319 dummyseq.createDatasetSequence();
1322 * Update mappings so they are now to the dataset sequence
1324 for (AlignedCodonFrame mapping : align.getCodonFrames())
1326 mapping.updateToDataset(dummyseq);
1330 * replace parsed sequence with the realised forward reference
1332 includedseqs.set(p, dummyseq);
1335 * and remove from the newseqs list
1337 newseqs.remove(dummyseq);
1342 * finally add sequences to the dataset
1344 for (SequenceI seq : includedseqs)
1346 // experimental: mapping-based 'alignment' to query sequence
1347 AlignmentUtils.alignSequenceAs(seq, align,
1348 String.valueOf(align.getGapCharacter()), false, true);
1350 // rename sequences if GFF handler requested this
1351 // TODO a more elegant way e.g. gffHelper.postProcess(newseqs) ?
1352 SequenceFeature[] sfs = seq.getSequenceFeatures();
1355 String newName = (String) sfs[0].getValue(GffHelperI.RENAME_TOKEN);
1356 if (newName != null)
1358 seq.setName(newName);
1361 align.addSequence(seq);
1366 * Process a ## directive
1372 * @throws IOException
1374 protected void processGffPragma(String line,
1375 Map<String, String> gffProps, AlignmentI align,
1376 List<SequenceI> newseqs) throws IOException
1379 if ("###".equals(line))
1381 // close off any open 'forward references'
1385 String[] tokens = line.substring(2).split(" ");
1386 String pragma = tokens[0];
1387 String value = tokens.length == 1 ? null : tokens[1];
1389 if ("gff-version".equalsIgnoreCase(pragma))
1395 // value may be e.g. "3.1.2"
1396 gffVersion = Integer.parseInt(value.split("\\.")[0]);
1397 } catch (NumberFormatException e)
1403 else if ("sequence-region".equalsIgnoreCase(pragma))
1405 // could capture <seqid start end> if wanted here
1407 else if ("feature-ontology".equalsIgnoreCase(pragma))
1409 // should resolve against the specified feature ontology URI
1411 else if ("attribute-ontology".equalsIgnoreCase(pragma))
1413 // URI of attribute ontology - not currently used in GFF3
1415 else if ("source-ontology".equalsIgnoreCase(pragma))
1417 // URI of source ontology - not currently used in GFF3
1419 else if ("species-build".equalsIgnoreCase(pragma))
1421 // save URI of specific NCBI taxon version of annotations
1422 gffProps.put("species-build", value);
1424 else if ("fasta".equalsIgnoreCase(pragma))
1426 // process the rest of the file as a fasta file and replace any dummy
1428 processAsFasta(align, newseqs);
1432 System.err.println("Ignoring unknown pragma: " + line);