Skip to content

Commit 7085ddb

Browse files
authored
Merge pull request #1145 from biojava/jd/cath-fixes
Fix ArrayIndexOutOfBoundsException and file validation in CathInstallation
2 parents 1fdfd2e + 954d75a commit 7085ddb

2 files changed

Lines changed: 113 additions & 11 deletions

File tree

‎biojava-structure/src/main/java/org/biojava/nbio/structure/cath/CathInstallation.java‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,15 @@ private void parseCathDomainList() throws IOException {
350350
parseCathDomainList(buffer);
351351
}
352352

353-
private void parseCathDomainList(BufferedReader bufferedReader) throws IOException{
353+
protected void parseCathDomainList(BufferedReader bufferedReader) throws IOException{
354354
String line;
355-
// int counter = 0;
355+
int counter = 0;
356356
while ( (line = bufferedReader.readLine()) != null ) {
357357
if ( line.startsWith("#") ) continue;
358+
if ( line.trim().isEmpty() ) continue;
358359
CathDomain cathDomain = parseCathListFileLine(line);
359-
// counter++;
360+
if ( cathDomain == null ) continue;
361+
counter++;
360362

361363
String pdbId = cathDomain.getPdbIdAndChain().substring(0,4); // includes chain letter
362364

@@ -372,6 +374,9 @@ private void parseCathDomainList(BufferedReader bufferedReader) throws IOExcepti
372374

373375
domainMap.put( cathDomain.getDomainName(), cathDomain );
374376
}
377+
if (counter == 0) {
378+
throw new IOException("Could not parse any CATH domains from the domain list file.");
379+
}
375380
}
376381

377382
private void parseCathNames() throws IOException {
@@ -386,7 +391,9 @@ private void parseCathNames(BufferedReader bufferedReader) throws IOException{
386391
//int counter = 0;
387392
while ( (line = bufferedReader.readLine()) != null ) {
388393
if ( line.startsWith("#") ) continue;
394+
if ( line.trim().isEmpty() ) continue;
389395
CathNode cathNode = parseCathNamesFileLine(line);
396+
if ( cathNode == null ) continue;
390397
cathTree.put(cathNode.getNodeId(), cathNode);
391398
}
392399
}
@@ -413,6 +420,7 @@ private void parseCathDomainDescriptionFile(BufferedReader bufferedReader) throw
413420
StringBuilder sseqs = null;
414421
while ( (line = bufferedReader.readLine()) != null ) {
415422
if ( line.startsWith("#") ) continue;
423+
if ( line.trim().isEmpty() ) continue;
416424
if ( line.startsWith("FORMAT") ) {
417425
cathDescription = new CathDomain();
418426
cathDescription.setFormat( line.substring(10) );
@@ -504,8 +512,11 @@ private void parseCathDomainDescriptionFile(BufferedReader bufferedReader) throw
504512
}*/
505513

506514
private CathDomain parseCathListFileLine(String line) {
515+
String [] token = line.trim().split("\\s+");
516+
if (token.length < 12) {
517+
return null;
518+
}
507519
CathDomain cathDomain = new CathDomain();
508-
String [] token = line.split("\\s+");
509520
cathDomain.setDomainName(token[0]);
510521
cathDomain.setClassId(Integer.parseInt(token[1]));
511522
cathDomain.setArchitectureId(Integer.parseInt(token[2]));
@@ -522,8 +533,12 @@ private CathDomain parseCathListFileLine(String line) {
522533
}
523534

524535
private CathNode parseCathNamesFileLine(String line) {
536+
String[] token = line.trim().split("\\s+",3);
537+
if (token.length < 3) {
538+
LOGGER.debug("Invalid line in cath names file, was expecting 3 tokens but got {} tokens: {}", token.length, line);
539+
return null;
540+
}
525541
CathNode cathNode = new CathNode();
526-
String[] token = line.split("\\s+",3);
527542
cathNode.setNodeId( token[0] );
528543
int idx = token[0].lastIndexOf(".");
529544
if ( idx == -1 ) idx = token[0].length();
@@ -544,8 +559,8 @@ private void parseCathDomall(BufferedReader bufferedReader) throws IOException{
544559
String line;
545560
while ( ((line = bufferedReader.readLine()) != null) ) {
546561
if ( line.startsWith("#") ) continue;
547-
if ( line.length() == 0 ) continue;
548-
String[] token = line.split("\\s+");
562+
if ( line.trim().isEmpty() ) continue;
563+
String[] token = line.trim().split("\\s+");
549564
String chainId = token[0];
550565
Integer numberOfDomains = Integer.parseInt( token[1].substring(1) );
551566
Integer numberOfFragments = Integer.parseInt( token[2].substring(1) );
@@ -671,25 +686,25 @@ protected void downloadFileFromRemote(URL remoteURL, File localFile) throws IOEx
671686
private boolean domainDescriptionFileAvailable(){
672687
String fileName = getDomainDescriptionFileName();
673688
File f = new File(fileName);
674-
return f.exists();
689+
return f.exists() && FileDownloadUtils.validateFile(f);
675690
}
676691

677692
private boolean domainListFileAvailable(){
678693
String fileName = getDomainListFileName();
679694
File f = new File(fileName);
680-
return f.exists();
695+
return f.exists() && FileDownloadUtils.validateFile(f);
681696
}
682697

683698
private boolean nodeListFileAvailable(){
684699
String fileName = getNodeListFileName();
685700
File f = new File(fileName);
686-
return f.exists();
701+
return f.exists() && FileDownloadUtils.validateFile(f);
687702
}
688703

689704
private boolean domallFileAvailable() {
690705
String fileName = getDomallFileName();
691706
File f= new File(fileName);
692-
return f.exists();
707+
return f.exists() && FileDownloadUtils.validateFile(f);
693708
}
694709

695710
protected void downloadDomainListFile() throws IOException{
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* BioJava development code
3+
*
4+
* This code may be freely distributed and modified under the
5+
* terms of the GNU Lesser General Public Licence. This should
6+
* be distributed with the code. If you do not have a copy,
7+
* see:
8+
*
9+
* http://www.gnu.org/copyleft/lesser.html
10+
*
11+
* Copyright for this code is held jointly by the individual
12+
* authors. These should be listed in @author doc comments.
13+
*
14+
* For more information on the BioJava project and its aims,
15+
* or to join the biojava-l mailing list, visit the home page
16+
* at:
17+
*
18+
* http://www.biojava.org/
19+
*/
20+
package org.biojava.nbio.structure.cath;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.BufferedReader;
25+
import java.io.IOException;
26+
import java.io.StringReader;
27+
import java.util.concurrent.atomic.AtomicBoolean;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.assertNotNull;
31+
import static org.junit.jupiter.api.Assertions.assertThrows;
32+
33+
public class CathInstallationTest {
34+
35+
@Test
36+
public void testParseCathDomainListSuccess() throws IOException {
37+
String data = "# CATH domain list\n"
38+
+ "\n"
39+
+ "1oaiA00 1 10 490 10 1 1 1 1 1 124 1.80\n"
40+
+ "1oaiA01 1 10 490 10 1 1 1 1 2 150 1.80\n";
41+
42+
CathInstallation installation = new CathInstallation("");
43+
BufferedReader reader = new BufferedReader(new StringReader(data));
44+
installation.parseCathDomainList(reader);
45+
installation.setInstalledDomainList(new AtomicBoolean(true)); //1
46+
installation.setInstalledDomall(new AtomicBoolean(true)); //2
47+
48+
CathDomain domain = installation.getDomainByCathId("1oaiA00");
49+
assertNotNull(domain);
50+
assertEquals("1oaiA00", domain.getDomainName());
51+
assertEquals(1, domain.getClassId());
52+
assertEquals(10, domain.getArchitectureId());
53+
assertEquals(490, domain.getTopologyId());
54+
assertEquals(10, domain.getHomologyId());
55+
assertEquals(124, domain.getLength());
56+
assertEquals(1.80, domain.getResolution(), 0.001);
57+
}
58+
59+
@Test
60+
public void testParseCathDomainListEmptyThrowsException() {
61+
CathInstallation installation = new CathInstallation("");
62+
BufferedReader reader = new BufferedReader(new StringReader(""));
63+
assertThrows(IOException.class, () -> installation.parseCathDomainList(reader));
64+
}
65+
66+
@Test
67+
public void testParseCathDomainListOnlyCommentsAndWhitespaceThrowsException() {
68+
String data = "# comment 1\n"
69+
+ "# comment 2\n"
70+
+ " \n"
71+
+ "\t\n";
72+
CathInstallation installation = new CathInstallation("");
73+
BufferedReader reader = new BufferedReader(new StringReader(data));
74+
assertThrows(IOException.class, () -> installation.parseCathDomainList(reader));
75+
}
76+
77+
@Test
78+
public void testParseCathDomainListNoParsableLinesThrowsException() {
79+
String data = "# comment\n"
80+
+ "invalid line with too few tokens\n"
81+
+ "another bad line\n";
82+
CathInstallation installation = new CathInstallation("");
83+
BufferedReader reader = new BufferedReader(new StringReader(data));
84+
IOException exception = assertThrows(IOException.class, () -> installation.parseCathDomainList(reader));
85+
assertNotNull(exception.getMessage());
86+
}
87+
}

0 commit comments

Comments
 (0)