All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.uu.cs.ape.LocalRun Maven / Gradle / Ivy

Go to download

APE is a command line tool and an API for the automated exploration of possible computational pipelines (workflows) from large collections of computational tools.

The newest version!
package nl.uu.cs.ape;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

import guru.nidi.graphviz.engine.Format;
import lombok.extern.slf4j.Slf4j;
import nl.uu.cs.ape.configuration.APEConfigException;
import nl.uu.cs.ape.configuration.APECoreConfig;
import nl.uu.cs.ape.configuration.APERunConfig;
import nl.uu.cs.ape.domain.BioToolsAPI;
import nl.uu.cs.ape.utils.APEFiles;
import nl.uu.cs.ape.utils.APEUtils;
import nl.uu.cs.ape.solver.solutionStructure.SolutionsList;

/**
 * The entry point of application when the library is used in a Command Line
 * Interface (CLI).
 *
 * @author Vedran Kasalica
 */
@Slf4j
public class LocalRun {

    private static String localRepo = "/Users/vedran/git/APE_repo/APE_UseCases/";

    /**
     * The entry point of application when the library is used in a Command Line
     * Interface (CLI).
     *
     * @param args APE expects at most one (1) argument: The absolute or relative
     *             path to the configuration file.
     * @throws IOException
     * @throws APEConfigException
     * @throws JSONException
     * @throws OWLOntologyCreationException
     */
    public static void main(String[] args)
            throws JSONException, APEConfigException, IOException, OWLOntologyCreationException {

        exampleLocal("wombat");
        // runAPE("/Users/vedran/Desktop/test.json");
        // getWombatBioTools();
    }

    /**
     * Reads the BioTools API and saves the tools in APE format in the specified
     * file.
     * 
     * @throws IOException
     */
    private static void getWombatBioTools() throws IOException {
        String root = "/Users/vedran/git/Workflomics_repo/domain-annotations/WombatP_tools/";
        String toolNames = "tools.json";
        String annotationFile = "bio.tools_new.json";

        BioToolsAPI.fetchToolSet(root + toolNames, root + annotationFile);
    }

    private static void exampleURL(String domain) throws JSONException, APEConfigException, IOException {
        Map examples = new HashMap<>();
        examples.put("proteomics",
                "https://raw.githubusercontent.com/Workflomics/domain-annotations/main/MassSpectometry/config.json");

        runAPE(examples.get(domain));
    }

    private static void exampleLocal(String domain) throws JSONException, APEConfigException, IOException {
        Map examples = new HashMap<>();
        examples.put("proteomics",
                "/Users/vedran/git/APE_repo/APE_UseCases/MassSpectometry/No1/config_original.json");
        examples.put("gmt", "/Users/vedran/git/APE_repo/APE_UseCases/GeoGMT/E0/config.json");
        examples.put("tmp", "/Users/vedran/Desktop/steven_example/config.json");
        examples.put("wombat", "/Users/vedran/git/Workflomics_repo/domain-annotations/WombatP_tools/config.json");

        runAPE(examples.get(domain));
    }

    private static JSONObject setupLocalPaths(String pathToFile) throws IOException {
        File runConfigFile = APEFiles.readPathToFile(pathToFile);
        String runConfigString = FileUtils.readFileToString(runConfigFile, StandardCharsets.UTF_8);
        runConfigString = runConfigString.replace("./", localRepo);
        return new JSONObject(runConfigString);
    }

    /**
     * Runs the APE framework with the specified configuration file.
     * 
     * @param pathToFile The absolute or relative path to the configuration file.
     * @throws JSONException
     * @throws APEConfigException
     * @throws IOException
     */
    public static void runAPE(String pathToFile) throws JSONException, APEConfigException, IOException {
        JSONObject configurationJson = setupLocalPaths(pathToFile);

        int solutionsNo = 13;
        APECoreConfig coreConfig = new APECoreConfig(configurationJson);
        // if (!APEUtils.isValidReadFile(path)) {
        // log.error("Bad path.");
        // return;
        // }

        APE apeFramework = null;
        try {

            // set up the APE framework
            apeFramework = new APE(coreConfig);

        } catch (APEConfigException | JSONException | IOException | OWLOntologyCreationException e) {
            log.error("Error in setting up the APE framework:");
            log.error(e.getMessage());
            return;
        }

        SolutionsList solutions;
        try {

            APERunConfig runConfig = new APERunConfig(configurationJson, apeFramework.getDomainSetup());

            runConfig.setMaxNoSolutions(solutionsNo);
            runConfig.setNoGraphs(solutionsNo);
            runConfig.setNoCWL(solutionsNo);
            // runConfig.setSolutionLength(10, 10);
            // run the synthesis and retrieve the solutions
            solutions = apeFramework.runSynthesis(runConfig);

        } catch (APEConfigException e) {
            log.error("Error in synthesis execution. APE configuration error:");
            log.error(e.getMessage());
            return;
        } catch (JSONException e) {
            log.error("Error in synthesis execution. Bad JSON formatting (APE configuration or constriants JSON). ");
            log.error(e.getMessage());
            return;
        } catch (IOException e) {
            log.error("Error in synthesis execution.");
            log.error(e.getMessage());
            return;
        }

        /*
         * Writing solutions to the specified file in human readable format
         */
        if (solutions.isEmpty()) {
            log.info("UNSAT");
        } else {
            try {
                APE.writeSolutionToFile(solutions);

                // APE.writeDataFlowGraphs(solutions, RankDir.TOP_TO_BOTTOM);
                // APE.writeControlFlowGraphs(solutions, RankDir.LEFT_TO_RIGHT);
                APE.writeTavernaDesignGraphs(solutions, Format.PNG);
                APE.writeTavernaDesignGraphs(solutions, Format.SVG);
                // APE.writeExecutableWorkflows(solutions);
                APE.writeCWLWorkflows(solutions);
                // APE.writeExecutableCWLWorkflows(solutions, apeFramework.getConfig());
            } catch (IOException e) {
                log.error("Error in writing the solutions. to the file system.");
                e.printStackTrace();
            }
        }
    }

    private static APECoreConfig generateCoreConfig(String configFilePath) throws IOException, JSONException {

        File configFile = APEFiles.readPathToFile(configFilePath);

        JSONObject configJson = APEFiles.readFileToJSONObject(configFile);
        String ontologyURL = configJson.getString("ontology_path");
        File ontology = APEFiles.readPathToFile(ontologyURL);
        String ontologyPrefixIRI = configJson.getString("ontologyPrefixIRI");
        String toolTaxonomyRoot = configJson.getString("toolsTaxonomyRoot");
        List dataDimensionsTaxonomyRoots = APEUtils
                .getListFromJsonList(configJson.getJSONArray("dataDimensionsTaxonomyRoots"), String.class);
        String toolAnnotationsURL = configJson.getString("tool_annotations_path");
        File toolAnnotations = APEFiles.readPathToFile(toolAnnotationsURL);
        boolean strictToolAnnotations = configJson.getBoolean("strict_tool_annotations");

        APECoreConfig coreConfig = new APECoreConfig(ontology, ontologyPrefixIRI, toolTaxonomyRoot,
                dataDimensionsTaxonomyRoots, toolAnnotations, strictToolAnnotations);

        return coreConfig;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy