JAL-2434 omit unmapped positions from mapping
[jalview.git] / src / jalview / ws / phyre2 / Phyre2Client.java
index e980a19..80dc841 100644 (file)
@@ -3,6 +3,7 @@ package jalview.ws.phyre2;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceI;
 import jalview.fts.core.DecimalFormatTableCellRenderer;
+import jalview.io.AppletFormatAdapter;
 import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
 import jalview.io.FormatAdapter;
@@ -15,7 +16,6 @@ import jalview.util.Comparison;
 import jalview.util.Format;
 
 import java.io.BufferedReader;
-import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintStream;
@@ -30,15 +30,20 @@ public class Phyre2Client extends StructureMappingClient
 {
   private final static String NEWLINE = System.lineSeparator();
 
-  public static final int UNASSIGNED = -1;
-
-  private final static String PATH_SEPARATOR = File.separator;
+  private String fastaMappingFile;
 
   public Phyre2Client(StructureFile structureFile)
   {
     this.structureFile = structureFile;
   }
 
+  public StructureMapping getStructureMapping(SequenceI seq,
+          String pdbFile, String fMappingFile, String chain)
+  {
+    this.fastaMappingFile = fMappingFile;
+    return getStructureMapping(seq, pdbFile, chain);
+  }
+
   @Override
   public StructureMapping getStructureMapping(SequenceI seq,
           String pdbFile, String chain)
@@ -73,8 +78,11 @@ public class Phyre2Client extends StructureMappingClient
     AlignmentI seq2Phyre2ModelFastaMapping = null;
     try
     {
-      seq2Phyre2ModelFastaMapping = new FormatAdapter().readFile(
-              getFastaMappingFile(), DataSourceType.FILE, FileFormat.Fasta);
+      String fastaFile = getFastaMappingFile();
+      DataSourceType protocol = AppletFormatAdapter
+              .checkProtocol(fastaFile);
+      seq2Phyre2ModelFastaMapping = new FormatAdapter().readFile(fastaFile,
+              protocol, FileFormat.Fasta);
     } catch (IOException e1)
     {
       e1.printStackTrace();
@@ -92,24 +100,25 @@ public class Phyre2Client extends StructureMappingClient
     tStructureRes.setEnd(structureFile.getSeqsAsArray()[0].getEnd());
     try
     {
-      int sequenceResLenght = tSequenceRes.getLength();
-      int structureResLenght = tStructureRes.getLength();
-      if (sequenceResLenght == structureResLenght)
+      int sequenceResLength = tSequenceRes.getLength();
+      int structureResLength = tStructureRes.getLength();
+      if (sequenceResLength == structureResLength)
       {
         int prevStructResNum = -1;
-        int alignmentLenght = sequenceResLenght + tSequenceRes.getStart();
-        for (int x = 0; x < alignmentLenght; x++)
+        int alignmentLength = sequenceResLength + tSequenceRes.getStart();
+        for (int x = 0; x < alignmentLength; x++)
         {
           int alignSeqResidueIndex = tSequenceRes.findIndex(x);
           int structResNum = tStructureRes
                   .findPosition(alignSeqResidueIndex);
           int sequenceResNum = tSequenceRes
                   .findPosition(alignSeqResidueIndex - 1);
-          boolean sameResNum = (structResNum == prevStructResNum);
-          // System.out.println(sequenceResNum + " : "
-          // + (sameResNum ? -1 : prevStructResNum));
-          mapping.put(sequenceResNum, new int[] {
-              sameResNum ? -1 : prevStructResNum, -1 });
+          if (structResNum != prevStructResNum)
+          {
+            // System.out.println(sequenceResNum + " : " + prevStructResNum);
+            mapping.put(sequenceResNum, new int[] { prevStructResNum,
+                StructureMapping.UNASSIGNED });
+          }
           prevStructResNum = structResNum;
         }
       }
@@ -118,6 +127,10 @@ public class Phyre2Client extends StructureMappingClient
       e.printStackTrace();
     }
 
+    /*
+     * now populate atom positions for structure residues (and remove
+     * residue if atom position cannot be found)
+     */
     try
     {
       populateAtomPositions(" ", mapping);
@@ -157,11 +170,12 @@ public class Phyre2Client extends StructureMappingClient
 
   private String getFastaMappingFile()
   {
-    File phyre2ModelFile = new File(structureFile.getDataName());
-    String phyre2ModelResultDir = phyre2ModelFile.getParent();
-    String modelId = structureFile.getId().substring(0,
-            structureFile.getId().lastIndexOf(".pdb"));
-    return phyre2ModelResultDir + PATH_SEPARATOR + modelId + ".fasta";
+    return fastaMappingFile;
+  }
+
+  void setFastaMappingFile(String fastaMappingFile)
+  {
+    this.fastaMappingFile = fastaMappingFile;
   }
 
   @Override
@@ -279,10 +293,6 @@ public class Phyre2Client extends StructureMappingClient
       output.append(NEWLINE).append(NEWLINE);
     }
     float pid = (float) matchedSeqCount / seqRes.length() * 100;
-    // if (pid < SiftsSettings.getFailSafePIDThreshold())
-    // {
-    // throw new Exception(">>> Low PID detected for Phyre2 mapping...");
-    // }
     output.append("Length of alignment = " + seqRes.length()).append(
             NEWLINE);
     output.append(new Format("Percentage ID = %2.2f").form(pid));