Skip to content

Commit 615a570

Browse files
committed
Writing out entity info in CIF
1 parent 88b51de commit 615a570

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public void test6ELW() throws IOException {
5959
// a structure with insertion codes
6060
testRoundTrip("6ELW");
6161
}
62+
63+
@Test
64+
public void test4HHB() throws IOException {
65+
// a structure with multiple poly entities
66+
testRoundTrip("4HHB");
67+
}
6268

6369
private static void testRoundTrip(String pdbId) throws IOException {
6470
URL url = new URL("https://files.rcsb.org/download/" + pdbId + ".cif");
@@ -121,6 +127,14 @@ private static void testRoundTrip(String pdbId) throws IOException {
121127
// Test cell and symmetry
122128
assertEquals(originalStruct.getCrystallographicInfo().getSpaceGroup(),
123129
readStruct.getCrystallographicInfo().getSpaceGroup());
130+
131+
// entity
132+
assertEquals(originalStruct.getEntityInfos().size(), readStruct.getEntityInfos().size());
133+
for (int i=0; i<originalStruct.getEntityInfos().size(); i++) {
134+
assertEquals(originalStruct.getEntityInfos().get(i).getMolId(), readStruct.getEntityInfos().get(i).getMolId());
135+
assertEquals(originalStruct.getEntityInfos().get(i).getType(), readStruct.getEntityInfos().get(i).getType());
136+
}
137+
124138
}
125139

126140
/**

biojava-structure/src/main/java/org/biojava/nbio/structure/io/cif/AbstractCifFileSupplier.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package org.biojava.nbio.structure.io.cif;
22

3-
import org.biojava.nbio.structure.Atom;
4-
import org.biojava.nbio.structure.Chain;
5-
import org.biojava.nbio.structure.Element;
6-
import org.biojava.nbio.structure.EntityType;
7-
import org.biojava.nbio.structure.Group;
8-
import org.biojava.nbio.structure.GroupType;
9-
import org.biojava.nbio.structure.Structure;
3+
import org.biojava.nbio.structure.*;
104
import org.biojava.nbio.structure.xtal.CrystalCell;
115
import org.biojava.nbio.structure.xtal.SpaceGroup;
126
import org.rcsb.cif.CifBuilder;
@@ -23,8 +17,10 @@
2317
import java.util.LinkedHashMap;
2418
import java.util.List;
2519
import java.util.Map;
20+
import java.util.Optional;
2621
import java.util.function.Consumer;
2722
import java.util.stream.Collector;
23+
import java.util.stream.Collectors;
2824

2925
/**
3026
* Convert a BioJava object to a CifFile.
@@ -41,9 +37,11 @@ protected CifFile getInternal(Structure structure, List<WrappedAtom> wrappedAtom
4137
SpaceGroup spaceGroup = structure.getPDBHeader().getCrystallographicInfo().getSpaceGroup();
4238
// atom_site
4339
Category atomSite = wrappedAtoms.stream().collect(toAtomSite());
40+
// entity information
41+
List<EntityInfo> entityInfos = structure.getEntityInfos();
4442

4543
MmCifBlockBuilder blockBuilder = CifBuilder.enterFile(StandardSchemata.MMCIF)
46-
.enterBlock(structure.getPDBCode());
44+
.enterBlock(structure.getPdbId() == null? "" : structure.getPdbId().getId());
4745

4846
blockBuilder.enterStructKeywords().enterText()
4947
.add(String.join(", ", structure.getPDBHeader().getKeywords()))
@@ -92,6 +90,49 @@ protected CifFile getInternal(Structure structure, List<WrappedAtom> wrappedAtom
9290
.leaveCategory();
9391
}
9492

93+
if (entityInfos != null) {
94+
95+
String[] entityIds = new String[entityInfos.size()];
96+
String[] entityTypes = new String[entityInfos.size()];
97+
String[] entityDescriptions = new String[entityInfos.size()];
98+
99+
for (int i=0; i<entityInfos.size(); i++) {
100+
EntityInfo e = entityInfos.get(i);
101+
entityIds[i] = Integer.toString(e.getMolId());
102+
entityTypes[i] = e.getType().getEntityType();
103+
entityDescriptions[i] = e.getDescription();
104+
}
105+
106+
String[] polyEntityIds = entityInfos.stream().filter(e -> e.getType() == EntityType.POLYMER).map(e -> Integer.toString(e.getMolId())).collect(Collectors.toList()).toArray(new String[]{});
107+
String[] entitySeqs = entityInfos.stream().filter(e -> e.getType() == EntityType.POLYMER).map(e -> e.getChains().get(0).getSeqResSequence()).collect(Collectors.toList()).toArray(new String[]{});
108+
109+
blockBuilder.enterEntity()
110+
.enterId()
111+
.add(entityIds)
112+
.leaveColumn()
113+
114+
.enterType()
115+
.add(entityTypes)
116+
.leaveColumn()
117+
118+
.enterPdbxDescription()
119+
.add(entityDescriptions)
120+
.leaveColumn()
121+
122+
.leaveCategory();
123+
124+
blockBuilder.enterEntityPoly()
125+
.enterEntityId()
126+
.add(polyEntityIds)
127+
.leaveColumn()
128+
129+
.enterPdbxSeqOneLetterCodeCan()
130+
.add(entitySeqs)
131+
.leaveColumn()
132+
133+
.leaveCategory();
134+
}
135+
95136
return blockBuilder.leaveBlock().leaveFile();
96137
}
97138

0 commit comments

Comments
 (0)