use of org.rcsb.mmtf.serialization.MessagePackSerialization in project mmtf-spark by sbl-sdsc.
the class MmtfReader method readMmtfFiles.
/**
* Reads uncompressed and compressed MMTF files recursively from
* a given directory.
* This methods reads files with the mmtf or mmtf.gz extension.
*
* @param path Path to MMTF files
* @param sc Spark context
* @return structure data as keyword/value pairs
*/
public static JavaPairRDD<String, StructureDataInterface> readMmtfFiles(String path, JavaSparkContext sc) {
return sc.parallelize(getFiles(path)).mapToPair(new PairFunction<File, String, StructureDataInterface>() {
private static final long serialVersionUID = 9018971417443154996L;
public Tuple2<String, StructureDataInterface> call(File f) throws Exception {
try {
if (f.toString().contains(".mmtf.gz")) {
InputStream in = new FileInputStream(f);
MmtfStructure mmtf = new MessagePackSerialization().deserialize(new GZIPInputStream(in));
return new Tuple2<String, StructureDataInterface>(f.getName().substring(0, f.getName().indexOf(".mmtf")), new GenericDecoder(mmtf));
} else if (f.toString().contains(".mmtf")) {
InputStream in = new FileInputStream(f);
MmtfStructure mmtf = new MessagePackSerialization().deserialize(in);
return new Tuple2<String, StructureDataInterface>(f.getName().substring(0, f.getName().indexOf(".mmtf")), new GenericDecoder(mmtf));
} else
return null;
} catch (Exception e) {
System.out.println(e);
return null;
}
}
}).filter(t -> t != null);
}
use of org.rcsb.mmtf.serialization.MessagePackSerialization in project mmtf-spark by sbl-sdsc.
the class MmtfWriter method toByteArray.
/**
* Returns an MMTF-encoded byte array with optional gzip compression.
* @return MMTF encoded and optionally gzipped structure data
* @throws IOException
*/
private static byte[] toByteArray(StructureDataInterface structure, boolean compressed) throws IOException {
GenericEncoder genericEncoder = new GenericEncoder(structure);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
MessagePackSerialization serializer = new MessagePackSerialization();
serializer.serialize(genericEncoder.getMmtfEncodedStructure(), bos);
if (compressed) {
return WriterUtils.gzipCompress(bos.toByteArray());
} else {
return bos.toByteArray();
}
}
Aggregations