Skip to content

Commit 754635b

Browse files
authored
Merge pull request #15 from rcsb/dev-sb-empty-categories
Assert empty categories don't result in null values in bcif map
2 parents 7c91f5a + 7b675af commit 754635b

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ CIFTools Changelog
33

44
This project uses semantic versioning. Furthermore, this project provides code that was generated from schemata. Any schema change that introduces a breaking change in the generated code is considered as breaking for the whole project. Additional information is provided below when this occurs (named `Breaking schema changes`). Most of these occur in experimental categories and are unlikely to affect your code. `Breaking API changes` will be avoided starting with version 1.0.0.
55

6+
Unreleased
7+
-------------
8+
### Bug fixes
9+
* BCIF writing: fix behavior when the model contains empty categories
10+
611
ciftools-java 7.0.1 - March 2025
712
-------------
813
### Bug fixes

src/main/java/org/rcsb/cif/binary/BinaryCifWriter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private Map<String, Object> encodeFile(CifFile cifFile) {
5757

5858
// filter category names
5959
List<Category> filteredCategories = cifBlock.categories()
60+
.filter(Category::isDefined) // at least 1 row present
6061
.filter(category -> options.filterCategory(category.getCategoryName()))
6162
.collect(Collectors.toList());
6263
Object[] categories = new Object[filteredCategories.size()];
@@ -68,9 +69,6 @@ private Map<String, Object> encodeFile(CifFile cifFile) {
6869
for (Category category : filteredCategories) {
6970
String categoryName = category.getCategoryName();
7071
int rowCount = category.getRowCount();
71-
if (rowCount == 0) {
72-
continue;
73-
}
7472

7573
Map<String, Object> categoryMap = new LinkedHashMap<>();
7674
categoryMap.put("name", "_" + category.getCategoryName());

src/test/java/org/rcsb/cif/WriterTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import org.rcsb.cif.schema.core.CifCoreFile;
2222
import org.rcsb.cif.schema.mm.MmCifBlock;
2323
import org.rcsb.cif.schema.mm.MmCifFile;
24+
import org.rcsb.cif.schema.mm.PdbxStructModResidue;
2425

2526
import java.io.ByteArrayInputStream;
2627
import java.io.IOException;
2728
import java.io.InputStream;
2829
import java.util.Arrays;
30+
import java.util.List;
2931
import java.util.Locale;
3032
import java.util.Map;
3133
import java.util.regex.Pattern;
@@ -257,4 +259,17 @@ void whenReadingChecksum_thenNotTreatedAsInt() throws IOException {
257259
assertEquals("0197355516942160", cifFile.getFirstBlock().getMaTargetRefDbDetails().getSeqDbSequenceChecksum().get(0));
258260
assertNotNull(CifIO.writeBinary(cifFile));
259261
}
262+
263+
@Test
264+
void whenEmptyCategory_thenNotWritten() throws IOException {
265+
MmCifFile cifFile = CifIO.readFromInputStream(TestHelper.getInputStream("bcif/1acj.bcif.gz")).as(StandardSchemata.MMCIF);
266+
MmCifBlock block = cifFile.getFirstBlock();
267+
PdbxStructModResidue modResidue = block.getPdbxStructModResidue(); // this access adds an empty to the block
268+
assertFalse(modResidue.isDefined(), "category should be undefined");
269+
270+
byte[] text = CifIO.writeText(cifFile);
271+
assertFalse(CifIO.readFromInputStream(new ByteArrayInputStream(text)).as(StandardSchemata.MMCIF).getFirstBlock().getPdbxStructModResidue().isDefined());
272+
byte[] binary = CifIO.writeBinary(cifFile);
273+
assertFalse(CifIO.readFromInputStream(new ByteArrayInputStream(binary)).as(StandardSchemata.MMCIF).getFirstBlock().getPdbxStructModResidue().isDefined());
274+
}
260275
}

0 commit comments

Comments
 (0)