Beispiel #1
0
def run():
    """
    Run page
    """
    inforun = None
    inforun_file = os.path.join(config_reader.config_dir, ".inforun")
    if os.path.exists(inforun_file):
        try:
            with open(inforun_file, "r") as info:
                inforun = json.loads(info.read())
        except json.JSONDecodeError:
            print("Unable to parse inforun file. Ignoring it.",
                  file=sys.stderr)
            pass
    tools = Tools().tools
    tools_names = sorted(list(tools.keys()),
                         key=lambda x: (tools[x].order, tools[x].name))
    tools_ava = {}
    tools_options = {}
    for tool_name, tool in tools.items():
        tools_ava[tool_name] = 1 if tool.all_vs_all is not None else 0
        tools_options[tool_name] = tool.options
    if MODE == "webserver":
        with Session.connect():
            s_id = Session.new()
    else:
        upload_folder = Functions.random_string(20)
        tmp_dir = config_reader.upload_folder
        upload_folder_path = os.path.join(tmp_dir, upload_folder)
        while os.path.exists(upload_folder_path):
            upload_folder = Functions.random_string(20)
            upload_folder_path = os.path.join(tmp_dir, upload_folder)
        s_id = upload_folder
    id_job = Functions.random_string(
        5) + "_" + datetime.datetime.fromtimestamp(
            time.time()).strftime('%Y%m%d%H%M%S')
    if "id_job" in request.args:
        id_job = request.args["id_job"]
    email = ""
    if "email" in request.args:
        email = request.args["email"]
    return render_template(
        "run.html",
        id_job=id_job,
        email=email,
        menu="run",
        allowed_ext=ALLOWED_EXTENSIONS,
        s_id=s_id,
        max_upload_file_size=config_reader.max_upload_file_size,
        example=config_reader.example_target != "",
        target=os.path.basename(config_reader.example_target),
        query=os.path.basename(config_reader.example_query),
        tools_names=tools_names,
        tools=tools,
        tools_ava=tools_ava,
        tools_options=tools_options,
        version=VERSION,
        inforun=inforun)
Beispiel #2
0
def get_tools_options(tool_name, chosen_options):
    """
    Transform options chosen in javascript into parameters

    :return: True is chosen options are valid + a string containing optional parameters to use with tool
    :rtype: boolean, str
    """
    tools = Tools().tools
    if tool_name is None:
        return True, None
    tool = tools[tool_name] if tool_name in tools else None
    tool_options = tool.options if tool is not None else None
    # We filter options for the chosen tool.
    tool_prefix = "tool-options-%s-" % tool_name
    filtered_options = [
        s[len(tool_prefix):].split("-") for s in chosen_options
        if s.startswith(tool_prefix)
    ]
    valid = True
    options_params = None
    try:
        options_params = [
            tool_options[int(o)]['entries'][int(e)]['value']
            for o, e in filtered_options
        ]
    except KeyError:
        valid = False
    except IndexError:
        valid = False
    return True, " ".join(options_params)
Beispiel #3
0
def run():
    """
    Run page
    """
    tools = Tools().tools
    tools_names = sorted(list(tools.keys()),
                         key=lambda x: (tools[x].order, tools[x].name))
    tools_ava = {}
    for tool_name, tool in tools.items():
        tools_ava[tool_name] = 1 if tool.all_vs_all is not None else 0
    if MODE == "webserver":
        with Session.connect():
            s_id = Session.new()
    else:
        upload_folder = Functions.random_string(20)
        tmp_dir = config_reader.upload_folder
        upload_folder_path = os.path.join(tmp_dir, upload_folder)
        while os.path.exists(upload_folder_path):
            upload_folder = Functions.random_string(20)
            upload_folder_path = os.path.join(tmp_dir, upload_folder)
        s_id = upload_folder
    id_job = Functions.random_string(
        5) + "_" + datetime.datetime.fromtimestamp(
            time.time()).strftime('%Y%m%d%H%M%S')
    if "id_job" in request.args:
        id_job = request.args["id_job"]
    email = ""
    if "email" in request.args:
        email = request.args["email"]
    return render_template(
        "run.html",
        id_job=id_job,
        email=email,
        menu="run",
        allowed_ext=ALLOWED_EXTENSIONS,
        s_id=s_id,
        max_upload_file_size=config_reader.max_upload_file_size,
        example=config_reader.example_target != "",
        target=os.path.basename(config_reader.example_target),
        query=os.path.basename(config_reader.example_query),
        tools_names=tools_names,
        tools=tools,
        tools_ava=tools_ava,
        version=VERSION)
Beispiel #4
0
def launch_analysis():
    """
    Launch the job
    """
    if MODE == "webserver":
        try:
            with Session.connect():
                session = Session.get(s_id=request.form["s_id"])
        except DoesNotExist:
            return jsonify({
                "success":
                False,
                "errors":
                ["Session has expired. Please refresh the page and try again"]
            })
        upload_folder = session.upload_folder
        # Delete session:
        session.delete_instance()
    else:
        upload_folder = request.form["s_id"]

    id_job = request.form["id_job"]
    email = request.form["email"]
    file_query = request.form["query"]
    file_query_type = request.form["query_type"]
    file_target = request.form["target"]
    file_target_type = request.form["target_type"]
    tool = request.form["tool"] if "tool" in request.form else None
    alignfile = request.form[
        "alignfile"] if "alignfile" in request.form else None
    alignfile_type = request.form[
        "alignfile_type"] if "alignfile_type" in request.form else None
    backup = request.form["backup"] if "backup" in request.form else None
    backup_type = request.form[
        "backup_type"] if "backup_type" in request.form else None

    # Check form:
    form_pass = True
    errors = []

    if alignfile is not None and alignfile_type is None:
        errors.append(
            "Server error: no alignfile_type in form. Please contact the support"
        )
        form_pass = False

    if backup is not None and backup != "" and (backup_type is None
                                                or backup_type == ""):
        errors.append(
            "Server error: no backup_type in form. Please contact the support")
        form_pass = False

    if backup is not None and backup != "":
        alignfile = ""
        file_query = ""
        file_target = ""
    else:
        backup = None
        if file_target == "":
            errors.append("No target fasta selected")
            form_pass = False

    if tool is not None and tool not in Tools().tools:
        errors.append("Tool unavailable: %s" % tool)
        form_pass = False

    if id_job == "":
        errors.append("Id of job not given")
        form_pass = False

    if MODE == "webserver":
        if email == "":
            errors.append("Email not given")
            form_pass = False
        elif not re.match(r"^[\w.\-]+@[\w\-.]{2,}\.[a-z]{2,4}$", email):
            errors.append("Email is invalid")
            form_pass = False

    # Form pass
    if form_pass:
        # Get final job id:
        id_job = re.sub('[^A-Za-z0-9_\-]+', '', id_job.replace(" ", "_"))
        id_job_orig = id_job
        i = 2
        while os.path.exists(os.path.join(APP_DATA, id_job)):
            id_job = id_job_orig + ("_%d" % i)
            i += 1

        folder_files = os.path.join(APP_DATA, id_job)
        os.makedirs(folder_files)

        # Save files:
        query = None
        if file_query != "":
            example = False
            if file_query.startswith("example://"):
                example = True
                query_path = config_reader.example_query
                query_name = os.path.basename(query_path)
                file_query_type = "local"
            else:
                query_name = os.path.splitext(file_query.replace(
                    ".gz", ""))[0] if file_query_type == "local" else None
                if file_query_type == "local":
                    query_path = os.path.join(app.config["UPLOAD_FOLDER"],
                                              upload_folder, file_query)
                    if os.path.exists(query_path):
                        if " " in file_query:
                            new_query_path = os.path.join(
                                app.config["UPLOAD_FOLDER"], upload_folder,
                                file_query.replace(" ", "_"))
                            shutil.move(query_path, new_query_path)
                            query_path = new_query_path
                    else:
                        errors.append("Query file not correct!")
                        form_pass = False
                else:
                    query_path = file_query
            query = Fasta(name=query_name,
                          path=query_path,
                          type_f=file_query_type,
                          example=example)
        example = False
        target = None
        if file_target != "":
            if file_target.startswith("example://"):
                example = True
                target_path = config_reader.example_target
                target_name = os.path.basename(target_path)
                file_target_type = "local"
            else:
                target_name = os.path.splitext(file_target.replace(".gz", ""))[0] if file_target_type == "local" \
                    else None
                if file_target_type == "local":
                    target_path = os.path.join(app.config["UPLOAD_FOLDER"],
                                               upload_folder, file_target)
                    if os.path.exists(target_path):
                        if " " in target_path:
                            new_target_path = os.path.join(
                                app.config["UPLOAD_FOLDER"], upload_folder,
                                file_target.replace(" ", "_"))
                            shutil.move(target_path, new_target_path)
                            target_path = new_target_path
                    else:
                        errors.append("Target file not correct!")
                        form_pass = False
                else:
                    target_path = file_target
            target = Fasta(name=target_name,
                           path=target_path,
                           type_f=file_target_type,
                           example=example)

        if alignfile is not None and alignfile != "" and backup is not None:
            Path(os.path.join(folder_files, ".align")).touch()

        align = None
        if alignfile is not None and alignfile != "":
            alignfile_name = os.path.splitext(
                alignfile)[0] if alignfile_type == "local" else None
            alignfile_path = os.path.join(app.config["UPLOAD_FOLDER"], upload_folder, alignfile) \
                if alignfile_type == "local" else alignfile
            if alignfile_type == "local" and not os.path.exists(
                    alignfile_path):
                errors.append("Alignment file not correct!")
                form_pass = False
            align = Fasta(name=alignfile_name,
                          path=alignfile_path,
                          type_f=alignfile_type)

        bckp = None
        if backup is not None:
            backup_name = os.path.splitext(
                backup)[0] if backup_type == "local" else None
            backup_path = os.path.join(app.config["UPLOAD_FOLDER"], upload_folder, backup) \
                if backup_type == "local" else backup
            if backup_type == "local" and not os.path.exists(backup_path):
                errors.append("Backup file not correct!")
                form_pass = False
            bckp = Fasta(name=backup_name,
                         path=backup_path,
                         type_f=backup_type)

        if form_pass:
            # Launch job:
            job = JobManager(id_job=id_job,
                             email=email,
                             query=query,
                             target=target,
                             align=align,
                             backup=bckp,
                             mailer=mailer,
                             tool=tool)
            if MODE == "webserver":
                job.launch()
            else:
                job.launch_standalone()
            return jsonify({
                "success": True,
                "redirect": url_for(".status", id_job=id_job)
            })
    if not form_pass:
        return jsonify({"success": False, "errors": errors})