Search in sources :

Example 1 with Coverage

use of algorithms.utils.Coverage in project Smiles2Monomers by yoann-dufresne.

the class ImagesGeneration method generatePeptidesImages.

public Map<Coverage, ColorsMap> generatePeptidesImages(File imgsDirectory, Iterable<Coverage> coverages) {
    // Coverage images directory
    File coverageDir = new File(imgsDirectory.getPath() + "/peptides");
    if (!coverageDir.exists())
        coverageDir.mkdir();
    for (File f : coverageDir.listFiles()) f.delete();
    Map<Coverage, ColorsMap> covsColors = new HashMap<>();
    for (Coverage cov : coverages) {
        // File png = new File(coverageDir.getPath() + "/" + cov.getChemicalObject().getId() + ".png");
        String name = cov.getChemicalObject().getName().replaceAll("\\s", "_");
        File png = new File(coverageDir.getPath() + "/" + cov.getChemicalObject().getId() + "_" + name + ".png");
        ColorsMap colors = pg.createPNG(cov, png);
        covsColors.put(cov, colors);
    }
    return covsColors;
}
Also used : HashMap(java.util.HashMap) Coverage(algorithms.utils.Coverage) File(java.io.File) ColorsMap(io.imgs.PictureCoverageGenerator.ColorsMap)

Example 2 with Coverage

use of algorithms.utils.Coverage in project Smiles2Monomers by yoann-dufresne.

the class ChainsFamilyMatching method matchFamilly.

@Override
public Coverage matchFamilly(Family family) {
    if (this.co == null)
        return null;
    this.coverage = new Coverage(co);
    FamilyChainsDB fc = this.chains.getObject(family.getJsonName());
    this.toMatch.addAll(family.getRoots());
    while (!toMatch.isEmpty()) {
        Residue res = toMatch.remove();
        List<MappedChain> mcs = null;
        List<ChainAdd> adds = fc.getAdds(res);
        // If the residue is a root
        if (adds.size() == 0) {
            Chain rootChain = fc.getRootChains().get(res);
            mcs = Isomorphism.searchAChain(rootChain, this.co, this.matchingType);
        } else // From previous mapping
        {
            mcs = new ArrayList<>();
            Residue from = adds.get(0).getFrom();
            for (MappedChain mc : mappings.get(from)) {
                try {
                    MappedChain clone = mc;
                    for (ChainAdd add : adds) clone = add.applyAndClone(mc, this.matchingType);
                    mcs.add(clone);
                } catch (Exception e) {
                }
            }
        }
        // Save results and recursive add
        if (mcs.size() > 0) {
            this.mappings.put(res, mcs);
            this.addToCoverage(family, mcs, res);
            for (Residue child : fc.getFamily().getChildrenOf(res)) {
                if (fc.getAdds(child).get(0).getFrom().equals(res)) {
                    this.toMatch.add(child);
                }
            }
        }
    }
    this.mappings.clear();
    return this.coverage;
}
Also used : MappedChain(algorithms.isomorphism.chains.MappedChain) Chain(algorithms.isomorphism.chains.Chain) MappedChain(algorithms.isomorphism.chains.MappedChain) FamilyChainsDB(algorithms.isomorphism.chains.FamilyChainsDB) Residue(model.Residue) ChainAdd(algorithms.isomorphism.chains.ChainAdd) Coverage(algorithms.utils.Coverage)

Example 3 with Coverage

use of algorithms.utils.Coverage in project Smiles2Monomers by yoann-dufresne.

the class MonomericSpliting method computeCoverage.

/**
 * Calculate an object Coverage with all matches from families.
 * @param pep Peptide to match
 */
public void computeCoverage(Polymer pep) {
    this.coverage = new Coverage(pep);
    this.matcher.setChemicalObject(pep);
    Isomorphism.setMappingStorage(true);
    // Step 1 : Strict Matching
    if (verbose) {
        System.out.println("+Strict matching");
        System.out.println("++Search residues");
    }
    this.matchAllFamilies(MatchingType.STRONG);
    double ratio = this.coverage.getCoverageRatio();
    if (ratio < 1.0) {
        if (verbose)
            System.out.println("++Modulation");
        this.coverage = this.modulation.modulate(this.coverage);
    }
    Coverage save = this.coverage.clone();
    // conditions to go to light matching
    if (!this.allowLightMatchs || ratio == 1.0) {
        Isomorphism.setMappingStorage(false);
        return;
    }
    // TODO : Why if/else ?
    if (this.condition == null)
        this.condition = new ExtendsNTimes(this.retry);
    else
        this.condition.init();
    this.remover.init();
    // Successive matchings
    int depth = 0;
    while (this.coverage.getCoverageRatio() < 1.0 && this.condition.toContinue(this.coverage)) {
        depth++;
        // Contract the atomic graph to monomeric graph
        ContractedGraph contracted = new ContractedGraph(this.coverage);
        // Remove monomers from the current solution to try with other matching strategy
        this.remover.remove(this.coverage, contracted);
        // Create a masked molecule to only search on free polymer areas
        Molecule mol = DeepenMatcher.mask(this.coverage);
        this.coverage.setCurrentMaskedMol(mol);
        OtherChemicalObject tmp = new OtherChemicalObject(mol);
        this.matcher.setChemicalObject(tmp);
        if (verbose) {
            System.out.println("+Light matching, depth " + depth);
            System.out.println("++Search residues");
        }
        // Compute for all families with ligth matching
        this.matchAllFamilies(MatchingType.LIGHT);
        // Re-compute coverage
        this.coverage.calculateGreedyCoverage();
        if (this.coverage.getCoverageRatio() < 1.0) {
            if (verbose)
                System.out.println("++Modulation");
            this.coverage = this.modulation.modulate(this.coverage);
        }
        if (this.coverage.getCoverageRatio() > save.getCoverageRatio())
            save = this.coverage.clone();
        this.remover.nextLevel();
    }
    Isomorphism.setMappingStorage(false);
    if (save.getCoverageRatio() > this.coverage.getCoverageRatio())
        this.coverage = save;
}
Also used : Molecule(org.openscience.cdk.Molecule) ExtendsNTimes(algorithms.isomorphism.conditions.ExtendsNTimes) ContractedGraph(model.graph.ContractedGraph) Coverage(algorithms.utils.Coverage) OtherChemicalObject(model.OtherChemicalObject)

Example 4 with Coverage

use of algorithms.utils.Coverage in project Smiles2Monomers by yoann-dufresne.

the class MonomericSpliting method computeCoverages.

public Coverage[] computeCoverages(PolymersDB polDB) {
    Coverage[] covs = new Coverage[polDB.size()];
    int idx = 0;
    for (Polymer pol : polDB.getObjects()) {
        this.computeCoverage(pol);
        covs[idx] = this.getCoverage();
        idx += 1;
    }
    return covs;
}
Also used : Polymer(model.Polymer) Coverage(algorithms.utils.Coverage)

Example 5 with Coverage

use of algorithms.utils.Coverage in project Smiles2Monomers by yoann-dufresne.

the class Json2HTML method main.

public static void main(String[] args) {
    if (args.length < 6) {
        System.err.println("Command line :\n\tjava main.AtomicToMonomeric <monomersFile> <peptidesFile> <rulesFile> <residuesFile> <coveragesFile> <outType> [outFile]  [-v]");
        System.err.println("  outType can be \"-zip\" or \"-html\"");
        System.exit(42);
    }
    String monoDBname = args[0];
    String pepDBname = args[1];
    String rulesDBname = args[2];
    String residuesDBname = args[3];
    String covsFile = args[4];
    boolean zip = args[5].equals("-zip") ? true : false;
    String outFile = null;
    if (zip && args.length < 7) {
        System.err.println("Command line :\n\tjava main.AtomicToMonomeric <monomersFile> <peptidesFile> <rulesFile> <residuesFile> <coveragesFile> <outType> [outFile]  [-v]");
        System.err.println("  outType can be \"-zip\" or \"-html\"");
        System.exit(42);
    } else if (zip && args.length >= 7)
        outFile = args[6];
    // Loading databases
    System.out.println("--- Loading ---");
    // Maybe loading can be faster for the learning base, using serialized molecules instead of CDK SMILES parsing method.
    long loadingTime = System.currentTimeMillis();
    MonomersDB monoDB = new MonomersJsonLoader(false).loadFile(monoDBname);
    MonomersSerialization ms = new MonomersSerialization();
    ms.deserialize(monoDB, "data/serials/monos.serial");
    PolymersJsonLoader pcl = new PolymersJsonLoader(monoDB, true);
    PolymersDB pepDB = pcl.loadFile(pepDBname);
    RulesDB rules = RulesJsonLoader.loader.loadFile(rulesDBname);
    ResidueJsonLoader rjl = new ResidueJsonLoader(rules, monoDB);
    FamilyDB families = rjl.loadFile(residuesDBname);
    loadingTime = (System.currentTimeMillis() - loadingTime) / 1000;
    System.out.println("--- Json to HTML ---");
    long creationTime = System.currentTimeMillis();
    CoveragesJsonLoader cl = new CoveragesJsonLoader(pepDB, families);
    CoveragesDB covs = cl.loadFile(covsFile);
    List<Coverage> covsList = covs.getObjects();
    Collections.sort(covsList);
    // Common generations
    File imgs = new File("tmp_imgs_" + covsFile.substring(covsFile.lastIndexOf("/") + 1, covsFile.lastIndexOf(".")));
    if (!imgs.exists())
        imgs.mkdir();
    ImagesGeneration ig = new ImagesGeneration();
    Map<Coverage, ColorsMap> allColors = ig.generate(imgs, monoDB, covsList);
    if (!zip) {
        // HTML
        File resultDir = null;
        if (covsFile.contains("/"))
            resultDir = new File(covsFile.substring(0, covsFile.lastIndexOf("/")));
        else
            resultDir = new File(".");
        if (!resultDir.exists())
            resultDir.mkdir();
        Coverages2HTML c2h = new Coverages2HTML(covsList, monoDB, families);
        File htmlFile = new File(resultDir.getPath() + "/test.html");
        c2h.createResults(htmlFile, imgs, allColors);
    } else {
        // Zip File
        OutputZiper oz = new OutputZiper(outFile);
        oz.createZip(imgs.getPath(), covsFile, pepDBname, monoDBname, residuesDBname, allColors);
    }
    ig.cleanTmp(imgs);
    creationTime = (System.currentTimeMillis() - creationTime) / 1000;
    System.out.println();
    System.out.println("Total time to load datas : " + loadingTime + "s");
    System.out.println("Total time to create HTML : " + creationTime + "s");
    System.out.println();
    System.out.println("--- Program ended ---");
}
Also used : MonomersJsonLoader(io.loaders.json.MonomersJsonLoader) MonomersDB(db.MonomersDB) Coverage(algorithms.utils.Coverage) FamilyDB(db.FamilyDB) MonomersSerialization(io.loaders.serialization.MonomersSerialization) RulesDB(db.RulesDB) ImagesGeneration(io.imgs.ImagesGeneration) CoveragesDB(db.CoveragesDB) PolymersDB(db.PolymersDB) PolymersJsonLoader(io.loaders.json.PolymersJsonLoader) OutputZiper(io.zip.OutputZiper) CoveragesJsonLoader(io.loaders.json.CoveragesJsonLoader) Coverages2HTML(io.html.Coverages2HTML) ResidueJsonLoader(io.loaders.json.ResidueJsonLoader) File(java.io.File) ColorsMap(io.imgs.PictureCoverageGenerator.ColorsMap)

Aggregations

Coverage (algorithms.utils.Coverage)12 ColorsMap (io.imgs.PictureCoverageGenerator.ColorsMap)5 File (java.io.File)5 Residue (model.Residue)4 Match (algorithms.utils.Match)3 FamilyDB (db.FamilyDB)2 MonomersDB (db.MonomersDB)2 PolymersDB (db.PolymersDB)2 RulesDB (db.RulesDB)2 Coverages2HTML (io.html.Coverages2HTML)2 ImagesGeneration (io.imgs.ImagesGeneration)2 CoveragesJsonLoader (io.loaders.json.CoveragesJsonLoader)2 MonomersJsonLoader (io.loaders.json.MonomersJsonLoader)2 PolymersJsonLoader (io.loaders.json.PolymersJsonLoader)2 ResidueJsonLoader (io.loaders.json.ResidueJsonLoader)2 MonomersSerialization (io.loaders.serialization.MonomersSerialization)2 OutputZiper (io.zip.OutputZiper)2 HashMap (java.util.HashMap)2 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2