JAL-2375 JAL-2376 First commit of implementation for Phyre2 result browsing, template...
[jalview.git] / src / jalview / structure / StructureMappingClient.java
1 package jalview.structure;
2
3 import jalview.datamodel.SequenceI;
4 import jalview.io.StructureFile;
5 import jalview.structures.models.MappingOutputModel;
6
7 import java.util.Collection;
8 import java.util.Map;
9
10 import MCview.Atom;
11 import MCview.PDBChain;
12
13 public abstract class StructureMappingClient
14 {
15
16   protected StructureFile structureFile;
17
18   public static final int UNASSIGNED = -1;
19
20   private static final int PDB_RES_POS = 0;
21
22   private static final int PDB_ATOM_POS = 1;
23
24   public void populateAtomPositions(String chainId,
25           Map<Integer, int[]> mapping)
26  throws IllegalArgumentException,
27           StructureMappingException
28   {
29     try
30     {
31       PDBChain chain = structureFile.findChain(chainId);
32
33       if (chain == null || mapping == null)
34       {
35         throw new IllegalArgumentException(
36                 "Chain id or mapping must not be null.");
37       }
38       for (int[] map : mapping.values())
39       {
40         if (map[PDB_RES_POS] != UNASSIGNED)
41         {
42           map[PDB_ATOM_POS] = getAtomIndex(map[PDB_RES_POS], chain.atoms);
43         }
44       }
45     } catch (Exception e)
46     {
47       throw new StructureMappingException(
48               "Structure mapping exception occured.");
49     }
50   }
51
52   /**
53    * 
54    * @param residueIndex
55    *          The residue index used for the search
56    * @param atoms
57    *          A collection of Atom to search
58    * @return atom position for the given residue index
59    */
60   public int getAtomIndex(int residueIndex, Collection<Atom> atoms)
61   {
62     if (atoms == null)
63     {
64       throw new IllegalArgumentException(
65               "atoms collection must not be null!");
66     }
67     for (Atom atom : atoms)
68     {
69       if (atom.resNumber == residueIndex)
70       {
71         return atom.atomIndex;
72       }
73     }
74     return UNASSIGNED;
75   }
76
77   public class StructureMappingException extends Exception
78   {
79
80     private static final long serialVersionUID = 1L;
81
82     public StructureMappingException(String message)
83     {
84       super(message);
85     }
86   }
87
88   /**
89    * 
90    * @param seq
91    *          sequence to generate mapping against the structure
92    * @param pdbFile
93    *          PDB file for the mapping
94    * @param chain
95    *          the chain of the entry to use for mapping
96    * @return StructureMapping
97    * @throws Exception
98    */
99   public abstract StructureMapping getStructureMapping(SequenceI seq,
100           String pdbFile, String chain) throws Exception,
101           StructureMappingException;
102
103   /**
104    * 
105    * @param mop
106    *          MappingOutputPojo
107    * @return Sequence<->Structure mapping as int[][]
108    * @throws StructureMappingException
109    */
110   public abstract StringBuffer getMappingOutput(MappingOutputModel mp)
111           throws StructureMappingException;
112
113 }