Skip to content

Commit eb0a882

Browse files
committed
Added caching functionality for MMTF
1 parent 529a1ef commit eb0a882

5 files changed

Lines changed: 92 additions & 13 deletions

File tree

biojava-structure/src/main/java/org/biojava/nbio/structure/align/util/AtomCache.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.biojava.nbio.structure.io.LocalPDBDirectory.ObsoleteBehavior;
4141
import org.biojava.nbio.structure.io.mmtf.MmtfActions;
4242
import org.biojava.nbio.structure.io.MMCIFFileReader;
43+
import org.biojava.nbio.structure.io.MMTFFileReader;
4344
import org.biojava.nbio.structure.io.PDBFileReader;
4445
import org.biojava.nbio.structure.io.util.FileDownloadUtils;
4546
import org.biojava.nbio.structure.quaternary.BiologicalAssemblyBuilder;
@@ -1025,13 +1026,20 @@ else if (useMmCif) {
10251026
}
10261027

10271028
/**
1028-
*
1029-
* @param pdbId
1030-
* @return
1031-
* @throws IOException
1029+
* Load a {@link Structure} from MMTF either from the local file system.
1030+
* @param pdbId the input PDB id
1031+
* @return the {@link Structure} object of the parsed structure
1032+
* @throws IOException error reading from Web or file system
10321033
*/
10331034
private Structure loadStructureFromMmtfByPdbId(String pdbId) throws IOException {
1034-
return MmtfActions.readFromWeb(pdbId);
1035+
MMTFFileReader reader = new MMTFFileReader();
1036+
reader.setFetchBehavior(fetchBehavior);
1037+
reader.setObsoleteBehavior(obsoleteBehavior);
1038+
Structure structure = reader.getStructureById(pdbId.toLowerCase());
1039+
return structure;
1040+
1041+
1042+
10351043
}
10361044

10371045
protected Structure loadStructureFromCifByPdbId(String pdbId) throws IOException, StructureException {
@@ -1042,9 +1050,7 @@ protected Structure loadStructureFromCifByPdbId(String pdbId) throws IOException
10421050
MMCIFFileReader reader = new MMCIFFileReader(path);
10431051
reader.setFetchBehavior(fetchBehavior);
10441052
reader.setObsoleteBehavior(obsoleteBehavior);
1045-
10461053
reader.setFileParsingParameters(params);
1047-
10481054
s = reader.getStructureById(pdbId.toLowerCase());
10491055

10501056
} finally {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@
2525
import org.biojava.nbio.structure.PDBStatus.Status;
2626
import org.biojava.nbio.structure.Structure;
2727
import org.biojava.nbio.structure.StructureException;
28+
import org.biojava.nbio.structure.StructureIO;
2829
import org.biojava.nbio.structure.align.util.UserConfiguration;
2930
import org.biojava.nbio.structure.io.util.FileDownloadUtils;
31+
import org.rcsb.mmtf.decoder.ReaderUtils;
32+
import org.rcsb.mmtf.utils.CodecUtils;
3033
import org.biojava.nbio.core.util.InputStreamProvider;
3134
import org.slf4j.Logger;
3235
import org.slf4j.LoggerFactory;
@@ -544,8 +547,15 @@ private File downloadStructure(String pdbId, String pathOnServer, boolean obsole
544547
File dir = getDir(pdbId,obsolete);
545548
File realFile = new File(dir,getFilename(pdbId));
546549

547-
String ftp = String.format("%s%s/%s/%s",
548-
serverName, pathOnServer, pdbId.substring(1,3).toLowerCase(), getFilename(pdbId));
550+
String ftp;
551+
552+
if (getFilename(pdbId).endsWith(".mmtf.gz")){
553+
ftp = ReaderUtils.getUrl(pdbId);
554+
}
555+
else{
556+
ftp = String.format("%s%s/%s/%s",
557+
serverName, pathOnServer, pdbId.substring(1,3).toLowerCase(), getFilename(pdbId));
558+
}
549559

550560
URL url = new URL(ftp);
551561

@@ -570,8 +580,6 @@ private File downloadStructure(String pdbId, String pathOnServer, boolean obsole
570580
logger.info("Fetching " + ftp);
571581
logger.info("Writing to "+ realFile);
572582

573-
574-
575583
FileDownloadUtils.downloadFile(url, realFile);
576584

577585
// Commented out following code used for setting the modified date to the downloaded file - JD 2015-01-15
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.biojava.nbio.structure.io;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import org.biojava.nbio.structure.Structure;
7+
import org.biojava.nbio.structure.io.mmtf.MmtfActions;
8+
9+
/**
10+
* A class to read MMTF files and cache them locally.
11+
* @author Anthony Bradley
12+
*
13+
*/
14+
public class MMTFFileReader extends LocalPDBDirectory {
15+
16+
17+
public static final String[] MMTF_SPLIT_DIR = new String[]{"data","structures","divided" ,"mmtf"};
18+
public static final String[] MMTF_OBSOLETE_DIR = new String[]{"data","structures","obsolete","mmtf"};
19+
20+
public static void main(String[] args) throws Exception {
21+
22+
MMTFFileReader reader = new MMTFFileReader();
23+
FileParsingParameters params = new FileParsingParameters();
24+
reader.setFileParsingParameters(params);
25+
Structure struc = reader.getStructureById("1m4x");
26+
System.out.println(struc);
27+
}
28+
29+
30+
@Override
31+
public Structure getStructure(InputStream inStream) throws IOException {
32+
return MmtfActions.readFromInputStream(inStream);
33+
}
34+
35+
@Override
36+
protected String getFilename(String pdbId) {
37+
return pdbId.toLowerCase()+".mmtf.gz";
38+
}
39+
40+
@Override
41+
protected String[] getSplitDirPath() {
42+
return MMTF_SPLIT_DIR;
43+
}
44+
45+
@Override
46+
protected String[] getObsoleteDirPath() {
47+
return MMTF_OBSOLETE_DIR;
48+
}
49+
50+
51+
}

biojava-structure/src/main/java/org/biojava/nbio/structure/io/mmtf/MmtfActions.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.biojava.nbio.structure.io.mmtf;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.IOException;
4+
import java.io.InputStream;
55
import java.io.OutputStream;
66
import java.nio.file.Path;
77

@@ -80,4 +80,18 @@ public static Structure readFromWeb(String pdbId) throws IOException {
8080
// Get the structue
8181
return mmtfStructureReader.getStructure();
8282
}
83+
84+
/**
85+
* Read a Biojava structure from an {@link InputStream}
86+
* @param inStream the {@link InputStream} to read from
87+
* @return the parsed {@link Structure}
88+
*/
89+
public static Structure readFromInputStream(InputStream inStream) {
90+
// Get the reader - this is the bit that people need to implement.
91+
MmtfStructureReader mmtfStructureReader = new MmtfStructureReader();
92+
// Do the inflation
93+
new StructureDataToAdapter(new GenericDecoder(ReaderUtils.getDataFromInputStream(inStream)), mmtfStructureReader);
94+
// Get the structue
95+
return mmtfStructureReader.getStructure();
96+
}
8397
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3838
<project.build.targetEncoding>UTF-8</project.build.targetEncoding>
3939
<maxmem>512M</maxmem>
40-
<mmtf.version>1.0.0</mmtf.version>
40+
<mmtf.version>1.0.3</mmtf.version>
4141
<slf4j.version>1.7.21</slf4j.version>
4242
<log4j.version>2.6.1</log4j.version>
4343
</properties>

0 commit comments

Comments
 (0)