JAL-629 Run non-alignframe linked groovy scripts. Use multiple specified groovy...
[jalview.git] / src / jalview / io / FeaturesFile.java
index 7e62f6b..a704f24 100755 (executable)
@@ -110,11 +110,11 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * Constructor which does not parse the file immediately
    * 
    * @param file
+   *          File or String filename
    * @param paste
    * @throws IOException
    */
-  public FeaturesFile(String file, DataSourceType paste)
-          throws IOException
+  public FeaturesFile(Object file, DataSourceType paste) throws IOException
   {
     super(false, file, paste);
   }
@@ -136,7 +136,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);
@@ -235,7 +235,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
         // skip comments/process pragmas
         if (line.length() == 0 || line.startsWith("#"))
         {
-          if (line.toLowerCase().startsWith("##"))
+          if (line.toLowerCase(Locale.ROOT).startsWith("##"))
           {
             processGffPragma(line, gffProps, align, newseqs);
           }
@@ -312,7 +312,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       // should report somewhere useful for UI if necessary
       warningMessage = ((warningMessage == null) ? "" : warningMessage)
               + "Parsing error at\n" + line;
-      System.out.println("Error parsing feature file: " + ex + "\n" + line);
+      jalview.bin.Console.outPrintln("Error parsing feature file: " + ex + "\n" + line);
       ex.printStackTrace(System.err);
       resetMatcher();
       return false;
@@ -347,7 +347,6 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     String line;
     while ((line = nextLine()) != null)
     {
-      // TODO: use .trim().equalsIgnoreCase here instead ? 
       if (line.toUpperCase(Locale.ROOT).startsWith(ENDFILTERS))
       {
         return;
@@ -355,7 +354,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       String[] tokens = line.split(TAB_REGEX);
       if (tokens.length != 2)
       {
-        System.err.println(String.format("Invalid token count %d for %d",
+        jalview.bin.Console.errPrintln(String.format("Invalid token count %d for %d",
                 tokens.length, line));
       }
       else
@@ -393,7 +392,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
      */
     if (gffColumns.length < 6)
     {
-      System.err.println("Ignoring feature line '" + line
+      jalview.bin.Console.errPrintln("Ignoring feature line '" + line
               + "' with too few columns (" + gffColumns.length + ")");
       return false;
     }
@@ -416,13 +415,13 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
         seq = alignment.getSequenceAt(idx);
       } catch (NumberFormatException ex)
       {
-        System.err.println("Invalid sequence index: " + seqIndex);
+        jalview.bin.Console.errPrintln("Invalid sequence index: " + seqIndex);
       }
     }
 
     if (seq == null)
     {
-      System.out.println("Sequence not found: " + line);
+      jalview.bin.Console.outPrintln("Sequence not found: " + line);
       return false;
     }
 
@@ -568,21 +567,19 @@ 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
    * @param fr
    * @param includeNonPositional
-   *                               if true, include non-positional features
-   *                               (regardless of group or type)
+   *          if true, include non-positional features (regardless of group or
+   *          type)
    * @param includeComplement
-   *                               if true, include visible complementary
-   *                               (CDS/protein) positional features, with
-   *                               locations converted to local sequence
-   *                               coordinates
+   *          if true, include visible complementary (CDS/protein) positional
+   *          features, with locations converted to local sequence coordinates
    * @return
    */
   public String printJalviewFormat(SequenceI[] sequences,
@@ -604,8 +601,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
               .entrySet())
       {
         FeatureColourI colour = featureColour.getValue();
-        out.append(colour.toJalviewFormat(featureColour.getKey())).append(
-                newline);
+        out.append(colour.toJalviewFormat(featureColour.getKey()))
+                .append(newline);
       }
     }
 
@@ -634,8 +631,9 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
 
   /**
    * Outputs any visible complementary (CDS/peptide) positional features as
-   * Jalview format, within feature group. The coordinates of the linked features
-   * are converted to the corresponding positions of the local sequences.
+   * Jalview format, within feature group. The coordinates of the linked
+   * features are converted to the corresponding positions of the local
+   * sequences.
    * 
    * @param out
    * @param fr
@@ -686,7 +684,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     /*
      * output features by group
      */
-    for (Entry<String, Map<String, List<SequenceFeature>>> groupFeatures : map.entrySet())
+    for (Entry<String, Map<String, List<SequenceFeature>>> groupFeatures : map
+            .entrySet())
     {
       out.append(newline);
       String group = groupFeatures.getKey();
@@ -766,8 +765,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
   }
 
   /**
-   * Outputs any feature filters defined for visible feature types, sandwiched by
-   * STARTFILTERS and ENDFILTERS lines
+   * Outputs any feature filters defined for visible feature types, sandwiched
+   * by STARTFILTERS and ENDFILTERS lines
    * 
    * @param out
    * @param visible
@@ -817,9 +816,9 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @param includeNonPositional
    * @return
    */
-  private int outputFeaturesByGroup(StringBuilder out,
-          FeatureRenderer fr, String[] featureTypes,
-          SequenceI[] sequences, boolean includeNonPositional)
+  private int outputFeaturesByGroup(StringBuilder out, FeatureRenderer fr,
+          String[] featureTypes, SequenceI[] sequences,
+          boolean includeNonPositional)
   {
     List<String> featureGroups = fr.getFeatureGroups();
 
@@ -868,8 +867,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
         if (featureTypes.length > 0
                 && (isNullGroup || visibleGroups.contains(group)))
         {
-          features.addAll(sequences[i].getFeatures().getFeaturesForGroup(
-                  true, group, featureTypes));
+          features.addAll(sequences[i].getFeatures()
+                  .getFeaturesForGroup(true, group, featureTypes));
         }
 
         for (SequenceFeature sf : features)
@@ -907,9 +906,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * @param sequenceName
    * @param sequenceFeature
    */
-  protected void formatJalviewFeature(
-          StringBuilder out, String sequenceName,
-          SequenceFeature sequenceFeature)
+  protected void formatJalviewFeature(StringBuilder out,
+          String sequenceName, SequenceFeature sequenceFeature)
   {
     if (sequenceFeature.description == null
             || sequenceFeature.description.equals(""))
@@ -1011,7 +1009,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
   @Override
   public String print(SequenceI[] sqs, boolean jvsuffix)
   {
-    System.out.println("Use printGffFormat() or printJalviewFormat()");
+    jalview.bin.Console.outPrintln("Use printGffFormat() or printJalviewFormat()");
     return null;
   }
 
@@ -1019,19 +1017,16 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * Returns features output in GFF2 format
    * 
    * @param sequences
-   *                                       the sequences whose features are to be
-   *                                       output
+   *          the sequences whose features are to be output
    * @param visible
-   *                                       a map whose keys are the type names of
-   *                                       visible features
+   *          a map whose keys are the type names of visible features
    * @param visibleFeatureGroups
    * @param includeNonPositionalFeatures
    * @param includeComplement
    * @return
    */
-  public String printGffFormat(SequenceI[] sequences,
-          FeatureRenderer fr, boolean includeNonPositionalFeatures,
-          boolean includeComplement)
+  public String printGffFormat(SequenceI[] sequences, FeatureRenderer fr,
+          boolean includeNonPositionalFeatures, boolean includeComplement)
   {
     FeatureRenderer fr2 = null;
     if (includeComplement)
@@ -1040,11 +1035,13 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       fr2 = Desktop.getAlignFrameFor(comp).getFeatureRenderer();
     }
 
-    Map<String, FeatureColourI> visibleColours = fr.getDisplayedFeatureCols();
+    Map<String, FeatureColourI> 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\n", GFF_VERSION,
+            gffVersion == 0 ? 2 : gffVersion));
 
     String[] types = visibleColours == null ? new String[0]
             : visibleColours.keySet()
@@ -1139,17 +1136,17 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * AC_Male=0;AF_NFE=0.00000e 00;Hom_FIN=0;GQ_MEDIAN=9
    * </pre>
    * 
-   * A map-valued attribute is formatted as a comma-delimited list within braces,
-   * for example
+   * A map-valued attribute is formatted as a comma-delimited list within
+   * braces, for example
    * 
    * <pre>
    * jvmap_CSQ={ALLELE_NUM=1,UNIPARC=UPI0002841053,Feature=ENST00000585561}
    * </pre>
    * 
-   * The {@code jvmap_} prefix designates a values map and is removed if the value
-   * is parsed when read in. (The GFF3 specification allows 'semi-structured data'
-   * to be represented provided the attribute name begins with a lower case
-   * letter.)
+   * The {@code jvmap_} prefix designates a values map and is removed if the
+   * value is parsed when read in. (The GFF3 specification allows
+   * 'semi-structured data' to be represented provided the attribute name begins
+   * with a lower case letter.)
    * 
    * @param sb
    * @param map
@@ -1236,11 +1233,11 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
    * format)
    * 
    * @param alignedRegions
-   *                         a list of "Align fromStart toStart fromCount"
+   *          a list of "Align fromStart toStart fromCount"
    * @param mapIsFromCdna
-   *                         if true, 'from' is dna, else 'from' is protein
+   *          if true, 'from' is dna, else 'from' is protein
    * @param strand
-   *                         either 1 (forward) or -1 (reverse)
+   *          either 1 (forward) or -1 (reverse)
    * @return
    * @throws IOException
    */
@@ -1336,7 +1333,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
      */
     if (gffColumns.length < 5)
     {
-      System.err.println("Ignoring GFF feature line with too few columns ("
+      jalview.bin.Console.errPrintln("Ignoring GFF feature line with too few columns ("
               + gffColumns.length + ")");
       return null;
     }
@@ -1367,7 +1364,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
         }
       } catch (IOException e)
       {
-        System.err.println("GFF parsing failed with: " + e.getMessage());
+        jalview.bin.Console.errPrintln("GFF parsing failed with: " + e.getMessage());
         return null;
       }
     }
@@ -1393,7 +1390,9 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     } catch (IOException q)
     {
     }
-    FastaFile parser = new FastaFile(this);
+    // Opening a FastaFile object with the remainder of this object's dataIn.
+    // Tell the constructor to NOT close the dataIn when finished.
+    FastaFile parser = new FastaFile(this, false);
     List<SequenceI> includedseqs = parser.getSeqs();
 
     SequenceIdMatcher smatcher = new SequenceIdMatcher(newseqs);
@@ -1456,8 +1455,8 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
       List<SequenceFeature> sfs = seq.getFeatures().getPositionalFeatures();
       if (!sfs.isEmpty())
       {
-        String newName = (String) sfs.get(0).getValue(
-                GffHelperI.RENAME_TOKEN);
+        String newName = (String) sfs.get(0)
+                .getValue(GffHelperI.RENAME_TOKEN);
         if (newName != null)
         {
           seq.setName(newName);
@@ -1533,7 +1532,7 @@ public class FeaturesFile extends AlignFile implements FeaturesSourceI
     }
     else
     {
-      System.err.println("Ignoring unknown pragma: " + line);
+      jalview.bin.Console.errPrintln("Ignoring unknown pragma: " + line);
     }
   }
 }