Search in sources :

Example 1 with MmtfStructureWriter

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

the class MmtfImporter method toStructureDataInterface.

/**
 * Parses PDB-formatted input stream and return structure data.
 *
 * @param inputStream PDB-formatted input stream
 * @param structureId id to be assigned to the structure
 * @return structure data
 * @throws IOException
 */
private static AdapterToStructureData toStructureDataInterface(InputStream inputStream, String structureId) throws IOException {
    // standardize PDB formatting
    InputStream pdbIs = standardizePdbInputStream(inputStream);
    // parse PDB and generate BioJava Structure object
    PDBFileParser parser = new PDBFileParser();
    parser.getFileParsingParameters().setCreateAtomBonds(true);
    Structure struct = parser.parsePDBFile(pdbIs);
    struct.setPDBCode(structureId);
    // where the entity info is null when there is only one polymer chain.
    for (EntityInfo info : struct.getEntityInfos()) {
        if (info.getType() == null) {
            for (String chainId : info.getChainIds()) {
                Chain c = struct.getChain(chainId);
                if (c.getAtomSequence().length() > 0) {
                    info.setType(EntityType.POLYMER);
                }
            }
        }
    }
    pdbIs.close();
    // convert to MMTF
    AdapterToStructureData writerToEncoder = new AdapterToStructureData();
    // TODO get version number
    writerToEncoder.setMmtfProducer("mmtf-spark 0.2.0");
    new MmtfStructureWriter(struct, writerToEncoder);
    return writerToEncoder;
}
Also used : Chain(org.biojava.nbio.structure.Chain) AdapterToStructureData(org.rcsb.mmtf.encoder.AdapterToStructureData) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) EntityInfo(org.biojava.nbio.structure.EntityInfo) PDBFileParser(org.biojava.nbio.structure.io.PDBFileParser) Structure(org.biojava.nbio.structure.Structure) MmtfStructureWriter(org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter)

Example 2 with MmtfStructureWriter

use of org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter 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 3 with MmtfStructureWriter

use of org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter 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)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 Structure (org.biojava.nbio.structure.Structure)3 MmtfStructureWriter (org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter)3 AdapterToStructureData (org.rcsb.mmtf.encoder.AdapterToStructureData)3 IOException (java.io.IOException)2 FileParsingParameters (org.biojava.nbio.structure.io.FileParsingParameters)2 MMCIFFileReader (org.biojava.nbio.structure.io.MMCIFFileReader)2 File (java.io.File)1 URL (java.net.URL)1 PairFunction (org.apache.spark.api.java.function.PairFunction)1 Chain (org.biojava.nbio.structure.Chain)1 EntityInfo (org.biojava.nbio.structure.EntityInfo)1 PDBFileParser (org.biojava.nbio.structure.io.PDBFileParser)1 StructureDataInterface (org.rcsb.mmtf.api.StructureDataInterface)1 Tuple2 (scala.Tuple2)1