Search in sources :

Example 11 with Polymer

use of model.Polymer in project Smiles2Monomers by yoann-dufresne.

the class PolymersJsonLoader method objectFromJson.

@SuppressWarnings("unchecked")
@Override
protected Polymer objectFromJson(JSONObject obj) {
    if ("".equals((String) obj.get("smiles"))) {
        System.err.println("No smiles for " + ((String) obj.get("name")));
        return null;
    } else if (((String) obj.get("smiles")).contains(".")) {
        System.err.println("The smiles for " + ((String) obj.get("name")) + " contains character '.'");
        System.err.println("The '.' means that the smiles is composed of more than one molecule.");
        System.err.println("Please split the smiles in two distinct smiles.");
        return null;
    }
    // --- Parse the graph ---
    JSONArray verticies;
    JSONArray edges;
    if (obj.containsKey("graph")) {
        JSONObject graph = (JSONObject) obj.get("graph");
        // Parse the s2m graph format
        verticies = (JSONArray) graph.get("V");
        edges = (JSONArray) graph.get("E");
    } else if (obj.containsKey("structure")) {
        verticies = new JSONArray();
        edges = new JSONArray();
        // Parse the Norine graph format
        String text = (String) ((JSONObject) obj.get("structure")).get("graph");
        String[] split = text.split("@");
        // The verticies
        verticies = new JSONArray();
        for (String val : split[0].split(",")) verticies.add(val);
        // The edges
        edges = new JSONArray();
        for (int idx = 1; idx < split.length; idx++) {
            String[] links = split[idx].split(",");
            for (String val : links) {
                int link_to = new Integer(val);
                if (idx - 1 < link_to) {
                    JSONArray edge = new JSONArray();
                    edge.add(idx - 1);
                    edge.add(link_to);
                    edges.add(edge);
                }
            }
        }
    } else {
        verticies = new JSONArray();
        edges = new JSONArray();
    }
    // --- Parse the monomer list ---
    Monomer[] monomers = new Monomer[verticies.size()];
    for (int i = 0; i < monomers.length; i++) {
        String name = (String) verticies.get(i);
        try {
            monomers[i] = this.monos.contains(name) ? this.monos.getObject(name) : new Monomer(name, "", "");
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
    // Create the graph in memory
    MonomerGraph g = new MonomerGraph(monomers);
    for (Object o : edges) {
        JSONArray link = (JSONArray) o;
        g.createLink(((Number) link.get(0)).intValue(), ((Number) link.get(1)).intValue());
    }
    int id = 0;
    if (obj.get("id") instanceof String) {
        String sid = (String) obj.get("id");
        id = sid.startsWith("NOR") ? new Integer(sid.substring(3)) : sid.hashCode();
    } else
        id = ((Number) obj.get("id")).intValue();
    Polymer pep = new Polymer(id, (String) obj.get("name"), (String) obj.get("smiles"), monomers);
    pep.setGraph(g);
    return pep;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) MonomerGraph(model.graph.MonomerGraph) Polymer(model.Polymer) JSONObject(org.json.simple.JSONObject) Monomer(model.Monomer)

Example 12 with Polymer

use of model.Polymer in project Smiles2Monomers by yoann-dufresne.

the class ContractedGraphTests method setUp.

@Before
public void setUp() throws Exception {
    MonomersDB monos = new MonomersJsonLoader().loadFile("data_tests/monos.json");
    PolymersDB peps = new PolymersJsonLoader(monos).loadFile("data_tests/peps.json");
    RulesDB rules = RulesJsonLoader.loader.loadFile("data_tests/rules.json");
    ResidueCreator rc = new ResidueCreator(rules);
    FamilyDB families = rc.createResidues(monos);
    ChainLearning cl = new ChainLearning(peps);
    cl.setMarkovianSize(3);
    cl.learn(families);
    ChainsDB chains = cl.getDb();
    MonomericSpliting ms = new MonomericSpliting(families, chains, 2, 2, 3);
    Polymer pol = peps.getObject("633");
    ms.computeCoverage(pol);
    this.coverage = ms.getCoverage();
    this.coverage.calculateGreedyCoverage();
    this.contractedGraph = new ContractedGraph(coverage);
}
Also used : RulesDB(db.RulesDB) MonomericSpliting(algorithms.MonomericSpliting) MonomersJsonLoader(io.loaders.json.MonomersJsonLoader) PolymersDB(db.PolymersDB) MonomersDB(db.MonomersDB) PolymersJsonLoader(io.loaders.json.PolymersJsonLoader) ChainsDB(algorithms.isomorphism.chains.ChainsDB) Polymer(model.Polymer) ResidueCreator(algorithms.ResidueCreator) FamilyDB(db.FamilyDB) ChainLearning(algorithms.isomorphism.chains.ChainLearning) Before(org.junit.Before)

Aggregations

Polymer (model.Polymer)12 MonomersDB (db.MonomersDB)5 PolymersDB (db.PolymersDB)5 PolymersJsonLoader (io.loaders.json.PolymersJsonLoader)5 MappedChain (algorithms.isomorphism.chains.MappedChain)4 Test (org.junit.Test)4 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)4 Monomer (model.Monomer)3 MonomersJsonLoader (io.loaders.json.MonomersJsonLoader)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 IMolecule (org.openscience.cdk.interfaces.IMolecule)2 MonomericSpliting (algorithms.MonomericSpliting)1 Planarity (algorithms.Planarity)1 ResidueCreator (algorithms.ResidueCreator)1 Chain (algorithms.isomorphism.chains.Chain)1 ChainLearning (algorithms.isomorphism.chains.ChainLearning)1 ChainsDB (algorithms.isomorphism.chains.ChainsDB)1 Extension (algorithms.isomorphism.chains.Extension)1 Coverage (algorithms.utils.Coverage)1