Search in sources :

Example 1 with FileParsingParameters

use of org.biojava.nbio.structure.io.FileParsingParameters in project mmtf-spark by sbl-sdsc.

the class MmtfImporter method importMmcifFiles.

/**
 * Reads uncompressed and compressed mmCIF files recursively from a given
 * directory path. This methods reads files with the .cif or .cif.gz
 * extension.
 *
 * @param path
 *            Path to .cif files
 * @param sc
 *            Spark context
 * @return structure data as keyword/value pairs
 */
public static JavaPairRDD<String, StructureDataInterface> importMmcifFiles(String path, JavaSparkContext sc) {
    FileParsingParameters params = new FileParsingParameters();
    params.setCreateAtomBonds(true);
    return sc.parallelize(getFiles(path)).mapToPair(new PairFunction<File, String, StructureDataInterface>() {

        private static final long serialVersionUID = -7815663658405168429L;

        public Tuple2<String, StructureDataInterface> call(File f) throws Exception {
            InputStream is = null;
            String path = f.getName();
            // TODO debugging
            System.out.println(path);
            if (path.endsWith(".cif") || path.endsWith((".cif.gz"))) {
                try {
                    is = new FileInputStream(f);
                    if (path.endsWith(".cif.gz")) {
                        is = new GZIPInputStream(is);
                    }
                    // parse .cif file
                    MMCIFFileReader mmcifReader = new MMCIFFileReader();
                    mmcifReader.setFileParsingParameters(params);
                    Structure struc = mmcifReader.getStructure(is);
                    is.close();
                    // convert to mmtf
                    AdapterToStructureData writerToEncoder = new AdapterToStructureData();
                    new MmtfStructureWriter(struc, writerToEncoder);
                    return new Tuple2<String, StructureDataInterface>(path.substring(0, path.indexOf(".cif")), writerToEncoder);
                } catch (Exception e) {
                    System.out.println("WARNING: cannot parse: " + path + ". Skipping this entry!");
                    return null;
                }
            } else {
                return null;
            }
        }
    }).filter(t -> t != null);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MMCIFFileReader(org.biojava.nbio.structure.io.MMCIFFileReader) FileParsingParameters(org.biojava.nbio.structure.io.FileParsingParameters) StructureDataInterface(org.rcsb.mmtf.api.StructureDataInterface) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) AdapterToStructureData(org.rcsb.mmtf.encoder.AdapterToStructureData) Tuple2(scala.Tuple2) PairFunction(org.apache.spark.api.java.function.PairFunction) Structure(org.biojava.nbio.structure.Structure) MmtfStructureWriter(org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter) File(java.io.File)

Example 2 with FileParsingParameters

use of org.biojava.nbio.structure.io.FileParsingParameters in project mmtf-spark by sbl-sdsc.

the class MmtfImporter method getFromMmcifUrl.

/**
 * Reads a mmCIF file from a URL.
 *
 * @param url URL for mmCIF file
 * @return
 * @throws IOException
 */
private static AdapterToStructureData getFromMmcifUrl(String url, String structureId) throws IOException {
    FileParsingParameters params = new FileParsingParameters();
    params.setCreateAtomBonds(true);
    URL u = new URL(url);
    InputStream is = null;
    try {
        is = u.openStream();
    } catch (IOException e) {
        return null;
    }
    try {
        if (url.endsWith(".gz")) {
            is = new GZIPInputStream(is);
        }
    } catch (Exception e) {
        return null;
    }
    try {
        // parse .cif file
        MMCIFFileReader mmcifReader = new MMCIFFileReader();
        mmcifReader.setFileParsingParameters(params);
        Structure struc = mmcifReader.getStructure(is);
        is.close();
        // convert to mmtf
        AdapterToStructureData writerToEncoder = new AdapterToStructureData();
        new MmtfStructureWriter(struc, writerToEncoder);
        return writerToEncoder;
    } catch (Exception e) {
        System.out.println("WARNING: cannot parse: " + url + ". Skipping this entry!");
        return null;
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) AdapterToStructureData(org.rcsb.mmtf.encoder.AdapterToStructureData) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MMCIFFileReader(org.biojava.nbio.structure.io.MMCIFFileReader) FileParsingParameters(org.biojava.nbio.structure.io.FileParsingParameters) IOException(java.io.IOException) Structure(org.biojava.nbio.structure.Structure) MmtfStructureWriter(org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter) URL(java.net.URL) IOException(java.io.IOException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Structure (org.biojava.nbio.structure.Structure)2 FileParsingParameters (org.biojava.nbio.structure.io.FileParsingParameters)2 MMCIFFileReader (org.biojava.nbio.structure.io.MMCIFFileReader)2 MmtfStructureWriter (org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter)2 AdapterToStructureData (org.rcsb.mmtf.encoder.AdapterToStructureData)2 File (java.io.File)1 URL (java.net.URL)1 PairFunction (org.apache.spark.api.java.function.PairFunction)1 StructureDataInterface (org.rcsb.mmtf.api.StructureDataInterface)1 Tuple2 (scala.Tuple2)1