Search in sources :

Example 11 with AdapterToStructureData

use of org.rcsb.mmtf.encoder.AdapterToStructureData 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 12 with AdapterToStructureData

use of org.rcsb.mmtf.encoder.AdapterToStructureData 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)

Example 13 with AdapterToStructureData

use of org.rcsb.mmtf.encoder.AdapterToStructureData in project mmtf-spark by sbl-sdsc.

the class StructureToBioassembly method call.

@Override
public Iterator<Tuple2<String, StructureDataInterface>> call(Tuple2<String, StructureDataInterface> t) throws Exception {
    StructureDataInterface structure = t._2;
    // Map<Integer, Integer> atomMap = new HashMap<>();
    List<Tuple2<String, StructureDataInterface>> resList = new ArrayList<>();
    // for each of them, create one structure.
    for (int i = 0; i < structure.getNumBioassemblies(); i++) {
        // initiate the bioassembly structure.
        AdapterToStructureData bioAssembly = new AdapterToStructureData();
        // set the structureID.
        String structureId = structure.getStructureId() + "-BioAssembly" + structure.getBioassemblyName(i);
        int totAtoms = 0, totBonds = 0, totGroups = 0, totChains = 0, totModels = 0;
        int numTrans = structure.getNumTransInBioassembly(i);
        totModels = structure.getNumModels();
        int[][] bioChainList = new int[numTrans][];
        double[][] transMatrix = new double[numTrans][];
        // calculate the total data we will use to initialize the structure.
        for (int ii = 0; ii < numTrans; ii++) {
            bioChainList[ii] = structure.getChainIndexListForTransform(i, ii);
            transMatrix[ii] = structure.getMatrixForTransform(i, ii);
            for (int j = 0; j < totModels; j++) {
                totChains += bioChainList[ii].length;
                // System.out.println(bioChainList[ii].length);
                for (int k = 0, groupCounter = 0; k < structure.getChainsPerModel()[j]; k++) {
                    boolean adding = false;
                    for (int currChain : bioChainList[ii]) {
                        if (currChain == k)
                            adding = true;
                    }
                    if (adding) {
                        // System.out.println("adding groups");
                        totGroups += structure.getGroupsPerChain()[k];
                    }
                    for (int h = 0; h < structure.getGroupsPerChain()[k]; h++, groupCounter++) {
                        if (adding) {
                            int groupIndex = structure.getGroupTypeIndices()[groupCounter];
                            totAtoms += structure.getNumAtomsInGroup(groupIndex);
                            totBonds += structure.getGroupBondOrders(groupIndex).length;
                        }
                    }
                }
            }
        }
        // init
        // System.out.println("Initializing the structure with\n"
        // + " totModel = " + totModels + ", totChains = " + totChains + ", totGroups = " + totGroups + ", totAtoms = "
        // + totAtoms + ", totBonds = " + totBonds + ", name : " + structureId);
        bioAssembly.initStructure(totBonds, totAtoms, totGroups, totChains, totModels, structureId);
        DecoderUtils.addXtalographicInfo(structure, bioAssembly);
        DecoderUtils.addHeaderInfo(structure, bioAssembly);
        /*
			 * Now we have bioChainList and transMatrix.
			 * bioChainList[i] is the ith trans' list of chains it has.  
			 * transMatrix[i] is the matrix that is going to be applied on those chains.
			 */
        // initialize the indices.
        int modelIndex = 0;
        int chainIndex = 0;
        int groupIndex = 0;
        int atomIndex = 0;
        int chainCounter = 0;
        // loop through models
        for (int ii = 0; ii < structure.getNumModels(); ii++) {
            // precalculate indices
            int numChainsPerModel = structure.getChainsPerModel()[modelIndex] * numTrans;
            bioAssembly.setModelInfo(modelIndex, numChainsPerModel);
            int[] chainToEntityIndex = getChainToEntityIndex(structure);
            // loop through chains
            for (int j = 0; j < structure.getChainsPerModel()[modelIndex]; j++) {
                // loop through each trans
                int currGroupIndex = groupIndex;
                int currAtomIndex = atomIndex;
                for (int k = 0; k < numTrans; k++) {
                    // get the currChainList that needs to be added
                    int[] currChainList = bioChainList[k];
                    double[] currMatrix = transMatrix[k];
                    boolean addThisChain = false;
                    for (int currChain : currChainList) {
                        if (currChain == j)
                            addThisChain = true;
                    }
                    groupIndex = currGroupIndex;
                    atomIndex = currAtomIndex;
                    float[] xCoords = structure.getxCoords();
                    float[] yCoords = structure.getyCoords();
                    float[] zCoords = structure.getzCoords();
                    float[] floatMatrix = Floats.toArray(Doubles.asList(currMatrix));
                    Matrix4f m = new Matrix4f(floatMatrix);
                    if (addThisChain) {
                        int entityToChainIndex = chainToEntityIndex[chainIndex];
                        // System.out.println("adding chain : " + chainIndex);
                        // TODO
                        // not sure
                        bioAssembly.setEntityInfo(new int[] { chainCounter }, structure.getEntitySequence(entityToChainIndex), structure.getEntityDescription(entityToChainIndex), structure.getEntityType(entityToChainIndex));
                        bioAssembly.setChainInfo(structure.getChainIds()[chainIndex], structure.getChainNames()[chainIndex], structure.getGroupsPerChain()[chainIndex]);
                        chainCounter++;
                    }
                    // loop through the groups in the chain
                    for (int jj = 0; jj < structure.getGroupsPerChain()[chainIndex]; jj++) {
                        int currgroup = structure.getGroupTypeIndices()[groupIndex];
                        if (addThisChain) {
                            bioAssembly.setGroupInfo(structure.getGroupName(currgroup), structure.getGroupIds()[groupIndex], structure.getInsCodes()[groupIndex], structure.getGroupChemCompType(currgroup), structure.getNumAtomsInGroup(currgroup), structure.getGroupBondOrders(currgroup).length, structure.getGroupSingleLetterCode(currgroup), structure.getGroupSequenceIndices()[groupIndex], structure.getSecStructList()[groupIndex]);
                        }
                        for (int kk = 0; kk < structure.getNumAtomsInGroup(currgroup); kk++) {
                            // System.out.println("currgroup : " + currgroup + " curratom : " + kk);
                            if (addThisChain) {
                                Point3f p1 = new Point3f(xCoords[atomIndex], yCoords[atomIndex], zCoords[atomIndex]);
                                m.transform(p1);
                                // System.out.println(kk + " " + currgroup);
                                bioAssembly.setAtomInfo(structure.getGroupAtomNames(currgroup)[kk], structure.getAtomIds()[atomIndex], structure.getAltLocIds()[atomIndex], p1.x, p1.y, p1.z, structure.getOccupancies()[atomIndex], structure.getbFactors()[atomIndex], structure.getGroupElementNames(currgroup)[kk], structure.getGroupAtomCharges(currgroup)[kk]);
                            }
                            // inc the atomIndex
                            atomIndex++;
                        }
                        if (addThisChain) {
                            for (int l = 0; l < structure.getGroupBondOrders(currgroup).length; l++) {
                                // System.out.println(structure.getGroupBondOrders(currgroup).length + " " + l);
                                int bondIndOne = structure.getGroupBondIndices(currgroup)[l * 2];
                                int bondIndTwo = structure.getGroupBondIndices(currgroup)[l * 2 + 1];
                                int bondOrder = structure.getGroupBondOrders(currgroup)[l];
                                bioAssembly.setGroupBond(bondIndOne, bondIndTwo, bondOrder);
                            }
                        }
                        // inc the groupIndex
                        groupIndex++;
                    }
                    if (addThisChain) {
                    // Add inter-group bond info
                    // for(int l = 0;  l < structure.getInterGroupBondOrders().length; l++){
                    // int bondIndOne = structure.getInterGroupBondIndices()[l*2];
                    // int bondIndTwo = structure.getInterGroupBondIndices()[l*2+1];
                    // int bondOrder = structure.getInterGroupBondOrders()[l];
                    // Integer indexOne = atomMap.get(bondIndOne);
                    // if (indexOne != null) {
                    // Integer indexTwo = atomMap.get(bondIndTwo);
                    // if (indexTwo != null) {
                    // bioAssembly.setInterGroupBond(indexOne, indexTwo, bondOrder);
                    // }
                    // }
                    }
                }
                // inc the chainIndex
                chainIndex++;
            }
            // inc the modelIndex
            modelIndex++;
        }
        bioAssembly.finalizeStructure();
        resList.add(new Tuple2<String, StructureDataInterface>(structureId, bioAssembly));
    }
    return resList.iterator();
}
Also used : ArrayList(java.util.ArrayList) StructureDataInterface(org.rcsb.mmtf.api.StructureDataInterface) AdapterToStructureData(org.rcsb.mmtf.encoder.AdapterToStructureData) Matrix4f(javax.vecmath.Matrix4f) Point3f(javax.vecmath.Point3f) Tuple2(scala.Tuple2)

Example 14 with AdapterToStructureData

use of org.rcsb.mmtf.encoder.AdapterToStructureData in project mm-dev by sbl-sdsc.

the class Molmporter method readCtab.

private void readCtab() throws IOException {
    // if (line == null) return; // this statement terminates reading her
    // if line 3 of the sdf file is empty, which sometimes occurs.
    readCounts();
    if (atomCount == 0) {
        // initiate molecule with the exact capacity needed to hold atoms
        throw new IOException("Molecule with zero atoms.");
    }
    structure = new AdapterToStructureData();
    structure.initStructure(bondCount, atomCount, 1, 1, 1, structureId);
    structure.setHeaderInfo(99, 99, 99, moleculeName, "20180101", "20180101", new String[] { "THEORETICAL MODEL" });
    structure.setModelInfo(0, 1);
    structure.setChainInfo("L", "L", 1);
    structure.setEntityInfo(new int[] { 0 }, "-1", moleculeName, "non-polymer");
    structure.setGroupInfo("LIG", 1, ' ', "non-polymer", atomCount, bondCount, 'X', -1, -1);
    readAtoms();
    readBonds();
    structure.finalizeStructure();
}
Also used : AdapterToStructureData(org.rcsb.mmtf.encoder.AdapterToStructureData) IOException(java.io.IOException)

Aggregations

AdapterToStructureData (org.rcsb.mmtf.encoder.AdapterToStructureData)14 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 StructureDataInterface (org.rcsb.mmtf.api.StructureDataInterface)7 IOException (java.io.IOException)5 GZIPInputStream (java.util.zip.GZIPInputStream)5 ArrayList (java.util.ArrayList)4 Tuple2 (scala.Tuple2)4 URL (java.net.URL)3 Structure (org.biojava.nbio.structure.Structure)3 MmtfStructureWriter (org.biojava.nbio.structure.io.mmtf.MmtfStructureWriter)3 HashMap (java.util.HashMap)2 Point3f (javax.vecmath.Point3f)2 FileParsingParameters (org.biojava.nbio.structure.io.FileParsingParameters)2 MMCIFFileReader (org.biojava.nbio.structure.io.MMCIFFileReader)2 File (java.io.File)1 HashSet (java.util.HashSet)1 Matrix4d (javax.vecmath.Matrix4d)1 Matrix4f (javax.vecmath.Matrix4f)1