Skip to content

Commit db1839e

Browse files
committed
Handle an empty Element column in a PDB file
Fix biojava#537
1 parent 231b41a commit db1839e

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,24 @@ public void testMissingElements() throws IOException {
579579
"ATOM 14 SG CYS L 1 12.247 14.885 22.538 1.00 20.55"+newline+
580580
"TER "+newline;
581581

582+
// A two residue structure with empty Element column
583+
String emptyElement =
584+
"ATOM 1 N ASP L 1A 11.095 19.341 20.188 1.00 30.14 "+newline+
585+
"ATOM 2 CA ASP L 1A 10.070 18.634 19.379 1.00 28.34 "+newline+
586+
"ATOM 3 C ASP L 1A 9.846 17.102 19.503 1.00 26.08 "+newline+
587+
"ATOM 4 O ASP L 1A 8.744 16.584 19.162 1.00 23.47 "+newline+
588+
"ATOM 5 CB ASP L 1A 10.255 18.858 17.853 1.00 37.55 "+newline+
589+
"ATOM 6 CG ASP L 1A 8.836 19.264 17.401 1.00 42.76 "+newline+
590+
"ATOM 7 OD1 ASP L 1A 8.058 19.292 18.400 1.00 44.03 "+newline+
591+
"ATOM 8 OD2 ASP L 1A 8.616 19.668 16.244 1.00 46.88 "+newline+
592+
"ATOM 9 N CYS L 1 10.835 16.440 20.113 1.00 23.72 "+newline+
593+
"ATOM 10 CA CYS L 1 10.769 14.970 20.210 1.00 20.89 "+newline+
594+
"ATOM 11 C CYS L 1 9.580 14.524 21.006 1.00 18.64 "+newline+
595+
"ATOM 12 O CYS L 1 9.110 15.220 21.912 1.00 19.03 "+newline+
596+
"ATOM 13 CB CYS L 1 12.117 14.468 20.771 1.00 21.77 "+newline+
597+
"ATOM 14 SG CYS L 1 12.247 14.885 22.538 1.00 20.55 "+newline+
598+
"TER "+newline;
599+
582600
String original =
583601
"ATOM 1 N ASP L 1A 11.095 19.341 20.188 1.00 30.14 N"+newline+
584602
"ATOM 2 CA ASP L 1A 10.070 18.634 19.379 1.00 28.34 C"+newline+
@@ -600,8 +618,13 @@ public void testMissingElements() throws IOException {
600618
BufferedReader br = new BufferedReader(new StringReader(missingElement));
601619
Structure s = parser.parsePDBFile(br);
602620
String pdb = s.toPDB();
621+
assertTrue("the Element column has not been filled correctly", pdb.equals(original));
603622

604-
assertTrue("the created PDB file does not match the input file", pdb.equals(original));
623+
624+
br = new BufferedReader(new StringReader(emptyElement));
625+
s = parser.parsePDBFile(br);
626+
pdb = s.toPDB();
627+
assertTrue("the Element column has not been filled correctly", pdb.equals(original));
605628

606629
}
607630
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,17 +1828,27 @@ private void pdb_ATOM_Handler(String line) {
18281828
// missing (i.e. misformatted PDB file), then parse the
18291829
// element from the chemical component.
18301830
Element element = Element.R;
1831+
boolean guessElement = true;
18311832
if ( line.length() > 77 ) {
18321833
// parse element from element field
1833-
String elementSymbol = line.substring (76, 78).trim();
1834-
try {
1835-
element = Element.valueOfIgnoreCase(elementSymbol);
1836-
} catch (IllegalArgumentException e){
1837-
logger.warn("Element {} was not recognised. Assigning generic element R to it", elementSymbol);
1834+
String elementSymbol = line.substring(76, 78).trim();
1835+
if (elementSymbol.equals("")) {
1836+
logger.warn("Element column was empty. Assigning atom element "
1837+
+ "from Chemical Component Dictionary information", elementSymbol);
1838+
} else {
1839+
try {
1840+
element = Element.valueOfIgnoreCase(elementSymbol);
1841+
guessElement = false;
1842+
} catch (IllegalArgumentException e){
1843+
logger.warn("Element {} was not recognised. Assigning atom element "
1844+
+ "from Chemical Component Dictionary information", elementSymbol);
1845+
}
18381846
}
18391847
} else {
18401848
logger.warn("Missformatted PDB file: element column is not present. "
1841-
+ "Assigning atom elements from Chemical Component Dictionary information");
1849+
+ "Assigning atom element from Chemical Component Dictionary information");
1850+
}
1851+
if (guessElement) {
18421852
String elementSymbol = null;
18431853
if (currentGroup.getChemComp() != null) {
18441854
for (ChemCompAtom a : currentGroup.getChemComp().getAtoms()) {

0 commit comments

Comments
 (0)