33import java .io .IOException ;
44
55import org .biojava .nbio .structure .Structure ;
6- import org .biojava .nbio .structure .StructureException ;
7- import org .biojava .nbio .structure .StructureIO ;
8- import org .rcsb .mmtf .api .MmtfDecodedDataInterface ;
9- import org .rcsb .mmtf .dataholders .MmtfBean ;
106import org .rcsb .mmtf .decoder .BeanToDataApi ;
117import org .rcsb .mmtf .decoder .DataApiToReader ;
12- import org .rcsb .mmtf .deserializers .MessagePackDeserializer ;
13- import org .rcsb .mmtf .encoder .DataApiToBean ;
8+ import org .rcsb .mmtf .decoder .ReaderUtils ;
149import org .rcsb .mmtf .encoder .WriterToDataApi ;
15- import org .rcsb .mmtf .serializers .MessagePackSerializer ;
16- import org .rcsb .mmtf .utils .DownloadUtils ;
10+ import org .rcsb .mmtf .encoder .WriterUtils ;
1711
12+ /**
13+ * A class of functions for reading and writing Biojava structures using MMTF
14+ * @author Anthony Bradley
15+ *
16+ */
1817public class MmtfActions {
19-
20- /**
21- * Utility function to get a Biojava structure from a PDB code.
22- * @param pdbCode the pdb code of the structure you desire
23- * @return a Biojava structure object relating to the input PDB code
24- * @throws IOException
25- */
26- public static Structure getBiojavaStruct (String pdbCode ) throws IOException {
27- return getBiojavaStruct (DownloadUtils .getDataFromUrl (pdbCode ));
28- }
2918
3019 /**
3120 * Utility function to get a Biojava structure from a byte array.
3221 * @param inputByteArray Must be uncompressed (i.e. with entropy compression methods like gzip)
3322 * @return a Biojava structure object relating to the input byte array.
3423 * @throws IOException
3524 */
36- public static Structure getBiojavaStruct ( byte [] inputByteArray ) throws IOException {
25+ public static Structure readBiojavaStruct ( String filePath ) throws IOException {
3726 // Get the reader - this is the bit that people need to implement.
3827 MmtfStructureReader mmtfStructureReader = new MmtfStructureReader ();
39- // Set up the deserializer
40- MessagePackDeserializer messagePackDeserializer = new MessagePackDeserializer ();
41- // Get the data
42- MmtfBean mmtfBean = messagePackDeserializer .deserialize (inputByteArray );
43- // Set up the data API
44- BeanToDataApi beanToGet = new BeanToDataApi (mmtfBean );
4528 // Set up the inflator
4629 DataApiToReader getToInflator = new DataApiToReader ();
4730 // Do the inflation
48- getToInflator .read (beanToGet , mmtfStructureReader );
31+ getToInflator .read (new BeanToDataApi ( ReaderUtils . getDataFromFile ( filePath )) , mmtfStructureReader );
4932 // Get the structue
5033 return mmtfStructureReader .getStructure ();
5134 }
52-
53- /**
54- * Get the byte array (messagepack encoded) of the PDB code you desire.
55- * @param pdbId the input PDB id for the structure you want
56- * @return the byte array of the structure compressed and message pack encoded
57- * @throws IOException
58- * @throws StructureException
59- */
60- public static byte [] getByteArray (String pdbId ) throws IOException , StructureException {
61- MmtfUtils .setUpBioJava ();
62- return getByteArray (StructureIO .getStructure (pdbId ));
63- }
64-
65- /**
66- * Utility function to get a byte array from a Biojava structure.
67- * @param structure the input Biojava structure object
68- * @return the byte array of the structure compressed and message pack encoded
69- * @throws IOException
70- */
71- public static byte [] getByteArray (Structure structure ) throws IOException {
72- MessagePackSerializer messagePackSerializer = new MessagePackSerializer ();
73- return messagePackSerializer .serialize (getBean (structure ));
74- }
75-
76- /**
77- * Utility function to get an mmtf bean from a Biojava structure.
78- * @param structure the input Biojava structure object
79- * @return the raw (compressed) data as an MmtfBean object
80- * @throws IOException
81- */
82- public static MmtfBean getBean (Structure structure ) throws IOException {
83- // Get to bean
84- DataApiToBean getToBean = new DataApiToBean (getApi (structure ));
85- return getToBean .getMmtfBean ();
86- }
8735
8836 /**
89- * Utility function to get an mmtf bean from a PDB id.
90- * @param pdbId the input PDB id for the structure you want
91- * @return the byte array of the structure compressed and message pack encoded
92- * @throws IOException
93- * @throws StructureException
94- */
95- public static MmtfBean getBean (String pdbId ) throws IOException , StructureException {
96- // Get to bean
97- DataApiToBean getToBean = new DataApiToBean (getApi (pdbId ));
98- return getToBean .getMmtfBean ();
99- }
100-
101- /**
102- * Utility function to get an API to the data from a Biojava structure
103- * @param structure the input Biojava structure object
104- * @return the API to the data in the form of an MmtfDecodedDataInterface
105- * @throws IOException
37+ * Utility function to write a Biojava structure object to a path.
38+ * @param structure the structure to write
39+ * @param path the full path of the file to write
40+ * @throws IOException
10641 */
107- public static MmtfDecodedDataInterface getApi (Structure structure ) throws IOException {
108- // Set up the transform from the inflator to the get api
42+ public static void writeBiojavaStruct (Structure structure , String path ) throws IOException {
43+ // Set up this writer
10944 WriterToDataApi inflatorToGet = new WriterToDataApi ();
11045 // Get the writer - this is what people implement
11146 MmtfStructureWriter mmtfStructureWriter = new MmtfStructureWriter (structure );
112- // Now deflate
47+ // Now pass to the get API
11348 mmtfStructureWriter .write (inflatorToGet );
114- // Return the API
115- return inflatorToGet ;
49+ // Now write this dat to file
50+ WriterUtils . writeDataToFile ( inflatorToGet , path ) ;
11651 }
11752
118- /**
119- * Utility function to get an API to the data from a PDB code.
120- * @param pdbId the input PDB id for the structure you want
121- * @return the API to the data in the form of an MmtfDecodedDataInterface
122- * @throws IOException
123- * @throws StructureException
124- */
125- public static MmtfDecodedDataInterface getApi (String pdbId ) throws IOException , StructureException {
126- // Set up the transform from the inflator to the get api
127- WriterToDataApi inflatorToGet = new WriterToDataApi ();
128- // Get the writer - this is what people implement
129- MmtfStructureWriter mmtfStructureWriter = new MmtfStructureWriter (
130- StructureIO .getStructure (pdbId ));
131- // Now deflate
132- mmtfStructureWriter .write (inflatorToGet );
133- // Get to bean
134- return inflatorToGet ;
135- }
136-
137- /**
138- * Round trip a given structure. Mainly for testing purposes.
139- * @param structure the input Biojava structure object
140- * @return the round tripped structure (conversion to messge pack mmtf and back).
141- * @throws IOException
142- */
143- public static Structure roundTrip (Structure structure ) throws IOException {
144- return getBiojavaStruct (getByteArray (structure ));
145- }
146- }
53+ }
0 commit comments