JAL-3390 pull up of getShownResidues() to AAStructureBindingModel
[jalview.git] / src / jalview / ext / rbvi / chimera / AtomSpecModel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.rbvi.chimera;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28
29 /**
30  * A class to model a Chimera or Jmol residue set, as
31  * {@code Map<modelNumber, Map<chainId, List<residueRange>>>}. This can then be
32  * traversed to generate the required display command in Chimera or Jmol syntax.
33  */
34 public class AtomSpecModel
35 {
36   private Map<Integer, Map<String, List<int[]>>> atomSpec;
37
38   /**
39    * Constructor
40    */
41   public AtomSpecModel()
42   {
43     atomSpec = new TreeMap<>();
44   }
45
46   public Map<Integer, Map<String, List<int[]>>> getMap()
47   {
48     return atomSpec;
49   }
50
51   /**
52    * Adds one contiguous range to this atom spec
53    * 
54    * @param model
55    * @param startPos
56    * @param endPos
57    * @param chain
58    */
59   public void addRange(int model, int startPos, int endPos, String chain)
60   {
61     /*
62      * Get/initialize map of data for the colour and model
63      */
64     Map<String, List<int[]>> modelData = atomSpec.get(model);
65     if (modelData == null)
66     {
67       atomSpec.put(model, modelData = new TreeMap<>());
68     }
69
70     /*
71      * Get/initialize map of data for colour, model and chain
72      */
73     List<int[]> chainData = modelData.get(chain);
74     if (chainData == null)
75     {
76       chainData = new ArrayList<>();
77       modelData.put(chain, chainData);
78     }
79
80     /*
81      * Add the start/end positions
82      */
83     chainData.add(new int[] { startPos, endPos });
84     // TODO add intelligently, using a RangeList class
85   }
86
87   /**
88    * Answers an iterable set of the structure models in this model
89    * 
90    * @return
91    */
92   public Iterable<Integer> getModels()
93   {
94     return atomSpec.keySet();
95   }
96
97   /**
98    * Answers an iterable set of the chains in this model for the given structure
99    * model, or an empty set if none
100    * 
101    * @param model
102    * @return
103    */
104   public Iterable<String> getChains(Integer model)
105   {
106     if (atomSpec.containsKey(model))
107     {
108       return atomSpec.get(model).keySet();
109     }
110     return Collections.emptySet();
111   }
112
113   public List<int[]> getRanges(Integer model, String chain)
114   {
115     Map<String, List<int[]>> modelData = atomSpec.get(model);
116     if (modelData != null)
117     {
118       List<int[]> chainData = modelData.get(chain);
119       if (chainData != null)
120       {
121         return chainData;
122       }
123     }
124     return Collections.EMPTY_LIST;
125   }
126 }