X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FFeaturesFile.java;h=aa21b0f653f870544c337266c37e8fe8f1fe5fe1;hb=4435c731351c802772dd1355fdb5747cfacd7838;hp=99663c8bb87bf1139827b76bbb23ec580ebb80b5;hpb=19b1c75919e4d2f255cbaf811dc15d26850490e6;p=jalview.git diff --git a/src/jalview/io/FeaturesFile.java b/src/jalview/io/FeaturesFile.java index 99663c8..aa21b0f 100755 --- a/src/jalview/io/FeaturesFile.java +++ b/src/jalview/io/FeaturesFile.java @@ -24,6 +24,7 @@ import jalview.analysis.AlignmentUtils; import jalview.analysis.SequenceIdMatcher; import jalview.api.AlignViewportI; import jalview.api.FeatureColourI; +import jalview.api.FeatureRenderer; import jalview.api.FeaturesSourceI; import jalview.datamodel.AlignedCodonFrame; import jalview.datamodel.Alignment; @@ -84,8 +85,6 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI private static final String NOTE = "Note"; - protected static final String TAB = "\t"; - protected static final String GFF_VERSION = "##gff-version"; private AlignmentI lastmatchedAl = null; @@ -106,11 +105,11 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI /** * Constructor which does not parse the file immediately * - * @param file + * @param file File or String filename * @param paste * @throws IOException */ - public FeaturesFile(String file, DataSourceType paste) + public FeaturesFile(Object file, DataSourceType paste) throws IOException { super(false, file, paste); @@ -133,7 +132,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI * @param type * @throws IOException */ - public FeaturesFile(boolean parseImmediately, String file, + public FeaturesFile(boolean parseImmediately, Object file, DataSourceType type) throws IOException { super(parseImmediately, file, type); @@ -564,28 +563,27 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI } /** - * Returns contents of a Jalview format features file, for visible features, as - * filtered by type and group. Features with a null group are displayed if their - * feature type is visible. Non-positional features may optionally be included - * (with no check on type or group). + * Returns contents of a Jalview format features file, for visible features, + * as filtered by type and group. Features with a null group are displayed if + * their feature type is visible. Non-positional features may optionally be + * included (with no check on type or group). * * @param sequences - * source of features - * @param visible - * map of colour for each visible feature type - * @param featureFilters - * @param visibleFeatureGroups + * @param fr * @param includeNonPositional * if true, include non-positional features (regardless of group or * type) * @return */ public String printJalviewFormat(SequenceI[] sequences, - Map visible, - Map featureFilters, - List visibleFeatureGroups, boolean includeNonPositional) + FeatureRenderer fr, boolean includeNonPositional) { - if (!includeNonPositional && (visible == null || visible.isEmpty())) + Map visibleColours = fr + .getDisplayedFeatureCols(); + Map featureFilters = fr.getFeatureFilters(); + + if (!includeNonPositional + && (visibleColours == null || visibleColours.isEmpty())) { // no point continuing. return "No Features Visible"; @@ -596,9 +594,10 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI */ // TODO: decide if feature links should also be written here ? StringBuilder out = new StringBuilder(256); - if (visible != null) + if (visibleColours != null) { - for (Entry featureColour : visible.entrySet()) + for (Entry featureColour : visibleColours + .entrySet()) { FeatureColourI colour = featureColour.getValue(); out.append(colour.toJalviewFormat(featureColour.getKey())).append( @@ -606,50 +605,22 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI } } - String[] types = visible == null ? new String[0] : visible.keySet() - .toArray(new String[visible.keySet().size()]); + String[] types = visibleColours == null ? new String[0] + : visibleColours.keySet() + .toArray(new String[visibleColours.keySet().size()]); /* * feature filters if any */ - outputFeatureFilters(out, visible, featureFilters); + outputFeatureFilters(out, visibleColours, featureFilters); /* - * sort groups alphabetically, and ensure that features with a - * null or empty group are output after those in named groups + * output features within groups */ - List sortedGroups = new ArrayList<>(visibleFeatureGroups); - sortedGroups.remove(null); - sortedGroups.remove(""); - Collections.sort(sortedGroups); - sortedGroups.add(null); - sortedGroups.add(""); + int count = outputFeaturesByGroup(out, fr, types, sequences, + includeNonPositional); - boolean foundSome = false; - - /* - * first output any non-positional features - */ - if (includeNonPositional) - { - for (int i = 0; i < sequences.length; i++) - { - String sequenceName = sequences[i].getName(); - for (SequenceFeature feature : sequences[i].getFeatures() - .getNonPositionalFeatures()) - { - foundSome = true; - out.append(formatJalviewFeature(sequenceName, feature)); - } - } - } - - /* - * positional features within groups - */ - foundSome |= outputFeaturesByGroup(out, sortedGroups, types, sequences); - - return foundSome ? out.toString() : "No Features Visible"; + return count > 0 ? out.toString() : "No Features Visible"; } /** @@ -687,65 +658,104 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI } if (!first) { - out.append(ENDFILTERS).append(newline).append(newline); + out.append(ENDFILTERS).append(newline); } } /** - * Appends output of sequence features within feature groups to the output - * buffer. Groups other than the null or empty group are sandwiched by - * STARTGROUP and ENDGROUP lines. + * Appends output of visible sequence features within feature groups to the + * output buffer. Groups other than the null or empty group are sandwiched by + * STARTGROUP and ENDGROUP lines. Answers the number of features written. * * @param out - * @param groups + * @param fr * @param featureTypes * @param sequences + * @param includeNonPositional * @return */ - private boolean outputFeaturesByGroup(StringBuilder out, - List groups, String[] featureTypes, SequenceI[] sequences) + private int outputFeaturesByGroup(StringBuilder out, + FeatureRenderer fr, String[] featureTypes, + SequenceI[] sequences, boolean includeNonPositional) { - boolean foundSome = false; - for (String group : groups) + List featureGroups = fr.getFeatureGroups(); + + /* + * sort groups alphabetically, and ensure that features with a + * null or empty group are output after those in named groups + */ + List sortedGroups = new ArrayList<>(featureGroups); + sortedGroups.remove(null); + sortedGroups.remove(""); + Collections.sort(sortedGroups); + sortedGroups.add(null); + sortedGroups.add(""); + + int count = 0; + List visibleGroups = fr.getDisplayedFeatureGroups(); + + /* + * loop over all groups (may be visible or not); + * non-positional features are output even if group is not visible + */ + for (String group : sortedGroups) { - boolean isNamedGroup = (group != null && !"".equals(group)); - if (isNamedGroup) - { - out.append(newline); - out.append(STARTGROUP).append(TAB); - out.append(group); - out.append(newline); - } + boolean firstInGroup = true; + boolean isNullGroup = group == null || "".equals(group); - /* - * output positional features within groups - */ for (int i = 0; i < sequences.length; i++) { String sequenceName = sequences[i].getName(); List features = new ArrayList<>(); - if (featureTypes.length > 0) + + /* + * get any non-positional features in this group, if wanted + * (for any feature type, whether visible or not) + */ + if (includeNonPositional) + { + features.addAll(sequences[i].getFeatures() + .getFeaturesForGroup(false, group)); + } + + /* + * add positional features for visible feature types, but + * (for named groups) only if feature group is visible + */ + if (featureTypes.length > 0 + && (isNullGroup || visibleGroups.contains(group))) { features.addAll(sequences[i].getFeatures().getFeaturesForGroup( true, group, featureTypes)); } - for (SequenceFeature sequenceFeature : features) + for (SequenceFeature sf : features) { - foundSome = true; - out.append(formatJalviewFeature(sequenceName, sequenceFeature)); + if (sf.isNonPositional() || fr.isVisible(sf)) + { + count++; + if (firstInGroup) + { + out.append(newline); + if (!isNullGroup) + { + out.append(STARTGROUP).append(TAB).append(group) + .append(newline); + } + } + firstInGroup = false; + out.append(formatJalviewFeature(sequenceName, sf)); + } } } - if (isNamedGroup) + if (!isNullGroup && !firstInGroup) { - out.append(ENDGROUP).append(TAB); - out.append(group); - out.append(newline); + out.append(ENDGROUP).append(TAB).append(group).append(newline); } } - return foundSome; + return count; } /** @@ -819,14 +829,15 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI AlignViewportI av = getViewport(); if (av != null) { - if (av.getAlignment() != null) + AlignmentI a = av.getAlignment(); + if (a != null) { - dataset = av.getAlignment().getDataset(); + dataset = a.getDataset(); } if (dataset == null) { // working in the applet context ? - dataset = av.getAlignment(); + dataset = a; } } else @@ -874,23 +885,24 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI * @return */ public String printGffFormat(SequenceI[] sequences, - Map visible, - List visibleFeatureGroups, - boolean includeNonPositionalFeatures) + FeatureRenderer fr, boolean includeNonPositionalFeatures) { + Map visibleColours = fr.getDisplayedFeatureCols(); + StringBuilder out = new StringBuilder(256); - out.append(String.format("%s %d\n", GFF_VERSION, gffVersion == 0 ? 2 : gffVersion)); + out.append(String.format("%s %d" + newline, GFF_VERSION, + gffVersion == 0 ? 2 : gffVersion)); if (!includeNonPositionalFeatures - && (visible == null || visible.isEmpty())) + && (visibleColours == null || visibleColours.isEmpty())) { return out.toString(); } - String[] types = visible == null ? new String[0] : visible.keySet() - .toArray( - new String[visible.keySet().size()]); + String[] types = visibleColours == null ? new String[0] + : visibleColours.keySet() + .toArray(new String[visibleColours.keySet().size()]); for (SequenceI seq : sequences) { @@ -899,21 +911,23 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI { features.addAll(seq.getFeatures().getNonPositionalFeatures()); } - if (visible != null && !visible.isEmpty()) + if (visibleColours != null && !visibleColours.isEmpty()) { features.addAll(seq.getFeatures().getPositionalFeatures(types)); } for (SequenceFeature sf : features) { - String source = sf.featureGroup; - if (!sf.isNonPositional() && source != null - && !visibleFeatureGroups.contains(source)) + if (!sf.isNonPositional() && !fr.isVisible(sf)) { - // group is not visible + /* + * feature hidden by group visibility, colour threshold, + * or feature filter condition + */ continue; } + String source = sf.featureGroup; if (source == null) { source = sf.getDescription();