Search in sources :

Example 6 with Coverage

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

the class ProcessPolymers method main.

public static void main(String[] args) {
    // ----------------- Parameters ---------------------------
    String monoDBname = "data/monomers.json";
    String pepDBname = "data/polymers.json";
    String rulesDBname = "data/rules.json";
    String residuesDBname = "data/residues.json";
    String chainsDBFile = "data/chains.json";
    String outfile = "results/coverages.json";
    String outfolderName = "results/";
    String imgsFoldername = "images/";
    boolean html = false;
    boolean zip = false;
    String serialFolder = "data/serials/";
    boolean lightMatch = true;
    boolean verbose = false;
    int removeDistance = 2;
    int retryCount = 2;
    int modulationDepth = 2;
    // Parsing
    loop: for (int idx = 0; idx < args.length; idx++) {
        if (args[idx].startsWith("-")) {
            switch(args[idx]) {
                case "-rul":
                    rulesDBname = args[idx + 1];
                    break;
                case "-mono":
                    monoDBname = args[idx + 1];
                    break;
                case "-poly":
                    pepDBname = args[idx + 1];
                    break;
                case "-res":
                    residuesDBname = args[idx + 1];
                    break;
                case "-cha":
                    chainsDBFile = args[idx + 1];
                    break;
                case "-serial":
                    serialFolder = args[idx + 1];
                    break;
                case "-outfile":
                    outfile = args[idx + 1];
                    break;
                case "-outfolder":
                    outfolderName = args[idx + 1];
                    break;
                case "-imgs":
                    imgsFoldername = args[idx + 1];
                    break;
                case "-strict":
                    lightMatch = false;
                    continue loop;
                case "-v":
                    verbose = true;
                    continue loop;
                case "-html":
                    html = true;
                    continue loop;
                case "-zip":
                    zip = true;
                    continue loop;
                default:
                    System.err.println("Wrong option " + args[idx]);
                    System.exit(1);
                    break;
            }
            idx++;
        } else {
            System.err.println("Wrong parameter " + args[idx]);
            System.exit(1);
        }
    }
    // ------------------- Loadings ------------------------
    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, serialFolder + "monos.serial");
    boolean d2 = html || zip;
    PolymersJsonLoader pjl = new PolymersJsonLoader(monoDB, d2);
    PolymersDB polDB = pjl.loadFile(pepDBname);
    RulesDB rulesDB = RulesJsonLoader.loader.loadFile(rulesDBname);
    ResidueJsonLoader rjl = new ResidueJsonLoader(rulesDB, monoDB);
    // Need optimizations
    FamilyDB families = rjl.loadFile(residuesDBname);
    FamilyChainIO fcio = new FamilyChainIO(families);
    ChainsDB chains = fcio.loadFile(chainsDBFile);
    loadingTime = System.currentTimeMillis() - loadingTime;
    System.out.println("Loading time : " + (loadingTime / 1000) + "s");
    // ------------------- Spliting ------------------------
    System.out.println("--- Monomers search ---");
    long searchTime = System.currentTimeMillis();
    MonomericSpliting.setVerbose(verbose);
    MonomericSpliting split = new MonomericSpliting(families, chains, removeDistance, retryCount, modulationDepth);
    split.setAllowLightMatchs(lightMatch);
    Coverage[] covs = split.computeCoverages(polDB);
    searchTime = System.currentTimeMillis() - searchTime;
    System.out.println("Search time : " + (searchTime / 1000) + "s");
    // ------------------- Output ------------------------
    System.out.println("--- Output creations ---");
    long outputTime = System.currentTimeMillis();
    // Creation of the out directory
    File outfolder = new File(outfolderName);
    if (!outfolder.exists())
        outfolder.mkdir();
    CoveragesJsonLoader cjl = new CoveragesJsonLoader(polDB, families);
    cjl.saveFile(covs, outfile);
    // Images generation
    if (html || zip) {
        File imgsFolder = new File(imgsFoldername);
        if (!imgsFolder.exists())
            imgsFolder.mkdir();
        ImagesGeneration ig = new ImagesGeneration();
        ig.generateMonomerImages(imgsFolder, monoDB);
        Map<Coverage, ColorsMap> colors = ig.generatePeptidesImages(imgsFolder, covs);
        if (html) {
            // HTML
            Coverages2HTML c2h = new Coverages2HTML(covs, monoDB, families);
            File htmlFile = new File(outfolderName + "/s2m.html");
            c2h.createResults(htmlFile, imgsFolder, colors);
        }
        if (zip) {
            // Zip File
            OutputZiper oz = new OutputZiper(outfolderName + "/s2m.zip");
            oz.createZip(imgsFolder.getPath(), outfile, pepDBname, monoDBname, residuesDBname, colors);
        }
    }
    outputTime = System.currentTimeMillis() - outputTime;
    System.out.println("Ouputing time : " + (outputTime / 1000) + "s");
    System.out.println("--- Ended ---");
}
Also used : MonomericSpliting(algorithms.MonomericSpliting) MonomersJsonLoader(io.loaders.json.MonomersJsonLoader) MonomersDB(db.MonomersDB) ChainsDB(algorithms.isomorphism.chains.ChainsDB) Coverage(algorithms.utils.Coverage) FamilyDB(db.FamilyDB) MonomersSerialization(io.loaders.serialization.MonomersSerialization) RulesDB(db.RulesDB) FamilyChainIO(io.loaders.json.FamilyChainIO) ImagesGeneration(io.imgs.ImagesGeneration) 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)

Example 7 with Coverage

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

the class OutputZiper method createSupplementaries.

@SuppressWarnings("unchecked")
private void createSupplementaries(String path, Map<Coverage, ColorsMap> allColors) {
    File sup = new File(path);
    if (sup.exists())
        sup.delete();
    JSONArray array = new JSONArray();
    for (Coverage cov : allColors.keySet()) {
        JSONObject jso = new JSONObject();
        jso.put("id", cov.getId());
        jso.put("ratio", cov.getCoverageRatio());
        ColorsMap cm = allColors.get(cov);
        JSONArray colorsArray = new JSONArray();
        jso.put("colors", colorsArray);
        for (Residue res : cm.keySet()) {
            JSONObject resObj = new JSONObject();
            JSONArray resColors = new JSONArray();
            for (Color col : cm.get(res)) {
                resColors.add(Integer.toHexString(col.getRGB()));
            }
            resObj.put(res.getId(), resColors);
            colorsArray.add(resObj);
        }
        array.add(jso);
    }
    try {
        FileWriter fw = new FileWriter(sup);
        fw.write(array.toJSONString());
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.simple.JSONObject) Residue(model.Residue) Color(java.awt.Color) FileWriter(java.io.FileWriter) JSONArray(org.json.simple.JSONArray) Coverage(algorithms.utils.Coverage) IOException(java.io.IOException) File(java.io.File) ColorsMap(io.imgs.PictureCoverageGenerator.ColorsMap)

Example 8 with Coverage

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

the class ImagesGeneration method generatePeptidesImages.

public Map<Coverage, ColorsMap> generatePeptidesImages(File imgsFolder, Coverage[] covs) {
    // Coverage images directory
    File coverageDir = new File(imgsFolder.getPath() + "/peptides");
    if (!coverageDir.exists())
        coverageDir.mkdir();
    for (File f : coverageDir.listFiles()) f.delete();
    Map<Coverage, ColorsMap> covsColors = new HashMap<>();
    for (Coverage cov : covs) {
        // 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 9 with Coverage

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

the class CoveragesJsonLoader method objectFromJson.

@Override
protected Coverage objectFromJson(JSONObject obj) {
    // TODO : Create new parser with the files created by the new output functions
    System.err.println("From classe '" + CoveragesJsonLoader.class.getName() + "' method 'objectFromJson':");
    System.err.println("This is deprecated ! You will have some problems with recent json files !");
    ChemicalObject co = null;
    try {
        co = this.db.getObject("" + ((Number) obj.get("peptide")).intValue());
    } catch (NullPointerException e) {
        System.err.println(e.getMessage());
        System.err.println("Maybe coverage json and molecule json don't match");
        System.exit(2);
    }
    Coverage cov = new Coverage(co);
    JSONArray array = (JSONArray) obj.get("matches");
    for (Object o : array) {
        JSONObject jso = (JSONObject) o;
        Residue res = null;
        try {
            res = this.residues.getObject("" + ((Number) jso.get("residue")).intValue());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        JSONObject jMatch = (JSONObject) jso.get("match");
        Match match = new Match(res);
        JSONArray atoms = (JSONArray) jMatch.get("atoms");
        for (Object atObj : atoms) {
            JSONObject atom = (JSONObject) atObj;
            int idx = ((Number) atom.get("a")).intValue();
            match.addAtom(idx);
            match.addHydrogens(idx, ((Number) atom.get("h")).intValue());
        }
        JSONArray bonds = (JSONArray) jMatch.get("bonds");
        for (Object boObj : bonds) {
            int bond = ((Number) boObj).intValue();
            match.addBond(bond);
        }
        cov.addMatch(match);
    }
    cov.calculateGreedyCoverage();
    return cov;
}
Also used : JSONObject(org.json.simple.JSONObject) Residue(model.Residue) JSONArray(org.json.simple.JSONArray) ChemicalObject(model.ChemicalObject) Coverage(algorithms.utils.Coverage) JSONObject(org.json.simple.JSONObject) ChemicalObject(model.ChemicalObject) Match(algorithms.utils.Match)

Example 10 with Coverage

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

the class CoveragesDB method addObject.

@Override
public void addObject(String id, Coverage o) {
    if (!this.database.containsKey(id))
        super.addObject(id, o);
    else {
        Coverage prev = this.database.get(id);
        prev.addMatches(o);
    }
}
Also used : Coverage(algorithms.utils.Coverage)

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