Skip to content

Commit bf91324

Browse files
committed
Important bug fixes and docs in Structure/StructureImpl.
Fixed also pdb writing. Fixed some tests in integration tests, removed some warnings.
1 parent 38ed2ce commit bf91324

File tree

10 files changed

+123
-56
lines changed

10 files changed

+123
-56
lines changed

biojava-integrationtest/src/test/java/org/biojava/nbio/structure/test/PDBFileParserTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void test2LetterResidueName() throws IOException {
6767
String t =
6868
"HETATM 2242 NA NA L 541 5.845 -14.122 30.560 0.88 23.48 NA"+newline+
6969
"HETATM 2243 NA NA L 542 18.411 -16.475 38.464 0.88 24.77 NA"+newline+
70-
"TER"+newline;
70+
"TER "+newline;
7171
BufferedReader br = new BufferedReader(new StringReader(t));
7272
Structure s = parser.parsePDBFile(br);
7373
String pdb = s.toPDB();
@@ -98,7 +98,7 @@ public void testCorrectFloatingPointDisplay() throws IOException {
9898
"ATOM 12 O CYS L 1 9.110 15.220 21.912 1.00 19.03 O"+newline+
9999
"ATOM 13 CB CYS L 1 12.117 14.468 20.771 1.00 21.77 C"+newline+
100100
"ATOM 14 SG CYS L 1 12.247 14.885 22.538 1.00 20.55 S"+newline+
101-
"TER"+newline;
101+
"TER "+newline;
102102

103103
BufferedReader br = new BufferedReader(new StringReader(t));
104104

@@ -273,7 +273,7 @@ public void testSITE() throws IOException {
273273
Structure s = parser.parsePDBFile(inStream);
274274
// System.out.print(s.getSites());
275275
Chain chain = new ChainImpl();
276-
chain.setChainID("H");
276+
chain.setName("H");
277277
for (Site site : s.getSites()) {
278278
//System.out.println("Site: " + site.getSiteID());
279279
for (Group group : site.getGroups()) {
@@ -541,7 +541,7 @@ public void testCorrectAtomNamePadding() throws IOException {
541541
"HETATM 2283 C2'1 QWE H 373 16.825 -12.903 16.107 1.00 40.59 C"+newline+
542542
"HETATM 2284 C42 QWE H 373 18.146 -14.734 13.451 1.00 43.96 C"+newline+
543543
"HETATM 2285 N3 QWE H 373 18.049 -13.554 14.106 1.00 43.46 N"+newline+
544-
"TER"+newline;
544+
"TER "+newline;
545545

546546
BufferedReader br = new BufferedReader(new StringReader(atomLines));
547547

biojava-integrationtest/src/test/java/org/biojava/nbio/structure/test/io/Test3th3.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.biojava.nbio.structure.StructureException;
3232
import org.biojava.nbio.structure.StructureIO;
3333
import org.biojava.nbio.structure.align.util.AtomCache;
34-
import org.biojava.nbio.structure.io.FileParsingParameters;
3534
import org.junit.Test;
3635

3736
/**
@@ -56,7 +55,10 @@ public void test3th3() throws StructureException, IOException {
5655
// below we make sure that we parse both residues but that we can only lookup the
5756
// aminoacid residue (see ChainImpl.addChain)
5857

59-
Chain c = s.getChainByPDB("T");
58+
// since biojava 5.0 polymer and nonpolymer chains are separated, we've modified the
59+
// test accordingly below
60+
61+
Chain c = s.getPolyChainByPDB("T");
6062

6163
ResidueNumber rn = ResidueNumber.fromString("201");
6264
rn.setChainName("T");
@@ -74,7 +76,14 @@ public void test3th3() throws StructureException, IOException {
7476
if (gr.getResidueNumber().equals(rn)) count++;
7577
}
7678

77-
assertEquals(2, count);
79+
assertEquals(1, count);
80+
81+
c = s.getNonPolyChainByPDB("T");
82+
83+
g = c.getGroupByPDB(rn);
84+
85+
assertNotNull(g);
86+
assertEquals("BGC", g.getPDBName());
7887
}
7988

8089

biojava-structure/src/main/java/demo/DemoChangeChemCompProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,18 @@ private void printStructure(Structure struc) {
124124

125125
// loop chain
126126
for (Chain ch : struc.getModel(i)) {
127-
if (! ch.getChainID().equals("A") )
127+
if (! ch.getName().equals("A") )
128128
continue;
129-
System.out.println(pdbid + ">>>" + ch.getChainID() + ">>>"
129+
System.out.println(pdbid + ">>>" + ch.getName() + ">>>"
130130
+ ch.getAtomSequence());
131-
System.out.println(pdbid + ">>>" + ch.getChainID() + ">>>"
131+
System.out.println(pdbid + ">>>" + ch.getName() + ">>>"
132132
+ ch.getSeqResSequence());
133133
// Test the getAtomGroups() and getSeqResGroups() method
134134

135135
List<Group> group = ch.getSeqResGroups();
136136
int seqPos = 0;
137137
for (Group gp : group) {
138-
System.out.println(ch.getChainID() + ":"+seqPos + ":" + gp.getResidueNumber() + ":"
138+
System.out.println(ch.getName() + ":"+seqPos + ":" + gp.getResidueNumber() + ":"
139139
+ gp.getPDBName() + " " + gp.getType());
140140
seqPos++;
141141
}

biojava-structure/src/main/java/demo/DemoContacts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static void demoContacts(String pdbCode) throws IOException, StructureEx
4848

4949
Structure structure = StructureIO.getStructure(pdbCode);
5050

51-
Chain chain = structure.getChainByPDB("A");
51+
Chain chain = structure.getPolyChainByPDB("A");
5252

5353

5454
String[] atoms = {"CA"};

biojava-structure/src/main/java/demo/DemoLoadSecStruc.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void main(String[] args) throws IOException,
6363
System.out.println("Author's assignment: ");
6464
List<SecStrucInfo> ssi = SecStrucTools.getSecStrucInfo(s);
6565
for (SecStrucInfo ss : ssi) {
66-
System.out.println(ss.getGroup().getChain().getChainID() + " "
66+
System.out.println(ss.getGroup().getChain().getName() + " "
6767
+ ss.getGroup().getResidueNumber() + " "
6868
+ ss.getGroup().getPDBName() + " -> " + ss.toString());
6969
}
@@ -75,7 +75,7 @@ public static void main(String[] args) throws IOException,
7575
System.out.println("DSSP assignment: ");
7676
ssi = SecStrucTools.getSecStrucInfo(s);
7777
for (SecStrucInfo ss : ssi) {
78-
System.out.println(ss.getGroup().getChain().getChainID() + " "
78+
System.out.println(ss.getGroup().getChain().getName() + " "
7979
+ ss.getGroup().getResidueNumber() + " "
8080
+ ss.getGroup().getPDBName() + " -> " + ss.toString());
8181
}

biojava-structure/src/main/java/demo/DemoLoadStructure.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void loadStructureIO(){
6161

6262
System.out.println("First chain: " + chain1);
6363

64-
System.out.println("Chain " + chain1.getChainID() + " has the following sequence mismatches:");
64+
System.out.println("Chain " + chain1.getName() + " has the following sequence mismatches:");
6565
for (SeqMisMatch mm : chain1.getSeqMisMatches()){
6666
System.out.println(mm);
6767
}
@@ -101,7 +101,7 @@ public void basicLoad(){
101101

102102
System.out.println(structure);
103103

104-
Chain c = structure.getChainByPDB("C");
104+
Chain c = structure.getPolyChainByPDB("C");
105105

106106
System.out.print(c);
107107

biojava-structure/src/main/java/demo/DemoMMCIFReader.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static void main(String[] args){
4949

5050
}
5151

52-
/** A basic example how to load an mmCif file and get a Structure object
52+
/**
53+
* A basic example how to load an mmCif file and get a Structure object
5354
*
5455
*/
5556
public void loadSimple(){
@@ -71,7 +72,8 @@ public void loadSimple(){
7172
}
7273

7374

74-
/** An example demonstrating how to directly use the mmCif file parsing classes. This could potentially be used
75+
/**
76+
* An example demonstrating how to directly use the mmCif file parsing classes. This could potentially be used
7577
* to use the parser to populate a data-structure that is different from the biojava-structure data model.
7678
*
7779
*/
@@ -83,11 +85,11 @@ public void loadFromDirectAccess(){
8385
try {
8486
Structure s = pdbreader.getStructureById(pdbId);
8587

86-
Chain h = s.getChainByPDB("H");
88+
Chain h = s.getNonPolyChainByPDB("H");
8789

8890
List<Group> ligands = h.getAtomLigands();
8991

90-
System.out.println("These ligands have been found in chain " + h.getChainID());
92+
System.out.println("These ligands have been found in chain " + h.getName());
9193

9294
for (Group l:ligands){
9395
System.out.println(l);

biojava-structure/src/main/java/org/biojava/nbio/structure/Structure.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,18 +409,18 @@ public interface Structure extends Cloneable {
409409
* @param authId name of a chain that should be returned
410410
* @return Chain the requested chain
411411
* @throws StructureException
412-
* @Deprecated use {@link #getChainByPDB(String)} instead
412+
* @Deprecated use {@link #getPolyChainByPDB(String)} or {@link #getNonPolyChainByPDB(String)} instead
413413
*/
414414
@Deprecated
415415
Chain findChain(String authId) throws StructureException;
416416

417417
/**
418418
* Request a particular chain from a particular model
419-
* @param modelnr the number of the model to use
420419
* @param authId the name of a chain that should be returned
420+
* @param modelnr the number of the model to use
421421
* @return Chain the requested chain
422422
* @throws StructureException
423-
* @Deprecated use {@link #getChainByPDB(String, int)} instead
423+
* @Deprecated use {@link #getPolyChainByPDB(String, int)} or {@link #getNonPolyChainByPDB(String, int)} instead
424424
*/
425425
@Deprecated
426426
Chain findChain(String authId, int modelnr) throws StructureException;
@@ -578,16 +578,30 @@ public interface Structure extends Cloneable {
578578
Chain getNonPolyChain(String asymId, int modelIdx);
579579

580580
/**
581-
* Retrieve a non-polymeric chain based on the 'public' chain
582-
* name (authId) for the first model
581+
* Retrieve the first non-polymeric Chain corresponding to the given 'public' chain
582+
* name (authId) for the first model.
583+
* <p>
584+
* Note that other non-polymeric chains might still
585+
* exist with the same authId, due to the one-to-many relationship
586+
* between public chain ids (authIds) and the stored non-polymeric
587+
* chains that follow the mmCIF data model.
588+
* If the user is interested in non-polymeric chains different from first one, he/she would
589+
* need to go through {@link #getNonPolyChains()} to find them all.
583590
* @param authId the author id (chainName, public chain id)
584591
* @return a non-polymeric Chain or null if it can't be found
585592
*/
586593
Chain getNonPolyChainByPDB(String authId);
587594

588595
/**
589-
* Retrieve a non-polymeric Chain based on the 'public' chain
590-
* name (authId) for the given model index
596+
* Retrieve the first non-polymeric Chain corresponding to the 'public' chain
597+
* name (authId) and the given model index.
598+
* <p>
599+
* Note that other non-polymeric chains might still
600+
* exist with the same authId, due to the one-to-many relationship
601+
* between public chain ids (authIds) and the stored non-polymeric
602+
* chains that follow the mmCIF data model.
603+
* If the user is interested in non-polymeric chains different from first one, he/she would
604+
* need to go through {@link #getNonPolyChains(int)} to find them all.
591605
* @param authId the author id (chainName, public chain id)
592606
* @param modelIdx the index of the required model (0-based)
593607
* @return a non-polymeric Chain or null if it can't be found

biojava-structure/src/main/java/org/biojava/nbio/structure/StructureImpl.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public Structure clone() {
191191

192192
/** {@inheritDoc} */
193193
@Override
194-
public Group findGroup(String chainId, String pdbResnum, int modelnr)
194+
public Group findGroup(String chainName, String pdbResnum, int modelnr)
195195
throws StructureException {
196196

197197

@@ -201,11 +201,23 @@ public Group findGroup(String chainId, String pdbResnum, int modelnr)
201201
" in this structure. (contains "+models.size()+")");
202202

203203

204-
Chain c = findChain(chainId,modelnr);
204+
// first we need to gather all groups with the author id chainName: polymers, non-polymers and waters
205+
Chain polyChain = getPolyChainByPDB(chainName, modelnr);
206+
List<Group> groups = new ArrayList<>();
207+
groups.addAll(polyChain.getAtomGroups());
208+
209+
// there can be more thatn one non-poly chain for a given author id
210+
for (Chain chain: getNonPolyChains(modelnr)) {
211+
if (chain.getName().equals(chainName))
212+
groups.addAll(chain.getAtomGroups());
213+
}
214+
for (Chain chain: getWaterChains(modelnr)) {
215+
if (chain.getName().equals(chainName))
216+
groups.addAll(chain.getAtomGroups());
217+
}
205218

206-
List<Group> groups = c.getAtomGroups();
207219

208-
// now iterate over all groups in this chain.
220+
// now iterate over all groups
209221
// in order to find the amino acid that has this pdbRenum.
210222

211223
for (Group g : groups) {
@@ -219,7 +231,7 @@ public Group findGroup(String chainId, String pdbResnum, int modelnr)
219231
}
220232

221233
throw new StructureException("could not find group " + pdbResnum +
222-
" in chain " + chainId);
234+
" in chain " + chainName);
223235
}
224236

225237

@@ -236,17 +248,10 @@ public Group findGroup(String chainName, String pdbResnum) throws StructureExcep
236248

237249
/** {@inheritDoc} */
238250
@Override
239-
public Chain findChain(String asymId, int modelnr) throws StructureException {
240-
241-
List<Chain> chains = getChains(modelnr);
251+
public Chain findChain(String chainName, int modelnr) throws StructureException {
242252

243-
// iterate over all chains.
244-
for (Chain c : chains) {
245-
if (c.getName().equals(asymId)) {
246-
return c;
247-
}
248-
}
249-
throw new StructureException("Could not find chain by asymId \"" + asymId + "\" for PDB id " + pdb_id);
253+
return getChainByPDB(chainName, modelnr);
254+
250255
}
251256

252257

biojava-structure/src/main/java/org/biojava/nbio/structure/io/FileConvert.java

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ private String printPDBConnections(){
119119
for (Chain c:structure.getChains()) {
120120
for (Group g:c.getAtomGroups()) {
121121
for (Atom a:g.getAtoms()) {
122-
for (Bond b:a.getBonds()) { //7890123456789012345678901234567890123456789012345678901234567890
123-
str.append(String.format("CONECT%5d%5d "+newline, b.getAtomA().getPDBserial(), b.getAtomB().getPDBserial()));
122+
if (a.getBonds()!=null) {
123+
for (Bond b:a.getBonds()) { //7890123456789012345678901234567890123456789012345678901234567890
124+
str.append(String.format("CONECT%5d%5d "+newline, b.getAtomA().getPDBserial(), b.getAtomB().getPDBserial()));
125+
}
124126
}
125127
}
126128
}
@@ -186,34 +188,69 @@ public String toPDB() {
186188
str.append("EXPDTA NMR, "+ nrModels+" STRUCTURES"+newline) ;
187189
}
188190
for (int m = 0 ; m < nrModels ; m++) {
189-
List<Chain> model = structure.getModel(m);
190-
// todo support NMR structures ...
191+
192+
191193
if ( nrModels>1 ) {
192194
str.append("MODEL " + (m+1)+ newline);
193195
}
194-
// do for all chains
195-
int nrChains = model.size();
196-
for ( int c =0; c<nrChains;c++) {
197-
Chain chain = model.get(c);
198-
//String chainID = chain.getChainID();
199-
//if ( chainID.equals(DEFAULTCHAIN) ) chainID = " ";
196+
197+
List<Chain> polyChains = structure.getPolyChains(m);
198+
List<Chain> nonPolyChains = structure.getNonPolyChains(m);
199+
List<Chain> waterChains = structure.getWaterChains(m);
200+
201+
for (Chain chain : polyChains) {
202+
200203
// do for all groups
201204
int nrGroups = chain.getAtomLength();
202205
for ( int h=0; h<nrGroups;h++){
203206

204207
Group g= chain.getAtomGroup(h);
205208

209+
toPDB(g,str);
210+
211+
}
212+
// End any polymeric chain with a "TER" record
213+
if (nrGroups > 0) str.append(String.format("%-80s","TER")).append(newline);
214+
215+
}
216+
217+
boolean nonPolyGroupsExist = false;
218+
for (Chain chain : nonPolyChains) {
219+
220+
// do for all groups
221+
int nrGroups = chain.getAtomLength();
222+
for ( int h=0; h<nrGroups;h++){
223+
224+
Group g= chain.getAtomGroup(h);
206225

207226
toPDB(g,str);
227+
228+
nonPolyGroupsExist = true;
229+
}
230+
231+
}
232+
if (nonPolyGroupsExist) str.append(String.format("%-80s","TER")).append(newline);;
233+
234+
boolean waterGroupsExist = false;
235+
for (Chain chain : waterChains) {
236+
237+
// do for all groups
238+
int nrGroups = chain.getAtomLength();
239+
for ( int h=0; h<nrGroups;h++){
208240

241+
Group g= chain.getAtomGroup(h);
209242

243+
toPDB(g,str);
244+
245+
waterGroupsExist = true;
210246
}
211-
// End any chains with a "TER" record.
212-
if (nrGroups > 0) str.append("TER").append(newline);
247+
213248
}
249+
if (waterGroupsExist) str.append(String.format("%-80s","TER")).append(newline);;
250+
214251

215252
if ( nrModels>1) {
216-
str.append("ENDMDL").append(newline);
253+
str.append(String.format("%-80s","ENDMDL")).append(newline);
217254
}
218255

219256

@@ -416,7 +453,7 @@ record = "ATOM ";
416453
}
417454

418455
public static void toPDB(Atom a, StringBuffer str) {
419-
toPDB(a,str,a.getGroup().getChainId());
456+
toPDB(a,str,a.getGroup().getChain().getName());
420457
}
421458

422459

0 commit comments

Comments
 (0)