Esempio n. 1
0
def test_repeat():
    # Build up inputs for cat1
    as_dict = inputs().set("input1", dataset("encoded1")).set(
        "queries",
        repeat().instance(inputs().set_dataset_param(
            "input2", "encoded2")).instance(inputs().set_dataset_param(
                "input2", "encoded3"))).to_dict()
    assert as_dict["input1"]["src"] == "hda"
    assert as_dict["input1"]["id"] == "encoded1"
    assert as_dict["queries_0|input2"]["src"] == "hda"
    assert as_dict["queries_0|input2"]["id"] == "encoded2"
    assert as_dict["queries_1|input2"]["src"] == "hda"
    assert as_dict["queries_1|input2"]["id"] == "encoded3"
Esempio n. 2
0
 def test_run_cat1(self):
     history_id = self.gi.histories.create_history(
         name="test_run_cat1 history")["id"]
     dataset1_id = self._test_dataset(history_id, contents="1 2 3")
     dataset2_id = self._test_dataset(history_id, contents="4 5 6")
     dataset3_id = self._test_dataset(history_id, contents="7 8 9")
     tool_inputs = inputs().set("input1", dataset(dataset1_id)).set(
         "queries",
         repeat().instance(inputs().set(
             "input2", dataset(dataset2_id))).instance(inputs().set(
                 "input2", dataset(dataset3_id))))
     tool_output = self.gi.tools.run_tool(history_id=history_id,
                                          tool_id="cat1",
                                          tool_inputs=tool_inputs)
     self.assertEqual(len(tool_output["outputs"]), 1)
Esempio n. 3
0
def fetch_sequences_and_get_dataset_id(dataset, gi, logger):
    start = time.time()
    logger.info(
        f"Fetching DNA sequences from {colored(dataset.genome_build, 'blue')} in {colored('fasta', 'blue')} format..."
    )
    dataset_name = dataset.name
    dataset_id = dataset.id
    hist_id = dataset.container_id
    tool_id = 'Extract genomic DNA 1'
    tool_input = inputs().set_dataset_param(dataset_name,
                                            dataset_id,
                                            src="hda")
    job = gi.tools.run_tool(history_id=hist_id,
                            tool_id=tool_id,
                            tool_inputs=tool_input)
    job_id = job['jobs'][0]['id']
    dataset_id = job['outputs'][0]['id']
    while True:
        job_state = gi.jobs.get_state(job_id)
        if job_state == 'ok':
            break
        time.sleep(30)
    end = time.time()
    job_time = f'{str(datetime.timedelta(seconds=int(end - start)))}'
    logger.info(f"Extraction completed in {colored(job_time, 'blue')}")
    return dataset_id
Esempio n. 4
0
def test_repeat():
    # Build up inputs for cat1
    as_dict = inputs().set(
        "input1", dataset("encoded1")
    ).set(
        "queries", repeat().instance(
            inputs().set_dataset_param("input2", "encoded2")
        ).instance(
            inputs().set_dataset_param("input2", "encoded3")
        )
    ).to_dict()
    assert as_dict["input1"]["src"] == "hda"
    assert as_dict["input1"]["id"] == "encoded1"
    assert as_dict["queries_0|input2"]["src"] == "hda"
    assert as_dict["queries_0|input2"]["id"] == "encoded2"
    assert as_dict["queries_1|input2"]["src"] == "hda"
    assert as_dict["queries_1|input2"]["id"] == "encoded3"
Esempio n. 5
0
 def test_run_cat1(self):
     history_id = self.gi.histories.create_history(name="test_run_cat1 history")["id"]
     dataset1_id = self._test_dataset(history_id, contents="1 2 3")
     dataset2_id = self._test_dataset(history_id, contents="4 5 6")
     dataset3_id = self._test_dataset(history_id, contents="7 8 9")
     tool_inputs = (
         inputs()
         .set("input1", dataset(dataset1_id))
         .set(
             "queries",
             repeat()
             .instance(inputs().set("input2", dataset(dataset2_id)))
             .instance(inputs().set("input2", dataset(dataset3_id))),
         )
     )
     tool_output = self.gi.tools.run_tool(history_id=history_id, tool_id="cat1", tool_inputs=tool_inputs)
     self.assertEqual(len(tool_output["outputs"]), 1)
Esempio n. 6
0
 def test_wait_for_job(self):
     tool_inputs = inputs().set("input1", dataset(self.dataset_id))
     tool_output = self.gi.tools.run_tool(history_id=self.history_id,
                                          tool_id="cat1",
                                          tool_inputs=tool_inputs)
     job_id = tool_output['jobs'][0]['id']
     job = self.gi.jobs.wait_for_job(job_id)
     self.assertEqual(job['state'], 'ok')
Esempio n. 7
0
def test_conditional():
    # Build up example inputs for random_lines1
    as_dict = inputs().set("num_lines", 5).set(
        "input", dataset("encoded1")).set(
            "seed_source",
            conditional().set("seed_source_selector",
                              "set_seed").set("seed", "asdf")).to_dict()
    assert as_dict["num_lines"] == 5
    assert as_dict["input"]["src"] == "hda"
    assert as_dict["input"]["id"] == "encoded1"
    assert as_dict["seed_source|seed_source_selector"] == "set_seed"
    assert as_dict["seed_source|seed"] == "asdf"
Esempio n. 8
0
def export_output(request, project, gi, file_id, tool_id="export_sbw"):
    """Export data in galaxy MetaGenSense output directory"""
    """add the name of project on file name"""

    data = gi.datasets.show_dataset(dataset_id=file_id)
    hist_id = data['history_id']

    myinputs = inputs().set("prefix", project).set("input", dataset(file_id))

    tool_outputs = gi.tools.run_tool(history_id=hist_id, tool_id=tool_id, tool_inputs=myinputs)

    if tool_outputs:
        messages.warning(request, "Export is in progress, your file will be available: " + project + '_' + data['name'])
    return redirect('galaxy_history_detail', history_id=hist_id)
Esempio n. 9
0
 def test_run_random_lines(self):
     # Run second test case from randomlines.xml
     history_id = self.gi.histories.create_history(name="test_run_random_lines history")["id"]
     with open(get_abspath(os.path.join("data", "1.bed"))) as f:
         contents = f.read()
     dataset_id = self._test_dataset(history_id, contents=contents)
     tool_inputs = (
         inputs()
         .set("num_lines", "1")
         .set("input", dataset(dataset_id))
         .set("seed_source", conditional().set("seed_source_selector", "set_seed").set("seed", "asdf"))
     )
     tool_output = self.gi.tools.run_tool(history_id=history_id, tool_id="random_lines1", tool_inputs=tool_inputs)
     self.assertEqual(len(tool_output["outputs"]), 1)
Esempio n. 10
0
 def test_run_random_lines(self):
     # Run second test case from randomlines.xml
     history_id = self.gi.histories.create_history(
         name="test_run_random_lines history")["id"]
     with open(test_util.get_abspath(os.path.join("data", "1.bed"))) as f:
         contents = f.read()
     dataset_id = self._test_dataset(history_id, contents=contents)
     tool_inputs = inputs().set("num_lines", "1").set(
         "input", dataset(dataset_id)).set(
             "seed_source",
             conditional().set("seed_source_selector",
                               "set_seed").set("seed", "asdf"))
     tool_output = self.gi.tools.run_tool(history_id=history_id,
                                          tool_id="random_lines1",
                                          tool_inputs=tool_inputs)
     self.assertEqual(len(tool_output["outputs"]), 1)
Esempio n. 11
0
def test_conditional():
    # Build up example inputs for random_lines1
    as_dict = inputs().set(
        "num_lines", 5
    ).set(
        "input", dataset("encoded1")
    ).set(
        "seed_source", conditional().set(
            "seed_source_selector", "set_seed"
        ).set(
            "seed", "asdf"
        )
    ).to_dict()
    assert as_dict["num_lines"] == 5
    assert as_dict["input"]["src"] == "hda"
    assert as_dict["input"]["id"] == "encoded1"
    assert as_dict["seed_source|seed_source_selector"] == "set_seed"
    assert as_dict["seed_source|seed"] == "asdf"
Esempio n. 12
0
def tool_exec_view(request, pk, store_output=None):
    """
    :param request:
    :param pk:
    :param store_output:
    :return:
    """

    gi = request.galaxy
    message = ""

    tool_obj = get_object_or_404(Tool, pk=pk)

    toolfield, created = ToolFieldWhiteList.objects.get_or_create(
        tool=tool_obj, context='t')
    tool_visible_field = toolfield.saved_params

    tool_inputs_details = gi.tools.show_tool(tool_id=tool_obj.id_galaxy,
                                             io_details='true')
    tool_form = ToolForm(tool_params=tool_inputs_details['inputs'],
                         tool_id=pk,
                         whitelist=tool_visible_field,
                         data=request.POST or None,
                         session_files=request.session.get('files'))

    hid = {}
    if request.method == 'POST':
        if tool_form.is_valid():
            try:
                wksph = None
                tool_inputs = inputs()

                # mapping between form id to galaxy params names
                fields = tool_form.fields_ids_mapping
                exts = tool_form.fields_ext_mapping

                inputs_data = set(tool_form.input_file_ids)

                for key, value in request.POST.items():
                    if not key in inputs_data:
                        tool_inputs.set_param(fields.get(key), value)

                for inputfile in inputs_data:
                    outputs = ""
                    uploaded_file = ""
                    if request.FILES:
                        uploaded_file = request.FILES.get(inputfile, '')
                    if uploaded_file:
                        tmp_file = tempfile.NamedTemporaryFile()
                        for chunk in uploaded_file.chunks():
                            tmp_file.write(chunk)
                        tmp_file.flush()
                        # send file to galaxy after verifying the
                        # allowed extensions
                        type = detect_type(tmp_file.name)
                        if type in [
                                "fasta", "phylip"
                        ] and nb_sequences(tmp_file.name, type) <= 3:
                            raise ValueError(
                                'Sequence file %s should contain more than 3 sequences for field %s'
                                % (uploaded_file.name, fields.get(inputfile)))
                        if type in exts.get(inputfile, ""):
                            if wksph is None:
                                wksph = create_history(request,
                                                       name="Analyse with " +
                                                       tool_obj.name)
                            outputs = gi.tools.upload_file(
                                path=tmp_file.name,
                                file_name=uploaded_file.name,
                                history_id=wksph.history,
                                file_type=type)
                        else:
                            raise ValueError(
                                'File format of file %s is not allowed for field %s'
                                % (uploaded_file.name, fields.get(inputfile)))
                    else:
                        # else paste content
                        content = request.POST.get(inputfile)
                        galaxyfile = request.POST.get("galaxyfile_" +
                                                      inputfile)
                        if content:
                            tmp_file = tempfile.NamedTemporaryFile()
                            tmp_file.write(content)
                            tmp_file.flush()
                            # send file to galaxy after verifying the
                            # allowed extensions
                            input_fieldname = fields.get(inputfile)
                            type = detect_type(tmp_file.name)
                            if type in exts.get(inputfile, ""):
                                if wksph is None:
                                    wksph = create_history(
                                        request,
                                        name="Analyse with " + tool_obj.name)
                                outputs = gi.tools.upload_file(
                                    path=tmp_file.name,
                                    file_name=input_fieldname +
                                    " pasted_sequence",
                                    history_id=wksph.history,
                                    file_type=type)
                            else:
                                raise ValueError(
                                    'This file format is not allowed for field %s'
                                    % (input_fieldname))
                        # Else we look at galaxy file ids
                        else:
                            if galaxyfile:
                                file_id = galaxyfile
                                hid[inputfile] = file_id

                    if outputs:
                        file_id = outputs.get('outputs')[0].get('id')
                        hid[inputfile] = file_id

                for h, file_id in hid.items():
                    tool_inputs.set_dataset_param(fields.get(h), file_id)

                tool_outputs = gi.tools.run_tool(history_id=wksph.history,
                                                 tool_id=tool_obj.id_galaxy,
                                                 tool_inputs=tool_inputs)
                # Start monitoring (for sending emails)
                monitorworkspace.delay(wksph.history)
                wksph.monitored = True
                wksph.save()

                if store_output:
                    request.session['output'] = tool_outputs

                return redirect(
                    reverse_lazy("history_detail",
                                 kwargs={'history_id': wksph.history}))

            except ValueError as ve:
                message = str(ve)
            except ConnectionError as ce:
                message = str(ce)
            except NameError as ne:
                message = str(ne)
            except OperationalError as oe:
                message = str(oe)
            except AttributeError as ae:
                message = str(ae)
            except TypeError as te:
                message = str(te)
            except Exception as e:
                raw_message = ast.literal_eval(e.body)
                reverse_dict_field = {
                    v: k
                    for k, v in tool_form.fields_ids_mapping.items()
                }
                err_data = raw_message.get('err_data')

                if err_data:
                    for field, err_msg in err_data.items():
                        tool_form.add_error(
                            reverse_dict_field.get(field),
                            ValidationError(err_msg, code='invalid'))
                else:
                    message = raw_message

                delete_history(request, wksph.history)

    context = {"toolform": tool_form, "tool": tool_obj, "message": message}

    return render(request, 'tools/tool_form.html', context)
Esempio n. 13
0
def launch_pasteur_blast(blastrunid, sequence, prog, db, evalue, coverage, maxseqs):
    """
    Celery task that will launch a blast on the pasteur Galaxy Server
    """
    logging.info("Blasting %s with %s on %s" % (sequence, prog, db))
    b = BlastRun.objects.get(id=blastrunid)
    try:
        fasta_io = StringIO(sequence)
        records = list(SeqIO.parse(fasta_io, "fasta"))
        if len(records) == 1:
            galaxycon = galaxy_connection()
            galaxycon.nocache = True
            history = galaxycon.histories.create_history(name="BlastXplorer")
            
            b.history = history.get("id")
            b.query_id = biofile.cleanseqname(records[0].id)
            b.query_seq = records[0].seq
            b.evalue = evalue
            b.coverage = coverage
            b.database = db
            b.blastprog = prog
            b.maxseqs = maxseqs
            b.status = BlastRun.PENDING
            b.save()

            blast_type = BlastRun.blast_type(BlastRun.PASTEUR, prog)
            blast_inputtype = BlastRun.blast_inputtype(BlastRun.PASTEUR, prog)
            
            # We check alphabet of given sequence
            if ((blast_inputtype == "nt" and not biofile.check_nt(b.query_seq)) or
                (blast_inputtype == "aa" and not biofile.check_aa(b.query_seq))):
                b.status = BlastRun.ERROR
                b.message = "The given sequence has the wrong alphabet. Program %s expects %s sequence" % (
                    blast_type, blast_inputtype)
            elif blast_type is not None:
                tmp_file = tempfile.NamedTemporaryFile()
                tmp_file.write(sequence)
                tmp_file.flush()
                if biofile.is_fasta_one_seq(tmp_file.name):
                    ## Upload input query file to galaxy
	            outputs = galaxycon.tools.upload_file(path=tmp_file.name,file_name="blastinput.fasta",history_id=history.get("id"),file_type="fasta")
	            file_id = outputs.get('outputs')[0].get('id')
                    ## Configuring job
	            tool_inputs=inputs()
	            tool_inputs.set_dataset_param("query",file_id)
	            tool_inputs.set_param("db_opts|database", db)
	            tool_inputs.set_param("blast_type", blast_type)
	            tool_inputs.set_param("evalue_cutoff", evalue)
	            tool_inputs.set_param("output|out_format", "5")
                    ## Running blast job
	            outputs=galaxycon.tools.run_tool(history_id=history.get("id"),tool_id=prog,tool_inputs=tool_inputs)
                    b.history_fileid = outputs.get("outputs")[0].get("id")
                else:
                    b.status=BlastRun.ERROR
                    b.message="Bad input FASTA file format"
            else:
                b.status=BlastRun.ERROR
                b.message="Wrong blast program %s" % (prog)
            b.save()
        else:
            b.status = BlastRun.ERROR
            b.message = "More than one record in the fasta file! %d" % (
                len(list(records)))
    except Exception as e:
        logging.exception(str(e))
        b.status = BlastRun.ERROR
        b.message = str(e)
    b.save()
    time.sleep(30)
Esempio n. 14
0
 def analyze_forms(self, request, context, workflow, params, gi, wksph, nseq, length, seqaa):
     steps = workflow.json['steps']
     step_id = u'0'
     for tool_form in context['form_list']:
         if not tool_form.is_valid():
             raise WorkflowInvalidFormError(
                 "One form is invalid %s " % (tool_form.prefix))
         tid = getattr(tool_form, 'tool_id', 'null')
         t=Tool.objects.get(id_galaxy=tid)
         tool_inputs = inputs()
         # mapping between form id to galaxy params names
         fields = tool_form.fields_ids_mapping
         inputs_data = set(tool_form.input_file_ids)
         # set the Galaxy parameter (name, value)
         nboot = 0
         boot = False
         for key, value in tool_form.cleaned_data.items():
             if key not in inputs_data:
                 if fields.get(key,"") == 'bootstrap|replicates':
                     nboot = value
                 if fields.get(key,"") == 'bootstrap|do_bootstrap' and value == 'true':
                     boot = True
                 tool_inputs.set_param(fields.get(key), value)
         if not boot:
             nboot = 0
         if not t.can_run_on_data(nseq, length, nboot, seqaa):
             raise WorkflowInputFileFormatError(
                 "Input data is too large for the workflow"
             )
         for inputfile in inputs_data:
             uploaded_file = ""
             if request.FILES:
                 uploaded_file = request.FILES.get(inputfile, '')
             if uploaded_file:
                 tmp_file = tempfile.NamedTemporaryFile()
                 for chunk in uploaded_file.chunks():
                     tmp_file.write(chunk)
                 tmp_file.flush()
                 # send file to galaxy
                 outputs = gi.tools.upload_file(
                     path=tmp_file.name,
                     file_name=uploaded_file.name,
                     history_id=wksph.history)
                 file_id = outputs.get('outputs')[0].get('id')
                 tool_inputs.set_dataset_param(
                     fields.get(inputfile), file_id)
             else:
                 # else paste content
                 content = tool_form.cleaned_data.get(inputfile)
                 if content:
                     tmp_file = tempfile.NamedTemporaryFile()
                     tmp_file.write(content)
                     tmp_file.flush()
                     # send file to galaxy
                     input_fieldname = tool_form.fields_ids_mapping.get(
                         inputfile)
                     outputs = gi.tools.upload_file(
                         path=tmp_file.name,
                         file_name=input_fieldname + " pasted_sequence",
                         history_id=wksph.history)
                     file_id = outputs.get('outputs')[0].get('id')
                     tool_inputs.set_dataset_param(fields.get(inputfile),
                                                   file_id)
         # workflow step
         # get from which step the tools are used
         for i, step in steps.items():
             if (getattr(tool_form, 'tool_id', 'null') ==
                     step.get('tool_id')):
                 step_id = i
                 break
         # convert inputs to dict
         params[step_id] = tool_inputs.to_dict()
         if not params[step_id]:
             del params[step_id]
Esempio n. 15
0
 def analyze_forms(self, request, context, workflow, params, gi, wksph,
                   nseq, length, seqaa):
     steps = workflow.json['steps']
     step_id = u'0'
     for tool_form in context['form_list']:
         if not tool_form.is_valid():
             raise WorkflowInvalidFormError("One form is invalid %s " %
                                            (tool_form.prefix))
         tid = getattr(tool_form, 'tool_id', 'null')
         t = Tool.objects.get(id_galaxy=tid)
         tool_inputs = inputs()
         # mapping between form id to galaxy params names
         fields = tool_form.fields_ids_mapping
         inputs_data = set(tool_form.input_file_ids)
         # set the Galaxy parameter (name, value)
         nboot = 0
         boot = False
         for key, value in tool_form.cleaned_data.items():
             if key not in inputs_data:
                 if fields.get(key, "") == 'bootstrap|replicates':
                     nboot = value
                 if fields.get(
                         key, ""
                 ) == 'bootstrap|do_bootstrap' and value == 'true':
                     boot = True
                 tool_inputs.set_param(fields.get(key), value)
         if not boot:
             nboot = 0
         if not t.can_run_on_data(nseq, length, nboot, seqaa):
             raise WorkflowInputFileFormatError(
                 "Input data is too large for the workflow")
         for inputfile in inputs_data:
             uploaded_file = ""
             if request.FILES:
                 uploaded_file = request.FILES.get(inputfile, '')
             if uploaded_file:
                 tmp_file = tempfile.NamedTemporaryFile()
                 for chunk in uploaded_file.chunks():
                     tmp_file.write(chunk)
                 tmp_file.flush()
                 # send file to galaxy
                 outputs = gi.tools.upload_file(
                     path=tmp_file.name,
                     file_name=uploaded_file.name.encode('ascii', 'ignore'),
                     history_id=wksph.history)
                 file_id = outputs.get('outputs')[0].get('id')
                 tool_inputs.set_dataset_param(fields.get(inputfile),
                                               file_id)
             else:
                 # else paste content
                 content = tool_form.cleaned_data.get(inputfile)
                 if content:
                     tmp_file = tempfile.NamedTemporaryFile()
                     tmp_file.write(content)
                     tmp_file.flush()
                     # send file to galaxy
                     input_fieldname = tool_form.fields_ids_mapping.get(
                         inputfile)
                     outputs = gi.tools.upload_file(
                         path=tmp_file.name,
                         file_name=input_fieldname + " pasted_sequence",
                         history_id=wksph.history)
                     file_id = outputs.get('outputs')[0].get('id')
                     tool_inputs.set_dataset_param(fields.get(inputfile),
                                                   file_id)
         # workflow step
         # get from which step the tools are used
         for i, step in steps.items():
             if (getattr(tool_form, 'tool_id',
                         'null') == step.get('tool_id')):
                 step_id = i
                 break
         # convert inputs to dict
         params[step_id] = tool_inputs.to_dict()
         if not params[step_id]:
             del params[step_id]
Esempio n. 16
0
def launch_pasteur_blast(blastrunid, sequence, prog, db, evalue, coverage,
                         maxseqs):
    """
    Celery task that will launch a blast on the pasteur Galaxy Server
    """
    logging.info("Blasting %s with %s on %s" % (sequence, prog, db))
    b = BlastRun.objects.get(id=blastrunid)
    try:
        fasta_io = StringIO(sequence)
        records = list(SeqIO.parse(fasta_io, "fasta"))
        if len(records) == 1:
            galaxycon = galaxy_connection()
            galaxycon.nocache = True
            history = galaxycon.histories.create_history(name="BlastXplorer")

            b.history = history.get("id")
            b.query_id = biofile.cleanseqname(records[0].id)
            b.query_seq = records[0].seq
            b.evalue = evalue
            b.coverage = coverage
            b.database = db
            b.blastprog = prog
            b.maxseqs = maxseqs
            b.status = BlastRun.PENDING
            b.save()

            blast_type = BlastRun.blast_type(BlastRun.PASTEUR, prog)
            blast_inputtype = BlastRun.blast_inputtype(BlastRun.PASTEUR, prog)

            # We check alphabet of given sequence
            if ((blast_inputtype == "nt" and not biofile.check_nt(b.query_seq))
                    or (blast_inputtype == "aa"
                        and not biofile.check_aa(b.query_seq))):
                b.status = BlastRun.ERROR
                b.message = "The given sequence has the wrong alphabet. Program %s expects %s sequence" % (
                    blast_type, blast_inputtype)
            elif blast_type is not None:
                tmp_file = tempfile.NamedTemporaryFile()
                tmp_file.write(sequence)
                tmp_file.flush()
                if biofile.is_fasta_one_seq(tmp_file.name):
                    ## Upload input query file to galaxy
                    outputs = galaxycon.tools.upload_file(
                        path=tmp_file.name,
                        file_name="blastinput.fasta",
                        history_id=history.get("id"),
                        file_type="fasta")
                    file_id = outputs.get('outputs')[0].get('id')
                    ## Configuring job
                    tool_inputs = inputs()
                    tool_inputs.set_dataset_param("query", file_id)
                    tool_inputs.set_param("db_opts|database", db)
                    tool_inputs.set_param("blast_type", blast_type)
                    tool_inputs.set_param("evalue_cutoff", evalue)
                    tool_inputs.set_param("output|out_format", "5")
                    ## Running blast job
                    outputs = galaxycon.tools.run_tool(
                        history_id=history.get("id"),
                        tool_id=prog,
                        tool_inputs=tool_inputs)
                    b.history_fileid = outputs.get("outputs")[0].get("id")
                else:
                    b.status = BlastRun.ERROR
                    b.message = "Bad input FASTA file format"
            else:
                b.status = BlastRun.ERROR
                b.message = "Wrong blast program %s" % (prog)
            b.save()
        else:
            b.status = BlastRun.ERROR
            b.message = "More than one record in the fasta file! %d" % (len(
                list(records)))
    except Exception as e:
        logging.exception(str(e))
        b.status = BlastRun.ERROR
        b.message = str(e)
    b.save()
    time.sleep(30)
Esempio n. 17
0
def tool_exec_view(request, pk, store_output=None):
    """
    :param request:
    :param pk:
    :param store_output:
    :return:
    """

    gi = request.galaxy
    message = ""

    tool_obj = get_object_or_404(Tool, pk=pk)

    toolfield, created = ToolFieldWhiteList.objects.get_or_create(
        tool=tool_obj, context='t')
    tool_visible_field = toolfield.saved_params

    tool_inputs_details = gi.tools.show_tool(
        tool_id=tool_obj.id_galaxy, io_details='true')
    tool_form = ToolForm(tool_params=tool_inputs_details['inputs'],
                         tool_id=pk, whitelist=tool_visible_field,
                         data=request.POST or None,
                         session_files=request.session.get('files')
                         )

    hid = {}
    if request.method == 'POST':
        if tool_form.is_valid():
            try:
                wksph = None 
                tool_inputs = inputs()

                # mapping between form id to galaxy params names
                fields = tool_form.fields_ids_mapping
                exts = tool_form.fields_ext_mapping

                inputs_data = set(tool_form.input_file_ids)
                nboot = 0
                boot = False
                for key, value in request.POST.items():
                    if not key in inputs_data:
                        if fields.get(key,"") == 'bootstrap|replicates':
                            nboot = value
                        if (fields.get(key,"") == 'bootstrap|do_bootstrap' and value == 'true' or
                            fields.get(key,"") == 'bootstrap|support' and (value == 'boot' or value == '1')):
                            boot = True
                        tool_inputs.set_param(fields.get(key), value)
                if not boot:
                    nboot = 0
                for inputfile in inputs_data:
                    outputs = ""
                    uploaded_file = ""
                    if request.FILES:
                        uploaded_file = request.FILES.get(inputfile, '')

                    tmp_file = tempfile.NamedTemporaryFile()
                    if uploaded_file:
                        for chunk in uploaded_file.chunks():
                            tmp_file.write(chunk)
                        tmp_file.flush()
                        # send file to galaxy after verifying the
                        # allowed extensions
                        type = biofile.detect_type(tmp_file.name)
                        nseq, length, seqaa = biofile.nb_sequences(tmp_file.name, type)
                        if type in ["fasta", "phylip"] and nseq <= 3:
                            msg='Input Sequence file should contain more than 3 sequences for field %s' % (
                                fields.get(inputfile))
                            raise ValueError(msg)
                        if type in ["fasta", "phylip"] and not tool_obj.can_run_on_data(nseq, length, nboot, seqaa):
                            raise ValueError('Given data is too large to run with this tool')
                        if type in exts.get(inputfile, ""):
                            if wksph is None:
                                wksph = create_history(request,
                                                       name="Analyse with " + tool_obj.name,
                                                       wf_category="Tool",
                                                       wf_steps=tool_obj.name)
                            outputs = gi.tools.upload_file(path=tmp_file.name,
                                                           file_name=uploaded_file.name,
                                                           history_id=wksph.history,
                                                           file_type=type)
                        else:
                            raise ValueError('File format of file %s is not allowed for field %s' % (
                                uploaded_file.name, fields.get(inputfile)))
                    else:
                        # else paste content
                        content = request.POST.get(inputfile)
                        galaxyfile = request.POST.get(
                            "galaxyfile_" + inputfile)
                        if content:
                            tmp_file = tempfile.NamedTemporaryFile()
                            tmp_file.write(content)
                            tmp_file.flush()
                            # send file to galaxy after verifying the
                            # allowed extensions
                            input_fieldname = fields.get(inputfile)
                            type = biofile.detect_type(tmp_file.name)
                            nseq, length, seqaa = biofile.nb_sequences(tmp_file.name, type)
                            if type in ["fasta", "phylip"] and nseq <= 3:
                                msg='Input Sequence file should contain more than 3 sequences for field %s' % (
                                    fields.get(inputfile))
                                raise ValueError(msg)
                            if type in ["fasta", "phylip"] and not tool_obj.can_run_on_data(nseq, length, nboot, seqaa):
                                raise ValueError('Given data is too large to run with this tool')
                            if type in exts.get(inputfile, ""):
                                if wksph is None:
                                    wksph = create_history(request,
                                                           name="Analyse with " + tool_obj.name,
                                                           wf_category="Tool",
                                                           wf_steps=tool_obj.name)
                                outputs = gi.tools.upload_file(path=tmp_file.name,
                                                               file_name=input_fieldname + " pasted_sequence",
                                                               history_id=wksph.history,
                                                               file_type=type)
                            else:
                                raise ValueError(
                                    'This file format is not allowed for field %s' % (input_fieldname))
                        # Else we look at galaxy file ids
                        else:
                            if galaxyfile:
                                file_id = galaxyfile
                                hid[inputfile] = file_id
                            elif inputfile == "1":
                                # It should be the initial input file
                                # and is required
                                raise ValueError('No input file given')
                    if outputs:
                        file_id = outputs.get('outputs')[0].get('id')
                        hid[inputfile] = file_id

                for h, file_id in hid.items():
                    tool_inputs.set_dataset_param(fields.get(h), file_id)

                if wksph is None:
                    wksph = create_history(request,
                                           name="Analyse with " + tool_obj.name,
                                           wf_category="Tool",
                                           wf_steps=tool_obj.name)
                    
                tool_outputs = gi.tools.run_tool(history_id=wksph.history,
                                                 tool_id=tool_obj.id_galaxy,
                                                 tool_inputs=tool_inputs)
                # Start monitoring (for sending emails)
                initializeworkspacejob.delay(wksph.history)
                wksph.monitored = True
                wksph.save()

                if store_output:
                    request.session['output'] = tool_outputs

                return redirect(reverse_lazy("history_detail", kwargs={'history_id': wksph.history}))

            except ValueError as ve:
                message = str(ve)
            except ConnectionError as ce:
                message = str(ce)
            except NameError as ne:
                message = str(ne)
            except OperationalError as oe:
                message = str(oe)
            except AttributeError as ae:
                message = str(ae)
            except TypeError as te:
                message = str(te)
            except Exception as e:
                raw_message = ast.literal_eval(e)
                reverse_dict_field = {
                    v: k for k, v in tool_form.fields_ids_mapping.items()}
                err_data = raw_message.get('err_data')

                if err_data:
                    for field, err_msg in err_data.items():
                        tool_form.add_error(reverse_dict_field.get(field),
                                            ValidationError(err_msg, code='invalid'))
                else:
                    message = raw_message

                delete_history(request, wksph.history)

    context = {"toolform": tool_form,
               "tool": tool_obj,
               "message": message}

    return render(request, 'tools/tool_form.html', context)
    def analyze_forms(self, request, context, workflow, params, gi, wksph):
        steps = workflow.json['steps']
        step_id = u'0'
        for tool_form in context['form_list']:
            if not tool_form.is_valid():
                raise WorkflowInvalidFormError(
                    "One form is invalid %s " % (tool_form.prefix))

            tool_inputs = inputs()
            # mapping between form id to galaxy params names
            fields = tool_form.fields_ids_mapping
            inputs_data = set(tool_form.input_file_ids)
            # set the Galaxy parameter (name, value)
            for key, value in tool_form.cleaned_data.items():
                if key not in inputs_data:
                    tool_inputs.set_param(fields.get(key), value)
            for inputfile in inputs_data:
                uploaded_file = ""
                if request.FILES:
                    uploaded_file = request.FILES.get(inputfile, '')
                if uploaded_file:
                    tmp_file = tempfile.NamedTemporaryFile()
                    for chunk in uploaded_file.chunks():
                        tmp_file.write(chunk)
                    tmp_file.flush()
                    # send file to galaxy
                    outputs = gi.tools.upload_file(
                        path=tmp_file.name,
                        file_name=uploaded_file.name,
                        history_id=wksph.history)
                    file_id = outputs.get('outputs')[0].get('id')
                    tool_inputs.set_dataset_param(
                        fields.get(inputfile), file_id)
                else:
                    # else paste content
                    content = tool_form.cleaned_data.get(inputfile)
                    if content:
                        tmp_file = tempfile.NamedTemporaryFile()
                        tmp_file.write(content)
                        tmp_file.flush()
                        # send file to galaxy
                        input_fieldname = tool_form.fields_ids_mapping.get(
                            inputfile)
                        outputs = gi.tools.upload_file(
                            path=tmp_file.name,
                            file_name=input_fieldname + " pasted_sequence",
                            history_id=wksph.history)
                        file_id = outputs.get('outputs')[0].get('id')
                        tool_inputs.set_dataset_param(fields.get(inputfile),
                                                      file_id)
            # workflow step
            # get from which step the tools are used
            for i, step in steps.items():
                if (getattr(tool_form, 'tool_id', 'null') ==
                        step.get('tool_id')):
                    step_id = i
                    break
            # convert inputs to dict
            params[step_id] = tool_inputs.to_dict()
            if not params[step_id]:
                del params[step_id]
Esempio n. 19
0
def make_inputs(input_params):
    ''' input builder'''
    tool_inputs = inputs()
    for i in input_params:
        tool_inputs = tool_inputs.set(i, input_params[i])
    return tool_inputs