Search in sources :

Example 6 with Monomer

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

the class CoveragesJsonLoader method getJSONGraph.

@SuppressWarnings("unchecked")
private JSONObject getJSONGraph(Coverage cov) {
    ContractedGraph cg = new ContractedGraph(cov);
    MonomerGraph mg = cg.toMonomerGraph(families);
    JSONObject graph = new JSONObject();
    // Monomers
    JSONArray monos = new JSONArray();
    for (Monomer mono : mg.nodes) if (mono != null)
        monos.add(mono.getName());
    else
        monos.add("?");
    graph.put("monos", monos);
    // Residues (equivalent to monomers)
    JSONArray residues = new JSONArray();
    for (Residue res : mg.residues) if (res != null)
        residues.add(res.getId());
    else
        residues.add("?");
    graph.put("residues", residues);
    // Links
    JSONArray links = new JSONArray();
    for (MonomerLinks ml : mg.links) {
        JSONObject link = new JSONObject();
        JSONArray idxs = new JSONArray();
        idxs.add(ml.mono1);
        idxs.add(ml.mono2);
        link.put("idxs", idxs);
        JSONArray type = new JSONArray();
        type.add(ml.label1);
        type.add(ml.label2);
        link.put("types", type);
        links.add(link);
    }
    graph.put("links", links);
    return graph;
}
Also used : MonomerLinks(model.graph.MonomerGraph.MonomerLinks) JSONObject(org.json.simple.JSONObject) Residue(model.Residue) ContractedGraph(model.graph.ContractedGraph) JSONArray(org.json.simple.JSONArray) MonomerGraph(model.graph.MonomerGraph) Monomer(model.Monomer)

Example 7 with Monomer

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

the class Coverage method calculateCorrectIncorrectNotFound.

public void calculateCorrectIncorrectNotFound(FamilyDB families) {
    this.corrects = new HashMap<>();
    this.incorrects = new HashMap<>();
    this.notFound = new HashMap<>();
    List<Monomer> realMonos = new ArrayList<>();
    for (Monomer m : this.co.getGraph().nodes) realMonos.add(m);
    for (Match match : this.usedMatches) {
        Residue r = match.getResidue();
        // For each is correct ?
        boolean correct = false;
        Family fam = null;
        try {
            fam = families.getObject(r.getMonoName());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        for (Monomer m : fam.getMonomers()) {
            if (realMonos.contains(m)) {
                correct = true;
                realMonos.remove(m);
                break;
            }
        }
        if (correct) {
            int nb = this.corrects.containsKey(r) ? this.corrects.get(r) : 0;
            this.corrects.put(r, nb + 1);
        } else {
            int nb = this.incorrects.containsKey(r) ? this.incorrects.get(r) : 0;
            this.incorrects.put(r, nb + 1);
        }
    }
    for (Monomer m : realMonos) {
        String name = null;
        try {
            Family fam = families.getObject(m.getName());
            name = fam.getShortName();
        } catch (NullPointerException e) {
            name = m.getName();
        }
        int nb = this.notFound.containsKey(name) ? this.notFound.get(name) : 0;
        this.notFound.put(name, nb + 1);
    }
}
Also used : Residue(model.Residue) ArrayList(java.util.ArrayList) Family(model.Family) Monomer(model.Monomer)

Example 8 with Monomer

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

the class ResidueCreator method residuesFromMonomers.

/*
	 * Create residues from a monomer according to the rules database.
	 */
private Set<Residue> residuesFromMonomers(Family family) {
    Monomer mono = family.getMonomers().get(0);
    Set<Residue> residues = new HashSet<>();
    RulesDB rules = new RulesDB();
    rules.addDB(this.dbAlone);
    // List of residues in witch we search other residues.
    Set<Residue> searchResidues = new HashSet<>();
    IMolecule m = null;
    try {
        m = mono.getMolecule().clone();
        AtomContainerManipulator.convertImplicitToExplicitHydrogens(m);
    } catch (CloneNotSupportedException e1) {
        e1.printStackTrace();
    }
    searchResidues.add(new Residue(mono.getCode(), this.sg.createSMILES(m), true));
    boolean firstTime = true;
    while (searchResidues.size() != 0) {
        Set<Residue> nextLevelResidues = new HashSet<>();
        for (Residue res : searchResidues) {
            res.setExplicitHydrogens(true);
            Set<Residue> newResidues = new HashSet<>();
            for (Rule rule : rules.getObjects()) {
                Set<Residue> ruleResidues = this.getResiduesWithRule(res, rule);
                newResidues.addAll(ruleResidues);
                for (Residue r : ruleResidues) nextLevelResidues.add(r);
            }
            for (Residue newRes : newResidues) {
                family.addResidue(newRes);
                if (family.containsMonomer(res.getMonoName()) && res.getAtomicLinks().size() > 0)
                    family.addDependance(newRes, res);
            }
            res.setExplicitHydrogens(false);
        }
        searchResidues = nextLevelResidues;
        residues.addAll(searchResidues);
        if (firstTime) {
            firstTime = false;
            rules.addDB(this.dbNotAlone);
        }
    }
    if (residues.size() == 0) {
        String smiles = this.sg.createSMILES(m);
        Residue res = Residue.constructResidue(mono.getName(), smiles);
        family.addResidue(res);
        residues.add(res);
    }
    if (this.verbose) {
        System.out.println("Nb residues of " + mono.getCode() + " : " + residues.size());
        for (Residue res : residues) System.out.println("  " + res.getSmiles());
        System.out.println();
    }
    return residues;
}
Also used : RulesDB(db.RulesDB) IMolecule(org.openscience.cdk.interfaces.IMolecule) Residue(model.Residue) Rule(model.Rule) Monomer(model.Monomer) HashSet(java.util.HashSet)

Example 9 with Monomer

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

the class FamilyDB method init.

public void init(MonomersDB monoDB) {
    Map<String, List<Monomer>> clusters = new HashMap<>();
    for (Monomer mono : monoDB.getObjects()) {
        String smiles = null;
        try {
            smiles = SmilesConverter.conv.toCanonicalSmiles(mono.getSmiles());
        } catch (InvalidSmilesException e) {
            System.err.println("Impossible to parse " + mono.getName() + " id:" + mono.getId());
            System.err.println(mono.getSmiles());
            continue;
        }
        List<Monomer> cluster = clusters.containsKey(smiles) ? clusters.get(smiles) : new ArrayList<Monomer>();
        cluster.add(mono);
        clusters.put(smiles, cluster);
    }
    for (String smiles : clusters.keySet()) {
        Family family = new Family();
        for (Monomer mono : clusters.get(smiles)) {
            family.addMonomer(mono);
            this.addObject(mono.getCode(), family);
        }
    }
}
Also used : HashMap(java.util.HashMap) Family(model.Family) List(java.util.List) ArrayList(java.util.ArrayList) Monomer(model.Monomer) InvalidSmilesException(org.openscience.cdk.exception.InvalidSmilesException)

Example 10 with Monomer

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

the class HTMLColoredCoverageVue method createColoredList.

private String createColoredList(String listName, Map<String, Integer> monomers, Map<String, List<Color>> colors) {
    String monosHTML = "";
    monosHTML += "<div class='list' id='" + listName + "'>\n";
    for (String name : monomers.keySet()) {
        // HTML for each Residue matched.
        Monomer monomer = null;
        try {
            monomer = this.monosDB.getObject(name);
        } catch (NullPointerException e) {
            System.err.println(e.getMessage());
            for (Monomer candidate : monosDB.getObjects()) {
                if (candidate.getName().toLowerCase().equals(name.toLowerCase())) {
                    monomer = candidate;
                    break;
                }
            }
        }
        List<Color> col = colors.containsKey(name) ? colors.get(name) : new ArrayList<Color>();
        String code = monomer == null ? name + "_unloaded" : monomer.getCode();
        HTMLMonomerShortVue hmsv = new HTMLMonomerShortVue(code, monomers.get(name), col);
        monosHTML += hmsv.getHTML();
        // CSS for each Residue matched.
        Map<String, Map<String, String>> properties = hmsv.getCSSProperties();
        for (String property : properties.keySet()) {
            Map<String, String> localProperty = this.css.containsKey(property) ? this.css.get(property) : new HashMap<String, String>();
            for (Entry<String, String> value : properties.get(property).entrySet()) localProperty.put(value.getKey(), value.getValue());
            this.css.put(property, localProperty);
        }
    }
    monosHTML += "</div>\n";
    return monosHTML;
}
Also used : Color(java.awt.Color) Monomer(model.Monomer) HashMap(java.util.HashMap) Map(java.util.Map) ColorsMap(io.imgs.PictureCoverageGenerator.ColorsMap)

Aggregations

Monomer (model.Monomer)16 Residue (model.Residue)6 ArrayList (java.util.ArrayList)5 Family (model.Family)5 HashMap (java.util.HashMap)4 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 Polymer (model.Polymer)3 InvalidSmilesException (org.openscience.cdk.exception.InvalidSmilesException)3 MonomersDB (db.MonomersDB)2 Rule (model.Rule)2 MonomerGraph (model.graph.MonomerGraph)2 MonomerLinks (model.graph.MonomerGraph.MonomerLinks)2 Before (org.junit.Before)2 IAtom (org.openscience.cdk.interfaces.IAtom)2 IMolecule (org.openscience.cdk.interfaces.IMolecule)2 ResidueCreator (algorithms.ResidueCreator)1 Chain (algorithms.isomorphism.chains.Chain)1 Extension (algorithms.isomorphism.chains.Extension)1 MappedChain (algorithms.isomorphism.chains.MappedChain)1