2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.structure;
23 import java.util.ArrayList;
24 import java.util.BitSet;
25 import java.util.List;
27 import java.util.TreeMap;
30 * A class to model a set of models, chains and atom range positions
33 public class AtomSpecModel
36 * { modelId, {chainCode, List<from-to> ranges} }
38 private Map<String, Map<String, BitSet>> atomSpec;
43 public AtomSpecModel()
45 atomSpec = new TreeMap<>();
49 * Adds one contiguous range to this atom spec
56 public void addRange(String model, int startPos, int endPos, String chain)
59 * Get/initialize map of data for the colour and model
61 Map<String, BitSet> modelData = atomSpec.get(model);
62 if (modelData == null)
64 atomSpec.put(model, modelData = new TreeMap<>());
68 * Get/initialize map of data for colour, model and chain
70 BitSet chainData = modelData.get(chain);
71 if (chainData == null)
73 chainData = new BitSet();
74 modelData.put(chain, chainData);
78 * Add the start/end positions
80 chainData.set(startPos, endPos + 1);
83 public Iterable<String> getModels()
85 return atomSpec.keySet();
88 public int getModelCount()
90 return atomSpec.size();
93 public Iterable<String> getChains(String model)
95 return atomSpec.containsKey(model) ? atomSpec.get(model).keySet()
100 * Returns a (possibly empty) ordered list of contiguous atom ranges for the
101 * given model and chain.
107 public List<int[]> getRanges(String model, String chain)
109 List<int[]> ranges = new ArrayList<>();
110 if (atomSpec.containsKey(model))
112 BitSet bs = atomSpec.get(model).get(chain);
116 start = bs.nextSetBit(start);
120 end = bs.nextClearBit(start);
121 ranges.add(new int[] { start, end - 1 });
122 start = bs.nextSetBit(end);