Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 86cb468

Browse files
committed
Parse/write Keywords from/to mmCIF files
Updated unit tests as well
1 parent 840ca74 commit 86cb468

3 files changed

Lines changed: 58 additions & 11 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URL;
2222
import java.util.Arrays;
2323

24+
import static org.junit.Assert.assertArrayEquals;
2425
import static org.junit.Assert.assertEquals;
2526
import static org.junit.Assert.assertNotNull;
2627

@@ -47,6 +48,18 @@ public void test1A2C() throws IOException {
4748
testRoundTrip("1A2C");
4849
}
4950

51+
@Test
52+
public void test3CDL() throws IOException {
53+
// a structure with insertion codes
54+
testRoundTrip("3CDL");
55+
}
56+
57+
@Test
58+
public void test6ELW() throws IOException {
59+
// a structure with insertion codes
60+
testRoundTrip("6ELW");
61+
}
62+
5063
private static void testRoundTrip(String pdbId) throws IOException {
5164
URL url = new URL("https://files.rcsb.org/download/" + pdbId + ".cif");
5265
Structure originalStruct = CifStructureConverter.fromURL(url);
@@ -57,6 +70,10 @@ private static void testRoundTrip(String pdbId) throws IOException {
5770
assertNotNull(readStruct);
5871
assertEquals(originalStruct.getChains().size(), readStruct.getChains().size());
5972
assertEquals(originalStruct.nrModels(), readStruct.nrModels());
73+
74+
assertArrayEquals("Keywords Are not preserved",
75+
originalStruct.getKeywords().toArray(),
76+
readStruct.getKeywords().toArray());
6077

6178
for (int i = 0; i < originalStruct.nrModels(); i++) {
6279
assertEquals(originalStruct.getModel(i).size(), readStruct.getModel(i).size());

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ protected CifFile getInternal(Structure structure, List<WrappedAtom> wrappedAtom
4545
MmCifBlockBuilder blockBuilder = CifBuilder.enterFile(StandardSchemata.MMCIF)
4646
.enterBlock(structure.getPDBCode());
4747

48+
blockBuilder.enterStructKeywords().enterText()
49+
.add(structure.getKeywords().toArray(new String[structure.getKeywords().size()]))
50+
.leaveColumn().leaveCategory();
51+
4852
if (atomSite.isDefined() && atomSite.getRowCount() > 0) {
4953
// set atom site
5054
blockBuilder.addCategory(atomSite);

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

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package org.biojava.nbio.structure.io.cif;
22

3+
import java.time.LocalDate;
4+
import java.time.ZoneId;
5+
import java.time.format.DateTimeFormatter;
6+
import java.time.format.DateTimeFormatterBuilder;
7+
import java.util.ArrayList;
8+
import java.util.Date;
9+
import java.util.HashMap;
10+
import java.util.Iterator;
11+
import java.util.LinkedHashMap;
12+
import java.util.List;
13+
import java.util.Locale;
14+
import java.util.Map;
15+
import java.util.NoSuchElementException;
16+
import java.util.Optional;
17+
import java.util.OptionalInt;
18+
import java.util.stream.Collectors;
19+
import java.util.stream.IntStream;
20+
21+
import javax.vecmath.Matrix4d;
22+
323
import org.biojava.nbio.structure.AminoAcid;
424
import org.biojava.nbio.structure.AminoAcidImpl;
525
import org.biojava.nbio.structure.Atom;
@@ -88,15 +108,6 @@
88108
import org.slf4j.Logger;
89109
import org.slf4j.LoggerFactory;
90110

91-
import javax.vecmath.Matrix4d;
92-
import java.time.LocalDate;
93-
import java.time.ZoneId;
94-
import java.time.format.DateTimeFormatter;
95-
import java.time.format.DateTimeFormatterBuilder;
96-
import java.util.*;
97-
import java.util.stream.Collectors;
98-
import java.util.stream.IntStream;
99-
100111
/**
101112
* An implementation of a CifFileConsumer for BioJava. Will process the information provided by a CifFile instance and
102113
* use it to build up a {@link Structure} object.
@@ -885,11 +896,26 @@ public void consumeStructConnType(StructConnType structConnType) {
885896

886897
@Override
887898
public void consumeStructKeywords(StructKeywords structKeywords) {
899+
//There may be a simpler implementation or a better implementation using
900+
// Java streams. However, I am not sure about the returned value from
901+
// getText().values()
902+
ArrayList<String> keywordsList = new ArrayList<String>();
903+
Iterator<String> iterator = structKeywords.getText().values().iterator();
904+
while (iterator.hasNext()) {
905+
String longString = (String) iterator.next();
906+
String[] strings = longString.split(" *, *");
907+
for (String string : strings) {
908+
keywordsList.add(string.trim());
909+
}
910+
}
911+
structure.setKeywords(keywordsList);
912+
888913
StrColumn pdbxKeywords = structKeywords.getPdbxKeywords();
889-
// TODO what is the correct format for these?
890914
if (pdbxKeywords.isDefined()) {
915+
//TODO I think no need for the Collectors.joining(", ") part.
891916
String keywords = pdbxKeywords.values().collect(Collectors.joining(", "));
892-
pdbHeader.setDescription(keywords);
917+
//TODO What is the right place to fill this field from?
918+
// pdbHeader.setDescription(keywords);
893919
pdbHeader.setClassification(keywords);
894920
}
895921
}

0 commit comments

Comments
 (0)