Skip to content

Commit b2b7e95

Browse files
committed
fixing biojava#76
1 parent 884707d commit b2b7e95

6 files changed

Lines changed: 109 additions & 47 deletions

File tree

biojava3-structure/src/main/java/org/biojava/bio/structure/Group.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,12 @@ GLY does not have CB (unless we would calculate some artificially
380380
* @return the alternate location group if found, or null otherwise
381381
*/
382382
public Group getAltLocGroup(Character altLoc);
383+
384+
385+
/** attempts to reduce the memory imprint of this group by trimming
386+
* all internal Collection objects to the required size.
387+
*
388+
*/
389+
public void trimToSize();
390+
383391
}

biojava3-structure/src/main/java/org/biojava/bio/structure/HetatomImpl.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -270,25 +270,6 @@ public Atom getAtom(String name)
270270

271271
}
272272

273-
// if here, we could not find the atom in this group.
274-
// however in some alternate locations, the CA atoms are displayed.
275-
// check the alternate locations...
276-
277-
if ( hasAltLoc()) {
278-
for ( Group alt : altLocs){
279-
try {
280-
a = alt.getAtom(name);
281-
// dirty hack
282-
// we are adding this group to the main one...
283-
addAtom(a);
284-
if ( a != null)
285-
return a;
286-
} catch (StructureException e){
287-
// does not have that atom, ignore.
288-
}
289-
}
290-
}
291-
292273
throw new StructureException(" No atom "+name + " in group " + pdb_name + " " + residueNumber + " !");
293274

294275
}
@@ -338,15 +319,6 @@ public boolean hasAtom(String fullName){
338319
a = atomSingleCharLookup.get(fullName.trim());
339320
if ( a != null)
340321
return true;
341-
342-
// check altLocs:
343-
344-
if ( hasAltLoc()){
345-
for (Group alt: altLocs){
346-
if ( alt.hasAtom(fullName))
347-
return true;
348-
}
349-
}
350322

351323
return false;
352324

@@ -647,4 +619,32 @@ public void addAltLoc(Group group) {
647619
public boolean isWater() {
648620
return WATERNAMES.contains(pdb_name);
649621
}
622+
623+
/** attempts to reduce the memory imprint of this group by trimming
624+
* all internal Collection objects to the required size.
625+
*
626+
*/
627+
@SuppressWarnings("rawtypes")
628+
public void trimToSize(){
629+
630+
if ( atoms instanceof ArrayList) {
631+
ArrayList myatoms = (ArrayList) atoms;
632+
myatoms.trimToSize();
633+
}
634+
if ( altLocs instanceof ArrayList){
635+
ArrayList myAltLocs = (ArrayList) altLocs;
636+
myAltLocs.trimToSize();
637+
}
638+
atomLookup = new HashMap<String,Atom>(atomLookup);
639+
atomSingleCharLookup = new HashMap<String,Atom>(atomLookup);
640+
641+
if ( hasAltLoc()) {
642+
for (Group alt : getAltLocs()){
643+
alt.trimToSize();
644+
}
645+
}
646+
647+
648+
}
649+
650650
}

biojava3-structure/src/main/java/org/biojava/bio/structure/StructureTools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class StructureTools {
6565

6666
public static final String oAtomName = "O";
6767

68-
public static final String cbAtomName = "CB";
68+
public static final String cbAtomName = " CB ";
6969

7070

7171
/** The names of the Atoms that form the backbone.

biojava3-structure/src/main/java/org/biojava/bio/structure/io/PDBFileParser.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,9 @@ private void pdb_SEQRES_Handler(String line)
860860
if ( test == null)
861861
seqResChains.add(current_chain);
862862

863+
if (current_group != null)
864+
current_group.trimToSize();
865+
863866
current_group = null;
864867
current_chain = null;
865868

@@ -1742,7 +1745,8 @@ private void pdb_ATOM_Handler(String line)
17421745
if ( ! residueNumber.equals(current_group.getResidueNumber())) {
17431746

17441747
current_chain.addGroup(current_group);
1745-
1748+
current_group.trimToSize();
1749+
17461750
current_group = getNewGroup(recordName,aminoCode1,groupCode3);
17471751

17481752
//current_group.setPDBCode(pdbCode);
@@ -1755,13 +1759,16 @@ private void pdb_ATOM_Handler(String line)
17551759
// same residueNumber, but altLocs...
17561760

17571761
// test altLoc
1758-
if ( ! altLoc.equals(' ')) {
1762+
if ( ! altLoc.equals(' ')) {
1763+
17591764
altGroup = getCorrectAltLocGroup( altLoc,recordName,aminoCode1,groupCode3);
17601765
if ( altGroup.getChain() == null) {
17611766
// need to set current chain
17621767
altGroup.setChain(current_chain);
17631768
}
17641769
//System.out.println("found altLoc! " + current_group + " " + altGroup);
1770+
1771+
17651772
}
17661773
}
17671774

@@ -1912,6 +1919,14 @@ private void pdb_ATOM_Handler(String line)
19121919
current_group.addAtom(atom);
19131920
}
19141921

1922+
1923+
// make sure that main group has all atoms
1924+
// GitHub issue: #76
1925+
if ( ! current_group.hasAtom(atom.getFullName())) {
1926+
current_group.addAtom(atom);
1927+
}
1928+
1929+
19151930

19161931
//System.out.println("current group: " + current_group);
19171932
}
@@ -2090,6 +2105,7 @@ private void pdb_MODEL_Handler(String line) {
20902105
if (current_chain != null) {
20912106
if (current_group != null) {
20922107
current_chain.addGroup(current_group);
2108+
current_group.trimToSize();
20932109
}
20942110
//System.out.println("starting new model "+(structure.nrModels()+1));
20952111

biojava3-structure/src/main/java/org/biojava/bio/structure/io/mmcif/SimpleMMcifConsumer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ public void newAtomSite(AtomSite atom) {
382382
// add previous data
383383
if ( current_chain != null ) {
384384
current_chain.addGroup(current_group);
385+
current_group.trimToSize();
385386
}
386387

387388
// we came to the beginning of a new NMR model
@@ -480,7 +481,7 @@ public void newAtomSite(AtomSite atom) {
480481
if ( ! residueNumber.equals(current_group.getResidueNumber())) {
481482
//System.out.println("end of residue: "+current_group.getPDBCode()+" "+residueNrInt);
482483
current_chain.addGroup(current_group);
483-
484+
current_group.trimToSize();
484485
current_group = getNewGroup(recordName,aminoCode1,seq_id,groupCode3);
485486
//current_group.setPDBCode(pdbCode);
486487
try {
@@ -538,6 +539,14 @@ public void newAtomSite(AtomSite atom) {
538539
current_group.addAtom(a);
539540
}
540541

542+
543+
// make sure that main group has all atoms
544+
// GitHub issue: #76
545+
if ( ! current_group.hasAtom(a.getFullName())) {
546+
current_group.addAtom(a);
547+
}
548+
549+
541550
//System.out.println(">" + atom.getLabel_atom_id()+"< " + a.getGroup().getPDBName() + " " + a.getGroup().getChemComp() );
542551

543552
//System.out.println(current_group);

biojava3-structure/src/test/java/org/biojava/bio/structure/TestAltLocs.java

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,34 @@ public void test1AAC(){
180180

181181
Group g = a.getGroupByPDB( ResidueNumber.fromString("27"));
182182

183+
System.out.println(g);
184+
for (Atom as: g.getAtoms()) {
185+
System.out.println(as);
186+
}
183187

184-
//TODO: complete this test
185-
// System.out.println(g);
186-
// for (Atom atom : g.getAtoms()) {
187-
// System.out.print(atom.toPDB());
188-
// }
189-
//
190-
//
191-
// int pos = 0;
192-
// for (Group alt: g.getAltLocs()) {
193-
// pos++;
194-
//// System.out.println("altLoc: " + pos + " " + alt);
195-
//// for (Atom atom : alt.getAtoms()) {
196-
//// System.out.print(atom.toPDB());
197-
//// }
198-
// }
188+
testCBAtomInMainGroup(g);
189+
190+
AtomCache cache = new AtomCache();
191+
cache.setUseMmCif(true);
199192

193+
Structure s1 = cache.getStructure("1AAC");
194+
Chain a1 = s1.getChainByPDB("A");
195+
196+
Group g1 = a1.getGroupByPDB( ResidueNumber.fromString("27"));
197+
198+
testCBAtomInMainGroup(g1);
199+
200+
201+
202+
// int pos = 0;
203+
// for (Group alt: g.getAltLocs()) {
204+
// pos++;
205+
// System.out.println("altLoc: " + pos + " " + alt);
206+
// for (Atom atom : alt.getAtoms()) {
207+
// System.out.print(atom.toPDB());
208+
// }
209+
// }
210+
200211
} catch (Exception e){
201212
e.printStackTrace();
202213
fail(e.getMessage());
@@ -205,4 +216,22 @@ public void test1AAC(){
205216

206217
}
207218

219+
private void testCBAtomInMainGroup(Group g) {
220+
// test position of C-beta
221+
222+
boolean cbInMain = false;
223+
224+
for (Atom atom : g.getAtoms()) {
225+
System.out.print(atom.toPDB());
226+
if ( atom.getFullName().equals(StructureTools.caAtomName)){
227+
228+
cbInMain = true;
229+
break;
230+
}
231+
}
232+
233+
assertTrue("Did not find C beta atom in main group",cbInMain);
234+
235+
}
236+
208237
}

0 commit comments

Comments
 (0)