Search in sources :

Example 6 with Family

use of model.Family 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 7 with Family

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

the class ResidueCreatorTests method linksLoadingTest.

@Test
public void linksLoadingTest() {
    ResidueJsonLoader rjl = new ResidueJsonLoader(this.rules, this.monos);
    rjl.saveFile(this.families, "tmp.json");
    Residue.resetResidues();
    FamilyDB loaded = rjl.loadFile("tmp.json");
    new File("tmp.json").delete();
    Family famTyr = null;
    try {
        famTyr = loaded.getObject("Tyr");
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    Residue tyrN = null;
    for (Residue res : famTyr.getResidues()) if ("Tyr_pepN".equals(res.getName())) {
        tyrN = res;
        break;
    }
    Entry<IAtom, Rule> entry = tyrN.getAtomicLinks().entrySet().iterator().next();
    IAtom a = entry.getKey();
    Assert.assertEquals(a.getSymbol(), "N");
}
Also used : Residue(model.Residue) Family(model.Family) FamilyDB(db.FamilyDB) Rule(model.Rule) ResidueJsonLoader(io.loaders.json.ResidueJsonLoader) File(java.io.File) IAtom(org.openscience.cdk.interfaces.IAtom) Test(org.junit.Test)

Example 8 with Family

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

the class FamilyChainIO method objectFromJson.

@Override
protected FamilyChainsDB objectFromJson(JSONObject jso) {
    // FC creation
    String famName = (String) jso.get("family");
    if (famName.contains("€"))
        famName = famName.split("€")[0];
    Family fam = this.families.getObject(famName);
    FamilyChainsDB fc = new FamilyChainsDB(fam);
    // Roots
    JSONObject jsonRoots = (JSONObject) jso.get("roots");
    for (Object objIdx : jsonRoots.keySet()) {
        String idx = (String) objIdx;
        fc.addRootChain(this.residues.getObject(idx), new Chain((String) jsonRoots.get(objIdx)));
    }
    // Adds
    JSONObject jsonAdds = (JSONObject) jso.get("extensions");
    for (Object objIdx : jsonAdds.keySet()) {
        String idx = (String) objIdx;
        Residue current = this.residues.getObject(idx);
        JSONArray jsonAddsList = (JSONArray) jsonAdds.get(objIdx);
        for (Object obj : jsonAddsList) {
            JSONObject add = (JSONObject) obj;
            Residue from = this.residues.getObject((String) add.get("from"));
            ChainAdd ca = null;
            // TODO : Change it for better usage of genericity.
            switch((String) add.get("type")) {
                case "hydrogen":
                    ca = new HydrogenAdd(from, ((Number) add.get("idx")).intValue(), ((Number) add.get("num")).intValue());
                    break;
                case "extension":
                    ca = new BondAdd(from, new Extension((String) add.get("ext")), ((Number) add.get("idx1")).intValue(), ((Number) add.get("idx2")).intValue());
                    break;
                default:
                    break;
            }
            fc.addAnAdd(current, ca);
        }
    }
    return fc;
}
Also used : Chain(algorithms.isomorphism.chains.Chain) HydrogenAdd(algorithms.isomorphism.chains.HydrogenAdd) JSONArray(org.json.simple.JSONArray) Extension(algorithms.isomorphism.chains.Extension) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) JSONObject(org.json.simple.JSONObject) Residue(model.Residue) ChainAdd(algorithms.isomorphism.chains.ChainAdd) Family(model.Family) JSONObject(org.json.simple.JSONObject) BondAdd(algorithms.isomorphism.chains.BondAdd)

Example 9 with Family

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

the class Coverage method getCorrectMonomers.

public Map<String, Integer> getCorrectMonomers(FamilyDB families) {
    this.calculateCorrectIncorrectNotFound(families);
    Map<String, Integer> corrects = new HashMap<>();
    for (Residue res : this.corrects.keySet()) {
        Family fam = families.getObject(res.getMonoName());
        String name = fam.getShortName();
        int val = corrects.containsKey(name) ? corrects.get(name) : 0;
        corrects.put(name, val + this.corrects.get(res));
    }
    return corrects;
}
Also used : HashMap(java.util.HashMap) Residue(model.Residue) Family(model.Family)

Example 10 with Family

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

the class ContractedGraph method toMonomerGraph.

/**
 * Transform this contracted graph into a classical monomer graph.
 * @param fams The family database needed to always refer the same monomer of a family.
 * @return The monomer graph
 */
public MonomerGraph toMonomerGraph(FamilyDB fams) {
    // Transformation to a monomer graph
    List<Vertex> verticiesOrder = new ArrayList<>();
    List<Monomer> monos = new ArrayList<>();
    List<Residue> residues = new ArrayList<>();
    for (Object o : this.vertexSet()) {
        Vertex v = (Vertex) o;
        verticiesOrder.add(v);
        Residue res = v.res;
        if (res != null) {
            Family fam = null;
            try {
                fam = fams.getObject(res.getMonoName());
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            monos.add(fam.getPrincipalMonomer());
            residues.add(res);
        } else {
            monos.add(null);
            residues.add(null);
        }
    }
    Monomer[] array = new Monomer[monos.size()];
    for (int i = 0; i < monos.size(); i++) array[i] = monos.get(i);
    Residue[] resArray = new Residue[monos.size()];
    for (int i = 0; i < monos.size(); i++) resArray[i] = residues.get(i);
    MonomerGraph monoGraph = new MonomerGraph(array, resArray);
    for (Object o : this.edgeSet()) {
        LabeledEdge e = (LabeledEdge) o;
        int mono1 = verticiesOrder.indexOf((Vertex) e.getSource());
        int mono2 = verticiesOrder.indexOf((Vertex) e.getTarget());
        monoGraph.createLink(mono1, mono2, e.sourceLabel, e.targetLabel);
    }
    return monoGraph;
}
Also used : ArrayList(java.util.ArrayList) Residue(model.Residue) Family(model.Family) Monomer(model.Monomer)

Aggregations

Family (model.Family)14 Residue (model.Residue)11 Monomer (model.Monomer)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 FamilyDB (db.FamilyDB)3 Rule (model.Rule)3 IAtom (org.openscience.cdk.interfaces.IAtom)3 ResidueJsonLoader (io.loaders.json.ResidueJsonLoader)2 File (java.io.File)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 BondAdd (algorithms.isomorphism.chains.BondAdd)1 Chain (algorithms.isomorphism.chains.Chain)1 ChainAdd (algorithms.isomorphism.chains.ChainAdd)1 Extension (algorithms.isomorphism.chains.Extension)1 FamilyChainsDB (algorithms.isomorphism.chains.FamilyChainsDB)1 HydrogenAdd (algorithms.isomorphism.chains.HydrogenAdd)1 Coverage (algorithms.utils.Coverage)1 Match (algorithms.utils.Match)1