1 /* $RCSfile$
2  * $Author$
3  * $Date$
4  * $Revision$
5  *
6  * Copyright (C) 2011  The Jmol Development Team
7  *
8  * Contact: jmol-developers@lists.sf.net
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2.1 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  *  02110-1301, USA.
24  */
25 
26 package org.jmol.dssx;
27 
28 import java.util.Map;
29 
30 import org.jmol.modelset.Atom;
31 import org.jmol.util.Escape;
32 
33 /**
34  * A class for cataloging ladder bridges in a DSSP calculation
35  *
36  * @author hansonr
37  *
38  */
39 class Bridge {
40   Atom a, b;
41   int[][] ladder;
42   boolean isAntiparallel;
43 
Bridge(Atom a, Atom b, Map<int[][], Boolean> htLadders)44   Bridge(Atom a, Atom b, Map<int[][], Boolean> htLadders) {
45     this.a = a;
46     this.b = b;
47     ladder = new int[2][2];
48     ladder[0][0] = ladder[0][1] = Math.min(a.i, b.i);
49     ladder[1][0] = ladder[1][1] = Math.max(a.i, b.i);
50     addLadder(htLadders);
51   }
52 
addBridge(Bridge bridge, Map<int[][], Boolean> htLadders)53   boolean addBridge(Bridge bridge,  Map<int[][], Boolean> htLadders) {
54     if (bridge.isAntiparallel != isAntiparallel
55         || !canAdd(bridge) || !bridge.canAdd(this))
56       return false;
57     extendLadder(bridge.ladder[0][0], bridge.ladder[1][0]);
58     extendLadder(bridge.ladder[0][1], bridge.ladder[1][1]);
59     bridge.ladder = ladder;
60     if (bridge.ladder != ladder) {
61       htLadders.remove(bridge.ladder);
62       addLadder(htLadders);
63     }
64     return true;
65   }
66 
addLadder(Map<int[][], Boolean> htLadders)67   private void addLadder(Map<int[][], Boolean> htLadders) {
68     htLadders.put(ladder, (isAntiparallel ? Boolean.TRUE : Boolean.FALSE));
69   }
70 
canAdd(Bridge bridge)71   private boolean canAdd(Bridge bridge) {
72     int index1 = bridge.a.i;
73     int index2 = bridge.b.i;
74     // no crossing of ladder rungs (2WUJ)
75     return (isAntiparallel ?
76         (index1 >= ladder[0][1] && index2 <= ladder[1][0]
77         || index1 <= ladder[0][0] && index2 >= ladder[1][1])
78       : (index1 <= ladder[0][0] && index2 <= ladder[1][0]
79         || index1 >= ladder[0][1] && index2 >= ladder[1][1]));
80   }
81 
extendLadder(int index1, int index2)82   private void extendLadder(int index1, int index2) {
83     if (ladder[0][0] > index1)
84       ladder[0][0] = index1;
85     if (ladder[0][1] < index1)
86       ladder[0][1] = index1;
87     if (ladder[1][0] > index2)
88       ladder[1][0] = index2;
89     if (ladder[1][1] < index2)
90       ladder[1][1] = index2;
91   }
92 
93   @Override
toString()94   public String toString() {
95     return (isAntiparallel ? "a " : "p ") + a + " - " + b + "\t" + Escape.e(ladder);
96   }
97 
98 }