Repository navigation
Expand file tree
/
Copy pathGroupIterator.java
More file actions
211 lines (169 loc) · 5.08 KB
/
Copy pathGroupIterator.java
File metadata and controls
211 lines (169 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
* Created on 09.05.2004
* @author Andreas Prlic
*
*/
package org.biojava.nbio.structure;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* An iterator over all groups of a structure.
* @author Andreas Prlic
* @since 1.4
* @version %I% %G%
*/
public class GroupIterator implements Iterator<Group> {
private Structure structure ;
private int current_model_pos ;
private int current_chain_pos ;
private int current_group_pos ;
private boolean fixed_model ;
/**
* Constructs a GroupIterator object over all models
*
* @param struct a Structure object
*/
public GroupIterator (Structure struct) {
structure = struct ;
current_model_pos = 0 ;
current_chain_pos = 0 ;
current_group_pos = -1 ;
fixed_model = false ;
}
/**
* Constructs a GroupIterator object over a specific model
*
* @param struct a Structure object
*/
public GroupIterator (Structure struct, int modelNr) {
structure = struct ;
current_model_pos = modelNr;
current_chain_pos = 0 ;
current_group_pos = -1 ;
fixed_model = true ;
}
/** needed to do a copy of iterator ... */
private Structure getStructure() { return structure ;}
private int getModelPos() { return current_model_pos ;}
private void setModelPos(int pos){ current_model_pos = pos ;}
private int getChainPos() { return current_chain_pos ;}
private void setChainPos(int pos){ current_chain_pos = pos ;}
private int getGroupPos() { return current_group_pos ;}
private void setGroupPos(int pos){ current_group_pos = pos ;}
/** Creates and returns a copy of this object. */
@Override
public Object clone () {
GroupIterator gr = new GroupIterator(this.getStructure()) ;
gr.setModelPos(this.getModelPos());
gr.setChainPos(this.getChainPos());
gr.setGroupPos(this.getGroupPos());
gr.fixed_model = this.fixed_model;
return gr ;
}
/** is there a group after the current one in the structure? */
@Override
public boolean hasNext() {
return hasSubGroup(current_model_pos,current_chain_pos , current_group_pos +1) ;
}
/** recursive method to determine if there is a next group. Helper
* method for hasNext().
* @see #hasNext
*/
private boolean hasSubGroup(int tmp_model,int tmp_chain,int tmp_group) {
if (tmp_model >= structure.nrModels()) {
return false;
}
List<Chain> model = structure.getModel(tmp_model);
if (tmp_chain >= model.size()) {
if(fixed_model)
return false;
return hasSubGroup(tmp_model + 1, 0, 0);
}
Chain chain = model.get(tmp_chain);
// start search at beginning of next chain.
return tmp_group < chain.getAtomLength() || hasSubGroup(tmp_model, tmp_chain + 1, 0);
}
/** Get the model number of the current model.
*
* @return the number of the model
*/
public int getCurrentModel(){
return current_model_pos;
}
/** Get the current Chain. Returns null if we are at the end of the iteration.
*
* @return the Chain of the current position
*/
public Chain getCurrentChain(){
if ( current_model_pos >= structure.nrModels()){
return null;
}
List<Chain> model = structure.getModel(current_model_pos);
if ( current_chain_pos >= model.size() ){
return null;
}
return model.get(current_chain_pos);
}
/** get next Group.
* @return next Group
* @throws NoSuchElementException ...
*/
@Override
public Group next()
throws NoSuchElementException
{
return getNextGroup(current_model_pos,current_chain_pos,current_group_pos+1);
}
/** recursive method to retrieve the next group. Helper
* method for gext().
* @see #next
*/
private Group getNextGroup(int tmp_model,int tmp_chain,int tmp_group)
throws NoSuchElementException
{
if ( tmp_model >= structure.nrModels()){
throw new NoSuchElementException("arrived at end of structure!");
}
List<Chain> model = structure.getModel(tmp_model);
if ( tmp_chain >= model.size() ){
if(fixed_model)
throw new NoSuchElementException("arrived at end of model!");
return getNextGroup(tmp_model+1,0,0);
}
Chain chain = model.get(tmp_chain);
if (tmp_group >= chain.getAtomLength()){
// start search at beginning of next chain.
return getNextGroup(tmp_model,tmp_chain+1,0);
} else {
current_model_pos = tmp_model;
current_chain_pos = tmp_chain;
current_group_pos = tmp_group;
return chain.getAtomGroup(current_group_pos);
}
}
/** does nothing .
*/
@Override
public void remove() {
throw new UnsupportedOperationException("Cannot call remove() for GroupIterator");
}
}