JAL-2629 pad gaps when exporting alignment as 'database to search'
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 23 Mar 2018 12:18:32 +0000 (12:18 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 23 Mar 2018 12:18:32 +0000 (12:18 +0000)
src/jalview/hmmer/HMMSearch.java
src/jalview/hmmer/HmmerCommand.java

index caf0b37..30299e9 100644 (file)
@@ -19,7 +19,6 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Scanner;
@@ -193,20 +192,25 @@ public class HMMSearch extends HmmerCommand
     if (!dbFound || MessageManager.getString("label.this_alignment")
             .equals(dbPath))
     {
-      AlignmentI alignment = af.getViewport().getAlignment();
-      AlignmentI copy = new Alignment(alignment);
+      /*
+       * no external database specified for search, so
+       * export current alignment as 'database' to search
+       */
+      databaseFile = createTempFile("database", ".sto");
+      AlignmentI al = af.getViewport().getAlignment();
+      AlignmentI copy = new Alignment(al);
       SequenceI hmms = copy.getHmmConsensus();
       if (hmms != null)
       {
         copy.deleteSequence(hmms);
       }
-      StockholmFile stoFile = new StockholmFile(copy);
-      stoFile.setSeqs(copy.getSequencesArray());
-      String alignmentString = stoFile.print();
-      databaseFile = createTempFile("database", ".sto");
-      PrintWriter writer = new PrintWriter(databaseFile);
-      writer.print(alignmentString);
-      writer.close();
+      exportStockholm(copy.getSequencesArray(), databaseFile, null);
+      // StockholmFile stoFile = new StockholmFile(copy);
+      // stoFile.setSeqs(copy.getSequencesArray());
+      // String alignmentString = stoFile.print();
+      // PrintWriter writer = new PrintWriter(databaseFile);
+      // writer.print(alignmentString);
+      // writer.close();
     }
 
     args.add(hmmFile.getAbsolutePath());
index a25e5cd..e680cf2 100644 (file)
@@ -139,42 +139,47 @@ public abstract class HmmerCommand implements Runnable
    * @throws IOException
    */
   public void exportStockholm(SequenceI[] seqs, File toFile,
-          AnnotatedCollectionI annotated)
-          throws IOException
+          AnnotatedCollectionI annotated) throws IOException
   {
-    if (seqs != null)
+    if (seqs == null)
+    {
+      return;
+    }
+    AlignmentI newAl = new Alignment(seqs);
+    if (!newAl.isAligned())
+    {
+      newAl.padGaps();
+    }
+
+    if (toFile != null && annotated != null)
     {
-      AlignmentI newAl = new Alignment(seqs);
-      if (toFile != null && annotated != null)
+      for (AlignmentAnnotation annot : annotated.getAlignmentAnnotation())
       {
-        for (AlignmentAnnotation annot : annotated.getAlignmentAnnotation())
+        if (annot.label.contains("Reference") || "RF".equals(annot.label))
         {
-          if (annot.label.contains("Reference") || "RF".equals(annot.label))
+          AlignmentAnnotation newRF;
+          if (annot.annotations.length > newAl.getWidth())
           {
-            AlignmentAnnotation newRF;
-            if (annot.annotations.length > newAl.getWidth())
-            {
-              Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
-              System.arraycopy(annot.annotations, 0, rfAnnots, 0,
-                      rfAnnots.length);
-              newRF = new AlignmentAnnotation("RF", "Reference Positions",
-                      rfAnnots);
-            }
-            else
-            {
-              newRF = new AlignmentAnnotation(annot);
-            }
-            newAl.addAnnotation(newRF);
+            Annotation[] rfAnnots = new Annotation[newAl.getWidth()];
+            System.arraycopy(annot.annotations, 0, rfAnnots, 0,
+                    rfAnnots.length);
+            newRF = new AlignmentAnnotation("RF", "Reference Positions",
+                    rfAnnots);
+          }
+          else
+          {
+            newRF = new AlignmentAnnotation(annot);
           }
+          newAl.addAnnotation(newRF);
         }
       }
-
-      StockholmFile file = new StockholmFile(newAl);
-      String output = file.print(seqs, false);
-      PrintWriter writer = new PrintWriter(toFile);
-      writer.println(output);
-      writer.close();
     }
+
+    StockholmFile file = new StockholmFile(newAl);
+    String output = file.print(seqs, false);
+    PrintWriter writer = new PrintWriter(toFile);
+    writer.println(output);
+    writer.close();
   }
 
   /**