Repository navigation
Expand file tree
/
Copy pathAtomIterator.java
More file actions
186 lines (153 loc) · 3.89 KB
/
Copy pathAtomIterator.java
File metadata and controls
186 lines (153 loc) · 3.89 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
/*
* 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 24.05.2004
* @author Andreas Prlic
*
*/
package org.biojava.nbio.structure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Iterator;
import java.util.NoSuchElementException;
/** an iterator over all atoms of a structure / group.
* @author Andreas Prlic
* @since 1.4
* @version %I% %G%
*/
public class AtomIterator implements Iterator<Atom> {
private final static Logger logger = LoggerFactory.getLogger(AtomIterator.class);
private Group group ;
private int current_atom_pos ;
private GroupIterator groupiter ;
/**
* Constructs an AtomIterator object over all models
*
* @param struct a Structure object
*/
public AtomIterator(Structure struct) {
current_atom_pos = -1 ;
groupiter = new GroupIterator(struct) ;
if ( groupiter.hasNext() ) {
group = groupiter.next() ;
}
else
group = null ;
}
/**
* Constructs an AtomIterator object over a single model
*
* @param struct a Structure object
*/
public AtomIterator(Structure struct,int modelNr) {
current_atom_pos = -1 ;
groupiter = new GroupIterator(struct,modelNr) ;
if ( groupiter.hasNext() ) {
group = groupiter.next() ;
}
else
group = null ;
}
/** Get the chain that contains the current atom.
*
* @return a Chain object
*/
public Chain getCurrentChain(){
return groupiter.getCurrentChain();
}
/** Get the model number of the model containing the current atom.
*
* @return the number of the model
*/
public int getCurrentModel(){
return groupiter.getCurrentModel();
}
/**
* Constructs an AtomIterator object.
*
* @param g a Group object
*/
public AtomIterator(Group g) {
group = g ;
current_atom_pos = -1 ;
groupiter = null ;
}
/** Is there a next atom ?
* @return true if there is an atom after the current one
* */
@Override
public boolean hasNext() {
// trying to iterate over an empty structure...
if ( group == null)
return false;
// if there is another group ...
if ( current_atom_pos < group.size()-1 ) {
return true ;
} else {
// search through the next groups if they contain an atom
if (groupiter != null) {
GroupIterator tmp = (GroupIterator) groupiter.clone() ;
while (tmp.hasNext()) {
Group tmpg = tmp.next() ;
if ( tmpg.size() > 0 ) {
return true ;
}
}
} else {
// just an iterator over one group ...
return false ;
}
}
return false ;
}
/** Return next atom.
*
* @return the next Atom
* @throws NoSuchElementException if there is no atom after the current one
*/
@Override
public Atom next()
throws NoSuchElementException
{
current_atom_pos++ ;
if ( current_atom_pos >= group.size() ) {
if ( groupiter == null ) {
throw new NoSuchElementException("no more atoms found in group!");
}
if ( groupiter.hasNext() ) {
group = groupiter.next() ;
current_atom_pos = -1 ;
return next();
} else {
throw new NoSuchElementException("no more atoms found in structure!");
}
}
Atom a ;
a = group.getAtom(current_atom_pos);
if ( a == null) {
logger.error("current_atom_pos {} group {} size: {}", current_atom_pos, group, group.size());
throw new NoSuchElementException("error wile trying to retrieve atom");
}
return a ;
}
/** does nothing. */
@Override
public void remove() {
}
}