resolved merge conflict
[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.ArrayList;
8 import java.util.Collection;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Map.Entry;
12
13 import MCview.Atom;
14 import MCview.PDBChain;
15
16 public abstract class StructureMappingClient
17 {
18   protected StructureFile structureFile;
19
20   private static final int PDB_RES_POS = StructureMapping.PDB_RES_NUM_INDEX; // 0
21
22   private static final int PDB_ATOM_POS = StructureMapping.PDB_ATOM_NUM_INDEX; // 1
23
24   /**
25    * Populates the atom positions mapped to by finding the atom number (if any)
26    * for each structure residue number in the map. If no atom is found (as is
27    * the case for residues missing in a PDB file), then deletes the residue from
28    * the map.
29    * 
30    * @param chainId
31    * @param mapping
32    * @throws IllegalArgumentException
33    * @throws StructureMappingException
34    */
35   public void populateAtomPositions(String chainId,
36           Map<Integer, int[]> mapping) throws IllegalArgumentException,
37           StructureMappingException
38   {
39     try
40     {
41       PDBChain chain = structureFile.findChain(chainId);
42
43       if (chain == null || mapping == null)
44       {
45         throw new IllegalArgumentException(
46                 "Chain id or mapping must not be null.");
47       }
48
49       List<Integer> notFound = new ArrayList<Integer>();
50       for (Entry<Integer, int[]> map : mapping.entrySet())
51       {
52         int structureResNo = map.getValue()[PDB_RES_POS];
53
54         /*
55          * find the atom number in the chain for this residue number
56          * NB only CA or P atoms have been saved in the chain - see
57          * JmolParser.convertSignificantAtoms
58          */
59         int atomIndex = getAtomIndex(structureResNo, chain.atoms);
60         if (atomIndex == StructureMapping.UNASSIGNED)
61         {
62           notFound.add(map.getKey());
63         }
64         else
65         {
66           map.getValue()[PDB_ATOM_POS] = atomIndex;
67         }
68       }
69       for (Integer missing : notFound)
70       {
71         mapping.remove(missing);
72       }
73     } catch (Exception e)
74     {
75       throw new StructureMappingException(
76               "Structure mapping exception occured.");
77     }
78   }
79
80   /**
81    * 
82    * @param residueIndex
83    *          The residue index used for the search
84    * @param atoms
85    *          A collection of Atom to search
86    * @return atom position for the given residue index
87    */
88   public int getAtomIndex(int residueIndex, Collection<Atom> atoms)
89   {
90     if (atoms == null)
91     {
92       throw new IllegalArgumentException(
93               "atoms collection must not be null!");
94     }
95     for (Atom atom : atoms)
96     {
97       if (atom.resNumber == residueIndex)
98       {
99         return atom.atomIndex;
100       }
101     }
102     return StructureMapping.UNASSIGNED;
103   }
104
105   public class StructureMappingException extends Exception
106   {
107
108     private static final long serialVersionUID = 1L;
109
110     public StructureMappingException(String message)
111     {
112       super(message);
113     }
114   }
115
116   /**
117    * 
118    * @param seq
119    *          sequence to generate mapping against the structure
120    * @param pdbFile
121    *          PDB file for the mapping
122    * @param chain
123    *          the chain of the entry to use for mapping
124    * @return StructureMapping
125    * @throws Exception
126    */
127   public abstract StructureMapping getStructureMapping(SequenceI seq,
128           String pdbFile, String chain) throws Exception,
129           StructureMappingException;
130
131   /**
132    * 
133    * @param mop
134    *          MappingOutputPojo
135    * @return Sequence<->Structure mapping as int[][]
136    * @throws StructureMappingException
137    */
138   public abstract StringBuffer getMappingOutput(MappingOutputModel mp)
139           throws StructureMappingException;
140
141 }