From 35ab22585edd37b13a78dbe51edc279eb56002e6 Mon Sep 17 00:00:00 2001 From: Stephen Ficklin Date: Fri, 22 Nov 2019 15:43:10 -0800 Subject: [PATCH] Updated to handle dataset collections in results --- api/tripal_galaxy.api.inc | 75 +- .../de/db0/group__tripal__galaxy__api.xml | 200 +- .../df/d55/tripal__galaxy_8api_8inc.xml | 2081 +++++++++-------- .../dir_0188a3e6da905bc60aceb35bf790b8c9.xml | 4 +- docs/doxyxml/index.xml | 19 +- docs/workflow_settings.rst | 2 +- includes/tripal_galaxy.results.inc | 495 ++-- 7 files changed, 1433 insertions(+), 1443 deletions(-) diff --git a/api/tripal_galaxy.api.inc b/api/tripal_galaxy.api.inc index 642f4a4..3718d26 100755 --- a/api/tripal_galaxy.api.inc +++ b/api/tripal_galaxy.api.inc @@ -601,32 +601,59 @@ function tripal_galaxy_check_submission_status(int $sid = NULL, bool $force = FA // Get more details about each history content item. foreach ($history_contents as $index => $history_content) { - $history_contents[$index] = $ghistory_contents->show([ - 'id' => $history_content['id'], - 'history_id' => $history['id'], - ]); - switch ($history_content['type']) { - case 'file': - $params = []; - $params['history_id'] = $history['id']; - $params['url_only'] = TRUE; - $params['history_content_id'] = $history_content['id']; - $link = $ghistory_contents->download_history_content($params); - $history_contents[$index]['content_link'] = $link; - break; - - default: - break; + // For datasets collections handle things differently. + if ($history_content['type'] == 'collection') { + $history_contents[$index] = $ghistory_contents->show([ + 'id' => 'dataset_collections/' . $history_content['id'], + 'history_id' => $history['id'], + ]); + // Get history details for each element in the data collection. + foreach ($history_contents[$index]['elements'] as $eindex => $element) { + $object_contents = $ghistory_contents->show([ + 'id' => $element['object']['id'], + 'history_id' => $element['object']['history_id'], + ]); + if ($object_contents['type'] == 'file') { + $params = []; + $params['history_id'] = $object_contents['history_id']; + $params['url_only'] = TRUE; + $params['history_content_id'] = $object_contents['id']; + $link = $ghistory_contents->download_history_content($params); + $object_contents['content_link'] = $link; + } + $history_contents[$index]['elements'][$eindex]['contents'] = $object_contents; + } } - $job_id = $history_contents[$index]['creating_job']; - $gJobs = new GalaxyJobs($galaxy); - $job = $gJobs->show(['job_id' => $job_id, 'full' => 'TRUE']); - if (!$job) { - $error = $galaxy->getError(); - drupal_set_message('Could not retreive details about the submitting job. ' . $error['message'], 'error'); - return FALSE; + // For datasets get the job information. + else { + $history_contents[$index] = $ghistory_contents->show([ + 'id' => $history_content['id'], + 'history_id' => $history['id'], + ]); + + switch ($history_content['type']) { + case 'file': + $params = []; + $params['history_id'] = $history['id']; + $params['url_only'] = TRUE; + $params['history_content_id'] = $history_content['id']; + $link = $ghistory_contents->download_history_content($params); + $history_contents[$index]['content_link'] = $link; + break; + + default: + break; + } + $job_id = $history_contents[$index]['creating_job']; + $gJobs = new GalaxyJobs($galaxy); + $job = $gJobs->show(['job_id' => $job_id, 'full' => 'TRUE']); + if (!$job) { + $error = $galaxy->getError(); + drupal_set_message('Could not retreive details about the submitting job. ' . $error['message'], 'error'); + return FALSE; + } + $history_contents[$index]['creating_job'] = $job; } - $history_contents[$index]['creating_job'] = $job; } // Save all this history information so we can cahce it. diff --git a/docs/doxyxml/de/db0/group__tripal__galaxy__api.xml b/docs/doxyxml/de/db0/group__tripal__galaxy__api.xml index ed17ecd..a5567a4 100644 --- a/docs/doxyxml/de/db0/group__tripal__galaxy__api.xml +++ b/docs/doxyxml/de/db0/group__tripal__galaxy__api.xml @@ -4,68 +4,6 @@ tripal_galaxy_api Tripal Galaxy API @{ Provides an application - - - _tripal_galaxy_get_workflow_defaults - (array $tool_inputs, array &$parameters, array $step_inputs, string $step_input_key) - _tripal_galaxy_get_workflow_defaults - - array - $tool_inputs - - - array & - $parameters - - - array - $step_inputs - - - string - $step_input_key - - - - -A recursive helper function for tripal_galaxy_get_workflow_defaults. - -array -$tool_inputs - - -The 'tool_inputs' array for the step, or the sub elements on a recursive call. - - - -array -$parameters - - -A pass-by-reference of the $parameters array. - - - -array -$step_inputs - - -The 'input_steps' array for the step. This is always passed as is at every recursion level. - - - -string -$step_input_key - - -The key used to lookup up if an element has a key in the $step_inputs. For nested keys they are concatenated. with a '|' separating each level. - - - - - - - tripal_galaxy_add_galaxy @@ -91,7 +29,7 @@ - + @@ -143,7 +81,7 @@ - + @@ -184,7 +122,7 @@ - + @@ -223,7 +161,7 @@ - + @@ -236,7 +174,7 @@ Deletes expired histories.Walks through the tripal_galaxy_workflow_submission table and deletes any workflows older in days than specified in the tripal_galaxy_history_days_limit system variable. - + @@ -275,7 +213,7 @@ - + @@ -334,7 +272,7 @@ - + @@ -348,7 +286,7 @@ - + @@ -375,7 +313,7 @@ - + @@ -420,7 +358,7 @@ - + @@ -455,7 +393,7 @@ - + @@ -469,7 +407,7 @@ - + @@ -483,7 +421,7 @@ - + @@ -510,7 +448,7 @@ - + @@ -549,7 +487,7 @@ - + @@ -575,117 +513,73 @@ - + - + - tripal_galaxy_get_proxy_url - (stdClass $submission, array $dataset, int $uid, string $action) - tripal_galaxy_get_proxy_url - - stdClass - $submission - - - array - $dataset - + tripal_galaxy_get_submission + (int $sid) + tripal_galaxy_get_submission int - $uid - - - string - $action + $sid -Adds a URL to the proxy array.This function can be used to create a URL for downloading of a dataset from a remote Galaxy server. - -stdClass -$submission - - -A submission object as obtained by the tripal_galaxy_get_submission() function. - - - -array -$dataset - - -An array describing the dataset as returned by the tripal_galaxy_get_dataset() or tripal_galaxy_get_datasets() functions. - - +Retrieves a workflow submission object using the submission ID.The returned submission object includes all information about the submission. If the submission has been invoked then the 'errors' element will have more information about the run including the history information, the history name, and the state of the history_contents from the last time the status of the submission was checked. If the status of the submission is 'Completed' then the history_contents should contain all the history contents. int -$uid - - -The user that is allowed to view this link. - - - -string -$action - - -The action to perform when the link is clicked. Can be 'viewer', 'viewer-full', or 'download'. The action 'viewer' will create a link for results to be shown inside of a page on the Drupal site. The action 'viewer-full' will create a link for results to be shown stand-alone. The action 'download' will create a link that will start a download of the file. - - - - -Exception +$sid - +The submission ID of the workflow. -A URL that can be used, when clicked by the end-user, to either view or download the dataset. +object An object containing the submission information. - + - + - tripal_galaxy_get_submission - (int $sid) - tripal_galaxy_get_submission + tripal_galaxy_get_user_submissions + (int $userid) + tripal_galaxy_get_user_submissions int - $sid + $userid -Retrieves a workflow submission object using the submission ID.The returned submission object includes all information about the submission. If the submission has been invoked then the 'errors' element will have more information about the run including the history information, the history name, and the state of the history_contents from the last time the status of the submission was checked. If the status of the submission is 'Completed' then the history_contents should contain all the history contents. +Retrieve all submission IDs for a user. int -$sid +$userid -The submission ID of the workflow. +User id number. -object An object containing the submission information. +array A list of submission ids. - + - + tripal_galaxy_get_workflow_defaults - (GalaxyInstance $galaxy, int $workflow_id) + (GalaxyInstance $galaxy, string $workflow_id) tripal_galaxy_get_workflow_defaults GalaxyInstance $galaxy - int + string $workflow_id @@ -701,18 +595,18 @@ -int +string $workflow_id -The ID of the workflow to retrieve settings for. This is the Galaxy ID for the workflow. +The ID of the workflow to retrieve settings for. This is the Galaxy ID for the workflow not Tripal's internal workflow ID. bool|array An array compatible with the $parameters argument for the tripal_galaxy_invoke_wokflow(). Values are populatd with appropriate defaults. Returns FALSE if the workflow settings could not be retrieved. - + @@ -740,7 +634,7 @@ - + @@ -777,7 +671,7 @@ - + @@ -865,7 +759,7 @@ - + @@ -892,7 +786,7 @@ - + @@ -962,7 +856,7 @@ - + diff --git a/docs/doxyxml/df/d55/tripal__galaxy_8api_8inc.xml b/docs/doxyxml/df/d55/tripal__galaxy_8api_8inc.xml index 83fb726..bb3c988 100644 --- a/docs/doxyxml/df/d55/tripal__galaxy_8api_8inc.xml +++ b/docs/doxyxml/df/d55/tripal__galaxy_8api_8inc.xml @@ -2,11 +2,8 @@ tripal_galaxy.api.inc - PharIo::Version::OrVersionConstraintGroup - phpDocumentor::Reflection::Types::This - TheSeer::Tokenizer::Token - + _tripal_galaxy_get_workflow_defaults (array $tool_inputs, array &$parameters, array $step_inputs, string $step_input_key) @@ -66,7 +63,7 @@ - + @@ -93,7 +90,7 @@ - + @@ -145,7 +142,7 @@ - + @@ -186,7 +183,7 @@ - + @@ -225,7 +222,7 @@ - + @@ -238,7 +235,7 @@ Deletes expired histories.Walks through the tripal_galaxy_workflow_submission table and deletes any workflows older in days than specified in the tripal_galaxy_history_days_limit system variable. - + @@ -277,7 +274,7 @@ - + @@ -336,7 +333,7 @@ - + @@ -350,7 +347,7 @@ - + @@ -377,7 +374,7 @@ - + @@ -422,7 +419,7 @@ - + @@ -457,7 +454,7 @@ - + @@ -471,7 +468,7 @@ - + @@ -485,7 +482,7 @@ - + @@ -512,7 +509,7 @@ - + @@ -551,7 +548,7 @@ - + @@ -577,117 +574,73 @@ - + - + - tripal_galaxy_get_proxy_url - (stdClass $submission, array $dataset, int $uid, string $action) - tripal_galaxy_get_proxy_url - - stdClass - $submission - - - array - $dataset - + tripal_galaxy_get_submission + (int $sid) + tripal_galaxy_get_submission int - $uid - - - string - $action + $sid -Adds a URL to the proxy array.This function can be used to create a URL for downloading of a dataset from a remote Galaxy server. - -stdClass -$submission - - -A submission object as obtained by the tripal_galaxy_get_submission() function. - - - -array -$dataset - - -An array describing the dataset as returned by the tripal_galaxy_get_dataset() or tripal_galaxy_get_datasets() functions. - - +Retrieves a workflow submission object using the submission ID.The returned submission object includes all information about the submission. If the submission has been invoked then the 'errors' element will have more information about the run including the history information, the history name, and the state of the history_contents from the last time the status of the submission was checked. If the status of the submission is 'Completed' then the history_contents should contain all the history contents. int -$uid - - -The user that is allowed to view this link. - - - -string -$action - - -The action to perform when the link is clicked. Can be 'viewer', 'viewer-full', or 'download'. The action 'viewer' will create a link for results to be shown inside of a page on the Drupal site. The action 'viewer-full' will create a link for results to be shown stand-alone. The action 'download' will create a link that will start a download of the file. - - - - -Exception +$sid - +The submission ID of the workflow. -A URL that can be used, when clicked by the end-user, to either view or download the dataset. +object An object containing the submission information. - + - + - tripal_galaxy_get_submission - (int $sid) - tripal_galaxy_get_submission + tripal_galaxy_get_user_submissions + (int $userid) + tripal_galaxy_get_user_submissions int - $sid + $userid -Retrieves a workflow submission object using the submission ID.The returned submission object includes all information about the submission. If the submission has been invoked then the 'errors' element will have more information about the run including the history information, the history name, and the state of the history_contents from the last time the status of the submission was checked. If the status of the submission is 'Completed' then the history_contents should contain all the history contents. +Retrieve all submission IDs for a user. int -$sid +$userid -The submission ID of the workflow. +User id number. -object An object containing the submission information. +array A list of submission ids. - + - + tripal_galaxy_get_workflow_defaults - (GalaxyInstance $galaxy, int $workflow_id) + (GalaxyInstance $galaxy, string $workflow_id) tripal_galaxy_get_workflow_defaults GalaxyInstance $galaxy - int + string $workflow_id @@ -703,18 +656,18 @@ -int +string $workflow_id -The ID of the workflow to retrieve settings for. This is the Galaxy ID for the workflow. +The ID of the workflow to retrieve settings for. This is the Galaxy ID for the workflow not Tripal's internal workflow ID. bool|array An array compatible with the $parameters argument for the tripal_galaxy_invoke_wokflow(). Values are populatd with appropriate defaults. Returns FALSE if the workflow settings could not be retrieved. - + @@ -742,7 +695,7 @@ - + @@ -779,7 +732,7 @@ - + @@ -867,7 +820,7 @@ - + @@ -894,7 +847,7 @@ - + @@ -921,7 +874,7 @@ - + @@ -991,7 +944,7 @@ - + @@ -1001,349 +954,349 @@ <?php -usePharIo\Version\OrVersionConstraintGroup; -useTheSeer\Tokenizer\Token; -usephpDocumentor\Reflection\Types\This; - -functiontripal_galaxy_get_connection(int$galaxy_id){ -//Getthegalaxyserverforthisworkflow. -$galaxy_server=db_select('tripal_galaxy','tg')->fields('tg') -->condition('galaxy_id',$galaxy_id) -->execute() -->fetchObject(); - -$library=libraries_load('blend4php'); -if(array_key_exists('error',$library)){ -drupal_set_message($library['errormessage'],'error'); -returnFALSE; -} - -$connect=tripal_galaxy_split_url($galaxy_server->url); -$galaxy=newGalaxyInstance($connect['host'],$connect['port'],$connect['use_https']); -$galaxy->setAPIKey($galaxy_server->api_key); -$error=$galaxy->getErrorType(); -if($error){ -returnFALSE; -} -return$galaxy; -} - -functiontripal_galaxy_split_url(string$url){ - -//TODO:shouldthisgointoblend4php? -//FirstcheckaURLwithaport. -$matches=[]; -if(preg_match('/^(.*)\:\/\/(.+?)\:(\d+)\/*$/',$url,$matches)){ -$protocol=$matches[1]; -$host=$matches[2]; -$port=$matches[3]; -$use_https=FALSE; -if($protocol=='https'){ -$use_https=TRUE; -} -} -//NextcheckaURLwithoutaport. -else{ -if(preg_match('/^(.*)\:\/\/(.+?)\/*$/',$url,$matches)){ -$protocol=$matches[1]; -$host=$matches[2]; -$use_https=FALSE; -$port=80; -if($protocol=='https'){ -$use_https=TRUE; -$port=443; -} -} -//Forsimpleurlw/portie.localhost:8080. -else{ -if(preg_match('/(.*)\:(\d+)\/*$/',$url,$matches)){ -$use_https=FALSE; -$host=$matches[1]; -$port=$matches[2]; -} -} -} -return[ -'host'=>$host, -'port'=>$port, -'use_https'=>$use_https, -]; -} - -functiontripal_galaxy_add_workflow(int$galaxy_id,array$values,bool$create_webform=TRUE){ -$transaction=db_transaction(); -try{ -//Connecttothegalaxyserver. -$galaxy=tripal_galaxy_get_connection($galaxy_id); - -//Gettheworkflowdetailsandseeifthisworkflowexists. -$gworkflows=newGalaxyWorkflows($galaxy); -$workflows=$gworkflows->index(); -if(!$workflows){ -$error=$galaxy->getError(); -if(empty($error['message'])){ -$error['message']='NoworkflowusingthecriteriaprovidedwasfoundinthisGalaxyserver.'; -} -drupal_set_message($error['message'],'error'); -returnFALSE; -} - -//Loopthrougheachworkflowtoaddittotheform. -$found_workflow=NULL; -foreach($workflowsas$workflow){ -if(array_key_exists('workflow_name',$values)and$workflow['name']==$values['workflow_name']){ -$found_workflow=$workflow; -} -if(array_key_exists('workflow_id',$values)and$workflow['id']==$values['workflow_id']){ -$found_workflow=$workflow; -} -} - -if(!$found_workflow){ -drupal_set_message('NoworkflowusingthecriteriaprovidedwasfoundinthisGalaxyserver.','error'); -returnFALSE; -} - -//Nowlet'schecktoseeifthisworkflowexistsinthedatabase,Ifit -//existsthenjustreturnit. -$workflow=db_select('tripal_galaxy_workflow','tgw')->fields('tgw') -->condition('tgw.workflow_id',$found_workflow['id']) -->condition('tgw.galaxy_id',$galaxy_id) -->execute() -->fetchObject(); -if($workflow){ -return$workflow; -} - -//Inserttheworkflow. -$data=[ -'workflow_name'=>$found_workflow['name'], -'workflow_id'=>$found_workflow['id'], -'galaxy_id'=>$galaxy_id, -'workflow_uuid'=>$found_workflow['latest_workflow_uuid'], -]; -$success=drupal_write_record('tripal_galaxy_workflow',$data); -if($success){ -$workflows=tripal_galaxy_get_workflows([ -'workflow_id'=>$found_workflow['id'], -'galaxy_id'=>$galaxy_id, -]); -$workflow=$workflows[0]; +functiontripal_galaxy_get_connection(int$galaxy_id){ +//Getthegalaxyserverforthisworkflow. +$galaxy_server=db_select('tripal_galaxy','tg')->fields('tg') +->condition('galaxy_id',$galaxy_id) +->execute() +->fetchObject(); + +$library=libraries_load('blend4php'); +if(array_key_exists('error',$library)){ +drupal_set_message($library['errormessage'],'error'); +returnFALSE; +} + +$connect=tripal_galaxy_split_url($galaxy_server->url); +$galaxy=newGalaxyInstance($connect['host'],$connect['port'],$connect['use_https']); +$galaxy->setAPIKey($galaxy_server->api_key); +$error=$galaxy->getErrorType(); +if($error){ +returnFALSE; +} +return$galaxy; +} + +functiontripal_galaxy_split_url(string$url){ + +//TODO:shouldthisgointoblend4php? +//FirstcheckaURLwithaport. +$matches=[]; +if(preg_match('/^(.*)\:\/\/(.+?)\:(\d+)\/*$/',$url,$matches)){ +$protocol=$matches[1]; +$host=$matches[2]; +$port=$matches[3]; +$use_https=FALSE; +if($protocol=='https'){ +$use_https=TRUE; +} +} +//NextcheckaURLwithoutaport. +else{ +if(preg_match('/^(.*)\:\/\/(.+?)\/*$/',$url,$matches)){ +$protocol=$matches[1]; +$host=$matches[2]; +$use_https=FALSE; +$port=80; +if($protocol=='https'){ +$use_https=TRUE; +$port=443; +} +} +//Forsimpleurlw/portie.localhost:8080. +else{ +if(preg_match('/(.*)\:(\d+)\/*$/',$url,$matches)){ +$use_https=FALSE; +$host=$matches[1]; +$port=$matches[2]; +} +} +} +return[ +'host'=>$host, +'port'=>$port, +'use_https'=>$use_https, +]; +} + +functiontripal_galaxy_add_workflow(int$galaxy_id,array$values,bool$create_webform=TRUE){ +$transaction=db_transaction(); +try{ +//Connecttothegalaxyserver. +$galaxy=tripal_galaxy_get_connection($galaxy_id); + +//Gettheworkflowdetailsandseeifthisworkflowexists. +$gworkflows=newGalaxyWorkflows($galaxy); +$workflows=$gworkflows->index(); +if(!$workflows){ +$error=$galaxy->getError(); +if(empty($error['message'])){ +$error['message']='NoworkflowusingthecriteriaprovidedwasfoundinthisGalaxyserver.'; +} +drupal_set_message($error['message'],'error'); +returnFALSE; +} + +//Loopthrougheachworkflowtoaddittotheform. +$found_workflow=NULL; +foreach($workflowsas$workflow){ +if(array_key_exists('workflow_name',$values)and$workflow['name']==$values['workflow_name']){ +$found_workflow=$workflow; +} +if(array_key_exists('workflow_id',$values)and$workflow['id']==$values['workflow_id']){ +$found_workflow=$workflow; +} +} + +if(!$found_workflow){ +drupal_set_message('NoworkflowusingthecriteriaprovidedwasfoundinthisGalaxyserver.','error'); +returnFALSE; +} + +//Nowlet'schecktoseeifthisworkflowexistsinthedatabase,Ifit +//existsthenjustreturnit. +$workflow=db_select('tripal_galaxy_workflow','tgw')->fields('tgw') +->condition('tgw.workflow_id',$found_workflow['id']) +->condition('tgw.galaxy_id',$galaxy_id) +->execute() +->fetchObject(); +if($workflow){ +return$workflow; +} + +//Inserttheworkflow. +$data=[ +'workflow_name'=>$found_workflow['name'], +'workflow_id'=>$found_workflow['id'], +'galaxy_id'=>$galaxy_id, +'workflow_uuid'=>$found_workflow['latest_workflow_uuid'], +]; +$success=drupal_write_record('tripal_galaxy_workflow',$data); +if($success){ +$workflows=tripal_galaxy_get_workflows([ +'workflow_id'=>$found_workflow['id'], +'galaxy_id'=>$galaxy_id, +]); +$workflow=$workflows[0]; +} +else{ +drupal_set_message('Couldnotaddtheworkflow.','error'); +returnFALSE; } -else{ -drupal_set_message('Couldnotaddtheworkflow.','error'); -returnFALSE; -} - -//Iftheuserwantstocreatethewebformforend-userstosubmit. -if($create_webform){ -module_load_include('inc','tripal_galaxy','includes/tripal_galaxy.webform'); -$nid=tripal_galaxy_build_webform($galaxy_id,$workflow->workflow_id); -$num_updated=db_update('tripal_galaxy_workflow')->fields([ -'nid'=>$nid, -]) -->condition('workflow_id',$workflow->workflow_id) -->condition('galaxy_id',$galaxy_id) -->execute(); -$workflow->nid=$nid; -} - -return$workflow; -} -catch(Exception$e){ -$transaction->rollback(); -tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'UnabletoimportGalaxyworkflow'); -returnFALSE; -} -} - -functiontripal_galaxy_get_workflows(array$values=[]){ -$workflows=[]; - -$query=db_select('tripal_galaxy_workflow','tgw'); -$query->fields('tgw'); -if(array_key_exists('galaxy_id',$values)){ -$query->condition('tgw.galaxy_id',$values['galaxy_id']); -} -if(array_key_exists('id',$values)){ -$query->condition('tgw.galaxy_workflow_id',$values['id']); -} -if(array_key_exists('name',$values)){ -$query->condition('tgw.workflow_name',$values['name']); -} -if(array_key_exists('workflow_id',$values)){ -$query->condition('tgw.workflow_id',$values['workflow_id']); -} -if(array_key_exists('status',$values)){ -$query->condition('tgw.status',$values['status']); -} -$results=$query->execute(); - -while($workflow=$results->fetchObject()){ -if($workflow->nid){ -$node=node_load($workflow->nid); -$workflow->node_title=$node->title; -} -$workflows[]=$workflow; -} - -return$workflows; -} - -functiontripal_galaxy_init_submission($workflow,$user){ -$transaction=db_transaction(); - -try{ -$data=[ -'galaxy_workflow_id'=>$workflow->galaxy_workflow_id, -'errors'=>'', -'submit_date'=>REQUEST_TIME, -'status'=>'Waiting', -'uid'=>$user->uid, -]; - -//Nowinsertthesubmissionintothedatabase. -$sid=db_insert('tripal_galaxy_workflow_submission')->fields($data)->execute(); - -return$sid; -} -catch(Exception$e){ -$transaction->rollback(); -tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'UnabletocreateGalaxyworkflowsubmission'); -returnFALSE; -} -} - -functiontripal_galaxy_get_submission(int$sid){ -$query=db_select('tripal_galaxy_workflow_submission','tgws'); -$query->fields('tgws'); -$query->join('tripal_galaxy_workflow','tgw','tgw.galaxy_workflow_id=tgws.galaxy_workflow_id'); -$query->fields('tgw',[ -'galaxy_id', -'workflow_id', -'nid', -'workflow_name', -]); -//Doaleft-joinonthenodetogetthetitleforthewebformbutin -//theeventthissubmissiondidnotuseawebformtheleftjoinwill -//proivdeaNULLvalueforthewebformtitle. -$query->leftJoin('node','n','(n.nid=tgw.nid)'); -$query->addField('n','title','webform_title'); -$query->condition('tgws.sid',$sid); -$submission=$query->execute()->fetchObject(); -$submission->errors=!empty($submission->errors)?unserialize($submission->errors):[]; - -//Nowaddtheworkflowdetails: -$workflows=tripal_galaxy_get_workflows(['id'=>$submission->galaxy_workflow_id]); -$workflow=$workflows[0]; -$submission->workflow=$workflow; -return$submission; -} - -functiontripal_galaxy_get_galaxies(){ -$galaxies=[]; -$results=db_select('tripal_galaxy','tg')->fields('tg')->execute(); - -while($galaxy=$results->fetchObject()){ -$galaxies[]=$galaxy; -} -return$galaxies; -} - -functiontripal_galaxy_get_galaxy(int$galaxy_id){ -if(!$galaxy_id){ -tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'tripal_galaxy_get_galaxy:missingrequiredfield"$galaxy_id"'); -returnFALSE; -} - -$galaxies=[]; -$galaxy=db_select('tripal_galaxy','tg')->fields('tg') -->condition('galaxy_id',$galaxy_id) -->execute() -->fetchObject(); - -return$galaxy; -} - -functiontripal_galaxy_add_galaxy(array$values){ -$required_args=[ -'servername', -'url', -'username', -'api_key', -'uid', -]; -foreach($required_argsas$key){ -if(!array_key_exists($key,$values)){ -tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'tripal_galaxy_add_galaxy:missingrequiredfield"'.$key.'"'); -returnFALSE; -} -} - -//Theservernamecan'talreadyexist. -$exists=db_select('tripal_galaxy','tg')->fields('tg') -->condition('servername',$values['servername']) -->execute() -->fetchObject(); -if($exists){ -return$exists; -} - -//Createtheelement. -$data=array( -'servername'=>$values['servername'], -'description'=>$values['description'], -'url'=>$values['url'], -'username'=>$values['username'], -'api_key'=>$values['api_key'], -'uid'=>$values['uid'], -); -drupal_write_record('tripal_galaxy',$data); - -returntripal_galaxy_get_galaxy($data['galaxy_id']); -} - -functiontripal_galaxy_get_active_submissions(){ -$query=db_select('tripal_galaxy_workflow_submission','tgws'); -$query->fields('tgws',[ -'invocation_id', -'galaxy_workflow_id', -'sid', -'submit_date', -'status', -'email', -'uid', + +//Iftheuserwantstocreatethewebformforend-userstosubmit. +if($create_webform){ +module_load_include('inc','tripal_galaxy','includes/tripal_galaxy.webform'); +$nid=tripal_galaxy_build_webform($galaxy_id,$workflow->workflow_id); +$num_updated=db_update('tripal_galaxy_workflow')->fields([ +'nid'=>$nid, +]) +->condition('workflow_id',$workflow->workflow_id) +->condition('galaxy_id',$galaxy_id) +->execute(); +$workflow->nid=$nid; +} + +return$workflow; +} +catch(Exception$e){ +$transaction->rollback(); +tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'UnabletoimportGalaxyworkflow'); +returnFALSE; +} +} + +functiontripal_galaxy_get_workflows(array$values=[]){ +$workflows=[]; + +$query=db_select('tripal_galaxy_workflow','tgw'); +$query->fields('tgw'); +if(array_key_exists('galaxy_id',$values)){ +$query->condition('tgw.galaxy_id',$values['galaxy_id']); +} +if(array_key_exists('id',$values)){ +$query->condition('tgw.galaxy_workflow_id',$values['id']); +} +if(array_key_exists('name',$values)){ +$query->condition('tgw.workflow_name',$values['name']); +} +if(array_key_exists('workflow_id',$values)){ +$query->condition('tgw.workflow_id',$values['workflow_id']); +} +if(array_key_exists('status',$values)){ +$query->condition('tgw.status',$values['status']); +} +$results=$query->execute(); + +while($workflow=$results->fetchObject()){ +if($workflow->nid){ +$node=node_load($workflow->nid); +$workflow->node_title=$node->title; +} +$workflows[]=$workflow; +} + +return$workflows; +} + +functiontripal_galaxy_init_submission($workflow,$user){ +$transaction=db_transaction(); + +try{ +$data=[ +'galaxy_workflow_id'=>$workflow->galaxy_workflow_id, +'errors'=>'', +'submit_date'=>REQUEST_TIME, +'status'=>'Waiting', +'uid'=>$user->uid, +]; + +//Nowinsertthesubmissionintothedatabase. +$sid=db_insert('tripal_galaxy_workflow_submission')->fields($data)->execute(); + +return$sid; +} +catch(Exception$e){ +$transaction->rollback(); +tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'UnabletocreateGalaxyworkflowsubmission'); +returnFALSE; +} +} + +functiontripal_galaxy_get_submission(int$sid){ +$query=db_select('tripal_galaxy_workflow_submission','tgws'); +$query->fields('tgws'); +$query->join('tripal_galaxy_workflow','tgw','tgw.galaxy_workflow_id=tgws.galaxy_workflow_id'); +$query->fields('tgw',[ +'galaxy_id', +'workflow_id', +'nid', +'workflow_name', +]); +//Doaleft-joinonthenodetogetthetitleforthewebformbutin +//theeventthissubmissiondidnotuseawebformtheleftjoinwill +//proivdeaNULLvalueforthewebformtitle. +$query->leftJoin('node','n','(n.nid=tgw.nid)'); +$query->addField('n','title','webform_title'); +$query->condition('tgws.sid',$sid); +$submission=$query->execute()->fetchObject(); +if(!$submission){ +returnNULL; +} +$submission->errors=!empty($submission->errors)?unserialize($submission->errors):[]; + +//Nowaddtheworkflowdetails: +$workflows=tripal_galaxy_get_workflows(['id'=>$submission->galaxy_workflow_id]); +$workflow=$workflows[0]; +$submission->workflow=$workflow; +return$submission; +} + +functiontripal_galaxy_get_galaxies(){ +$galaxies=[]; +$results=db_select('tripal_galaxy','tg')->fields('tg')->execute(); + +while($galaxy=$results->fetchObject()){ +$galaxies[]=$galaxy; +} +return$galaxies; +} + +functiontripal_galaxy_get_galaxy(int$galaxy_id){ +if(!$galaxy_id){ +tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'tripal_galaxy_get_galaxy:missingrequiredfield"$galaxy_id"'); +returnFALSE; +} + +$galaxies=[]; +$galaxy=db_select('tripal_galaxy','tg')->fields('tg') +->condition('galaxy_id',$galaxy_id) +->execute() +->fetchObject(); + +return$galaxy; +} + +functiontripal_galaxy_add_galaxy(array$values){ +$required_args=[ +'servername', +'url', +'username', +'api_key', +'uid', +]; +foreach($required_argsas$key){ +if(!array_key_exists($key,$values)){ +tripal_report_error('tripal_galaxy',TRIPAL_ERROR,'tripal_galaxy_add_galaxy:missingrequiredfield"'.$key.'"'); +returnFALSE; +} +} + +//Theservernamecan'talreadyexist. +$exists=db_select('tripal_galaxy','tg')->fields('tg') +->condition('servername',$values['servername']) +->execute() +->fetchObject(); +if($exists){ +return$exists; +} + +//Createtheelement. +$data=array( +'servername'=>$values['servername'], +'description'=>$values['description'], +'url'=>$values['url'], +'username'=>$values['username'], +'api_key'=>$values['api_key'], +'uid'=>$values['uid'], +); +drupal_write_record('tripal_galaxy',$data); + +returntripal_galaxy_get_galaxy($data['galaxy_id']); +} + +functiontripal_galaxy_get_active_submissions(){ +$query=db_select('tripal_galaxy_workflow_submission','tgws'); +$query->fields('tgws',[ +'invocation_id', +'galaxy_workflow_id', +'sid', +'submit_date', +'status', +'email', +'uid', +'errors', ]); $query->join('tripal_galaxy_workflow','tgw','tgw.galaxy_workflow_id=tgws.galaxy_workflow_id'); -$query->fields('tgw',[ -'galaxy_id', -'workflow_id', -'nid', -]); -$query->leftJoin('node','n','(n.nid=tgw.nid)'); -$query->addField('n','title','webform_title'); -$query->condition('tgws.status',['Error','Completed'],'NOTIN'); -$results=$query->execute(); - -$submissions=[]; -while($submission=$results->fetchObject()){ -$submissions[]=$submission; -} -return$submissions; -} - -functiontripal_galaxy_check_submission_status(int$sid=NULL,bool$force=FALSE){ -$submissions=[]; -if(!$sid){ -$submissions=tripal_galaxy_get_active_submissions(); +$query->fields('tgw',['galaxy_id','workflow_id','nid']); +$query->leftJoin('node','n','(n.nid=tgw.nid)'); +$query->addField('n','title','webform_title'); +$query->condition('tgws.status',['Error','Completed','Deleted'],'NOTIN'); +$results=$query->execute(); + +$submissions=[]; +while($submission=$results->fetchObject()){ +$submission->errors=$submission->errors?unserialize($submission->errors):[]; +$submissions[]=$submission; +} +return$submissions; +} + +functiontripal_galaxy_check_submission_status(int$sid=NULL,bool$force=FALSE){ +$submissions=[]; +if(!$sid){ +$submissions=tripal_galaxy_get_active_submissions(); +} +elseif($sid&&!is_numeric($sid)){ +thrownewException('tripal_galaxy_check_submission_status():The$sidargumentisnotnumeric'); } -elseif($sid&&!is_numeric($sid)){ -thrownewException('tripal_galaxy_check_submission_status():The$sidargumentisnotnumeric'); -} -elseif($sid){ -$submissions[]=tripal_galaxy_get_submission($sid); -} - +elseif($sid){ +$submission=tripal_galaxy_get_submission($sid); +if(!$submission){ +thrownewException('TheprovidedsubmissionIDdoesnotexist'); +} +$submissions[]=$submission; +} foreach($submissionsas$submission){ $status=''; @@ -1355,615 +1308,687 @@ if($submission->status=='Completed'and!$force){ continue; } - -//ConnecttotheGalaxyinstance. -$galaxy=tripal_galaxy_get_connection($submission->galaxy_id); -if(!$galaxy){ -$error=$galaxy->getError(); -drupal_set_message('CouldnotconnecttoGalaxyserver.'.$error['message'],'error'); -returnFALSE; +//Ifthejobisdeletedskipitunless$forceissettoTRUE. +if($submission->status=='Deleted'and!$force){ +continue; +} +//Thereisnosubmissiongonofurther,dangerWillRobinson. +if($submission===NULL){ +continue; } -//Gettheinvocationspecified. -$gworkflows=newGalaxyWorkflows($galaxy); -$invocation=$gworkflows->showInvocations([ -'workflow_id'=>$submission->workflow_id, -'invocation_id'=>$submission->invocation_id, -]); -if(!$invocation){ -thrownewException($galaxy->getErrorMessage()); -} -$end_time=NULL; -$update_time=date_create_from_format('Y-m-d*G:i:s.ue',$invocation['update_time'].'UTC'); - -//FindtheHistoryforthissubmission. -$history_name=tripal_galaxy_get_history_name($submission); -$history=tripal_galaxy_get_history($galaxy,$history_name); - -//Ifthehistorydoesn'texistthenitmusthavebeendeleted. -if(!$history){ -$status='Deleted'; -} - -//Checkstatedetailsforalljobs -//validstatenamesare:paused,ok,failed_metadata,upload, -//discarded,running,setting_metadata,error,queued,empty. -//if'ok'statehasvaluelargerthan0andallotherstates'valuesbeing -//0,thenthishistoryhascompletedsuccessfully.Wecansetthe$status= -//'Completed'. + +//ConnecttotheGalaxyinstance. +$galaxy=tripal_galaxy_get_connection($submission->galaxy_id); +if(!$galaxy){ +$error=$galaxy->getError(); +drupal_set_message('CouldnotconnecttoGalaxyserver.'.$error['message'],'error'); +returnFALSE; +} +//Gettheinvocationspecified. +$gworkflows=newGalaxyWorkflows($galaxy); +$invocation=$gworkflows->showInvocations([ +'workflow_id'=>$submission->workflow_id, +'invocation_id'=>$submission->invocation_id, +]); +if(!$invocation){ +thrownewException($galaxy->getErrorMessage()); +} +$end_time=NULL; +$update_time=date_create_from_format('Y-m-d*G:i:s.ue',$invocation['update_time'].'UTC'); + +//FindtheHistoryforthissubmission. +$invocation_info=[]; + +if($submission->errors){ +$history_name=$submission->errors['history']['name']; +if($history_name){ +$history=tripal_galaxy_get_history($galaxy,$history_name); -if($history){ -//Getgreaterdetailsaboutthishistory -$ghistories=newGalaxyHistories($galaxy); -$history_info=$ghistories->show([ -'history_id'=>$history['id'], -]); -$state_details=array_filter($history_info['state_details']); - -//Ifnojobsareinthestateof'paused','running',or'queued',the -//historyiscompleted. -if(!isset($state_details['paused'])and!isset($state_details['running'])and!isset($state_details['queued'])){ - -//Getthehistorycontentsforthishistory. -$ghistory_contents=newGalaxyHistoryContents($galaxy); -$history_contents=$ghistory_contents->index([ -'history_id'=>$history['id'], -]); - -//Getmoredetailsabouteachhistorycontentitem. -foreach($history_contentsas$index=>$history_content){ -$history_contents[$index]=$ghistory_contents->show([ -'id'=>$history_content['id'], +//Ifthehistorydoesn'texistthenitmusthavebeendeleted. +if(!$history){ +$status='Deleted'; +} + +//Checkstatedetailsforalljobs +//validstatenamesare:paused,ok,failed_metadata,upload, +//discarded,running,setting_metadata,error,queued,empty. +//if'ok'statehasvaluelargerthan0andallotherstates'valuesbeing +//0,thenthishistoryhascompletedsuccessfully.Wecansetthe$status= +//'Completed'. +if($history){ +//Getgreaterdetailsaboutthishistory +$ghistories=newGalaxyHistories($galaxy); +$history_info=$ghistories->show([ +'history_id'=>$history['id'], +]); +$state_details=array_filter($history_info['state_details']); + +//Getthehistorycontentsforthishistory. +$ghistory_contents=newGalaxyHistoryContents($galaxy); +$history_contents=$ghistory_contents->index([ 'history_id'=>$history['id'], ]); -switch($history_content['type']){ -case'file': -$params=[]; -$params['history_id']=$history['id']; -$params['url_only']=TRUE; -$params['history_content_id']=$history_content['id']; -$link=$ghistory_contents->download_history_content($params); -$history_contents[$index]['content_link']=$link; -break; - -default: -break; -} -} - -$invocation_info['history']=$history; -$invocation_info['history_contents']=$history_contents; -$invocation_info['history_info']=$history_info; -if(isset($state_details['error'])){ -$status='Error'; -} -else{ -$status='Completed'; -$end_time=$update_time->getTimestamp(); -} -} -else{ -$status='Processing'; -} -} + +//Getmoredetailsabouteachhistorycontentitem. +foreach($history_contentsas$index=>$history_content){ +$history_contents[$index]=$ghistory_contents->show([ +'id'=>$history_content['id'], +'history_id'=>$history['id'], +]); +switch($history_content['type']){ +case'file': +$params=[]; +$params['history_id']=$history['id']; +$params['url_only']=TRUE; +$params['history_content_id']=$history_content['id']; +$link=$ghistory_contents->download_history_content($params); +$history_contents[$index]['content_link']=$link; +break; + +default: +break; +} +$job_id=$history_contents[$index]['creating_job']; +$gJobs=newGalaxyJobs($galaxy); +$job=$gJobs->show(['job_id'=>$job_id,'full'=>'TRUE']); +if(!$job){ +$error=$galaxy->getError(); +drupal_set_message('Couldnotretreivedetailsaboutthesubmittingjob.'.$error['message'],'error'); +returnFALSE; +} +$history_contents[$index]['creating_job']=$job; +} -//Nowinformtheuserthatthejobisdone! -if(!$force&&$submission->email!='SENT'){ -tripal_galaxy_send_submission_ended_mail($submission->sid,$node->uid); -} +//Saveallthishistoryinformationsowecancahceit. +$invocation_info['history']=$history; +$invocation_info['history_info']=$history_info; +$invocation_info['history_contents']=$history_contents; -$fields=[ -'status'=>$status, -'errors'=>serialize($invocation_info), -]; +//Ifanyofthejobshaveastateoferrorthenthat'sit. +if(isset($state_details['error'])){ +$status='Error'; +} -if($end_time!=NULL){ -$fields['end_time']=$end_time; -} -db_update('tripal_galaxy_workflow_submission') -->fields($fields) -->condition('sid',$submission->sid) -->execute(); -} - -returnTRUE; -} - -functiontripal_galaxy_get_history_name($submission){ -return"TG-".$submission->uid."-".$submission->galaxy_workflow_id."-".$submission->sid.'-'.date('Y_m_d_H:i:s',$submission->submit_date); -} - -functiontripal_galaxy_get_workflow_defaults(GalaxyInstance$galaxy,int$workflow_id){ -$gworkflows=newGalaxyWorkflows($galaxy); -$workflow=$gworkflows->show([ -'workflow_id'=>$workflow_id, -]); - -if(!$workflow){ -$error=$galaxy->getError(); -if(empty($error['message'])){ -$error['message']='NoworkflowisfoundinthisGalaxyserverfortheuseryouused -toconnecttotheGalaxyserver.'; -} -drupal_set_message($error['message'],'error'); -returnFALSE; -} - -$parameters=[]; -foreach($workflow['steps']as$step=>$details){ -$step_inputs=$details['input_steps']; -$step_input_key=''; -_tripal_galaxy_get_workflow_defaults($details['tool_inputs'],$parameters[$step],$step_inputs,$step_input_key); -} - -return$parameters; -} - -function_tripal_galaxy_get_workflow_defaults(array$tool_inputs,array&$parameters,array$step_inputs,string$step_input_key){ -foreach($tool_inputsas$key=>$value){ - -//SkipkeysthatarejustinternaltoGalaxy. -if(preg_match('/^__/',$key)){ -continue; -} - -$this_step_input_key=$step_input_key.$key; - -//Ifthisisanarraythenwewanttorecurse. -if(is_array($value)){ -$parameters[$key]=[]; -_tripal_galaxy_get_workflow_defaults($value,$parameters[$key],$step_inputs,$this_step_input_key.'|'); -if(empty($parameters[$key])){ -if(is_array($step_inputs)andarray_key_exists($this_step_input_key,$step_inputs)){ -$parameters[$key]=$step_inputs[$this_step_input_key]; -} -else{ -$parameters[$key]=''; -} -} -} -//Otherwisejustsetthevalue. -else{ -//Ifthiskeyisaninputfromanothertoolweneedtogetthat -//information. -if(is_array($step_inputs)andarray_key_exists($this_step_input_key,$step_inputs)){ -$parameters[$key]=$step_inputs[$this_step_input_key]; -} -else{ -$parameters[$key]=$value; -} -} -} -} - -//phpcs:disableDrupal.Commenting.DocCommentAlignment.SpaceAfterStar -//phpcs:disableDrupal.Commenting.DocComment.TagGroupSpacing -//phpcs:disableDrupal.Commenting.DocComment.TagsNotGrouped - -functiontripal_galaxy_invoke_workflow(GalaxyInstance$galaxy,$submission,array$parameters,array$inputs,array$history){ -$history_id=$history['id']; -//Createsomehandyvairablesforworkingwiththissubmission. -$workflow_id=$submission->workflow_id; -$uid=$submission->uid; -$sid=$submission->sid; - -//Invoketheworkflowandcheckforerrors. -$gworkflows=newGalaxyWorkflows($galaxy); -$params=[ -'workflow_id'=>$workflow_id, -'parameters'=>$parameters, -'inputs'=>$inputs, -'history_id'=>$history_id, -]; - -$invocation=$gworkflows->invoke($params); - -if(!$invocation){ -$error=$galaxy->getError(); -$error_msg='ERROR:'.$error['message']; +//Ifnojobsareinthestateof'paused','running',or'queued',the +//historyiscompleted. +elseif( +!isset($state_details['paused'])and!isset($state_details['running'])and +!isset($state_details['queued'])and!isset($state_details['new']) +){ +$status='Completed'; +$end_time=$update_time->getTimestamp(); +} + +//Forallotherjobstateswe'restillprocessing. +else{ +$status='Processing'; +} +} +} +} +//Nowinformtheuserthatthejobisdone! +if(!$force&&$submission->email!='SENT'){ +tripal_galaxy_send_submission_ended_mail($submission->sid); +} + +if(!empty($invocation_info)){ +$fields=[ +'status'=>$status, +'errors'=>serialize($invocation_info), +]; +} +else{ +$fields=[ +'status'=>$status, +'errors'=>serialize('Noinnovationinfo.'), +]; +} + + +//Weshouldneversetahistorytohaveanemptystatus.Ifthis +//happensweneedtofixthecodeabove! +if(!$status){ +thrownewException("Cannotsetemptystatusforsubmission:".$submission->sid); +} + +if($end_time!=NULL){ +$fields['end_time']=$end_time; +} +db_update('tripal_galaxy_workflow_submission') +->fields($fields) +->condition('sid',$submission->sid) +->execute(); +} +returnTRUE; +} + +functiontripal_galaxy_get_history_name($submission){ + +if(!$submission){ +thrownewException('Pleaseprovideavalidsubmissionobject.'); +} +if(!property_exists($submission,'galaxy_workflow_id')){ +thrownewException('Thesubmissionismissingthe"galaxy_workflow_id"property'); +} +if(!property_exists($submission,'uid')){ +thrownewException('Thesubmissionismissingthe"uid"property'); +} +if(!property_exists($submission,'sid')){ +thrownewException('Thesubmissionismissingthe"sid"property'); +} +if(!property_exists($submission,'submit_date')){ +thrownewException('Thesubmissionismissingthe"sid"property'); +} + +return"TG-".$submission->uid."-".$submission->galaxy_workflow_id."-".$submission->sid.'-'.date('Y_m_d_H:i:s',$submission->submit_date); +} + +functiontripal_galaxy_get_user_submissions(int$userid){ +$submissions=db_select('tripal_galaxy_workflow_submission','tgws') +->fields('tgws') +->condition('uid',$userid,'=') +->execute(); +while($submission=$submissions->fetchObject()){ +$sids[]=$submission->sid; +} +return$sids; +} + +functiontripal_galaxy_get_workflow_defaults(GalaxyInstance$galaxy,string$workflow_id){ +$gworkflows=newGalaxyWorkflows($galaxy); +$workflow=$gworkflows->show([ +'workflow_id'=>$workflow_id, +]); + +if(!$workflow){ +$error=$galaxy->getError(); +if(empty($error['message'])){ +$error['message']='NoworkflowisfoundinthisGalaxyserverfortheuseryouused +toconnecttotheGalaxyserver.'; +} +drupal_set_message($error['message'],'error'); +returnFALSE; +} + +$parameters=[]; +foreach($workflow['steps']as$step=>$details){ +$parameters[$step]=[]; +$step_inputs=$details['input_steps']; +$step_input_key=''; +_tripal_galaxy_get_workflow_defaults($details['tool_inputs'],$parameters[$step],$step_inputs,$step_input_key); +} + +return$parameters; +} + +function_tripal_galaxy_get_workflow_defaults(array$tool_inputs,array&$parameters,array$step_inputs,string$step_input_key){ +foreach($tool_inputsas$key=>$value){ + +//SkipkeysthatarejustinternaltoGalaxy. +if(preg_match('/^__/',$key)){ +continue; +} + +$this_step_input_key=$step_input_key.$key; + +//Ifthisisanarraythenwewanttorecurse. +if(is_array($value)){ +$parameters[$key]=[]; +_tripal_galaxy_get_workflow_defaults($value,$parameters[$key],$step_inputs,$this_step_input_key.'|'); +if(empty($parameters[$key])){ +if(is_array($step_inputs)andarray_key_exists($this_step_input_key,$step_inputs)){ +$parameters[$key]=$step_inputs[$this_step_input_key]; +} +else{ +$parameters[$key]=''; +} +} +} +//Otherwisejustsetthevalue. +else{ +//Ifthiskeyisaninputfromanothertoolweneedtogetthat +//information. +if(is_array($step_inputs)andarray_key_exists($this_step_input_key,$step_inputs)){ +$parameters[$key]=$step_inputs[$this_step_input_key]; +} +else{ +$parameters[$key]=$value; +} +} +} +} -//Ifthemessageisanarraythanwe'llconvertitintoastring. -if(is_array($error['message'])){ -$error_msg='ERROR:'; -foreach($error['message']as$key=>$value){ -$error_msg.=$key.':'.$value; -} -} -drupal_set_message($error_msg,'error'); -db_update('tripal_galaxy_workflow_submission')->fields([ -'status'=>'Error', -'errors'=>$error_msg, -]) -->condition('sid',$sid) -->execute(); -tripal_galaxy_send_submission_failed_mail($sid); -} -else{ -db_update('tripal_galaxy_workflow_submission')->fields([ -'status'=>'Submitted', -'start_time'=>REQUEST_TIME, -'end_time'=>NULL, -'invocation_id'=>$invocation['id'], -]) -->condition('sid',$sid) -->execute(); -tripal_galaxy_send_submission_start_mail($sid); -} -} - -//phpcs:enableDrupal.Commenting.DocComment.TagGroupSpacing -//phpcs:enableDrupal.Commenting.DocCommentAlignment.SpaceAfterStar -//phpcs:enableDrupal.Commenting.DocComment.TagsNotGrouped - -functiontripal_galaxy_upload_file($galaxy,int$fid,string$history_id,array$history_contents){ -if(!$fid){ -thrownewException('Cannotuploadafilewithoutanfid'); -} - -$file=file_load($fid); -$uploaded_file=NULL; - -foreach($history_contentsas$hfile){ -if(!$hfile['deleted']and$hfile['state']=='ok'and$hfile['name']==$file->filename){ -$uploaded_file=$hfile; -} +//phpcs:disableDrupal.Commenting.DocCommentAlignment.SpaceAfterStar +//phpcs:disableDrupal.Commenting.DocComment.TagGroupSpacing +//phpcs:disableDrupal.Commenting.DocComment.TagsNotGrouped + +functiontripal_galaxy_invoke_workflow(GalaxyInstance$galaxy,$submission,array$parameters,array$inputs,array$history){ + +//Makesurewehavethecorrectinputs +if(!$submission){ +thrownewException('Pleaseprovideavalidsubmissionargument.'); +} +if(!property_exists($submission,'sid')){ +thrownewException('Thesubmissionargumentismissingthe"sid"property.'); } - -//Onlyuploadthefileifitisn'talreadythere. -if(!$uploaded_file){ -$file_list=[]; -$file_list[]=[ -'name'=>$file->filename, -'path'=>drupal_realpath($file->uri), -]; -//Nowuploadthefiles. -$report="Uploading$file->filename..."; -if(is_object($job)){ -$job->logMessage($report); -} -else{ -print$report."\n"; -} -$gtool=newGalaxyTools($galaxy); -$tool=$gtool->create([ -'tool_id'=>'upload1', -'history_id'=>$history_id, -'files'=>$file_list, -]); -if(!$tool){ -$error=$galaxy->getError(); -thrownewException($error['message']); -} -return$tool['outputs'][0]; -} -else{ -$report="Filealreadyexistsinhistory:$file->filename..."; -if(is_object($job)){ -$job->logMessage($report); -} -else{ -print$report."\n"; -} -return$uploaded_file; -} -} - -functiontripal_galaxy_get_history(GalaxyInstance$galaxy,string$history_name){ - -$ghistories=newGalaxyHistories($galaxy); - -//Lookthroughexistinghistoriestofindwhatwe'relookingfor. -$histories=$ghistories->index(); -if(!$histories){ -thrownewException($error['message']); -} -foreach($historiesas$history){ -if($history['name']==$history_name){ -return$history; -} -} -returnFALSE; -} - -functiontripal_galaxy_create_history(GalaxyInstance$galaxy,string$history_name){ -$ghistories=newGalaxyHistories($galaxy); - -//Lookthroughexistinghistoriestofindwhatwe'relookingfor. -$histories=$ghistories->index(); -if(!$histories){ -$error=$galaxy->getError(); -thrownewException($error['message']); -} -foreach($historiesas$history){ -if($history['name']==$history_name){ -return$history; -} -} - -//Ifwe'reherethenthehistorydoesn'texist,socreateone. -$history=$ghistories->create([ -'name'=>$history_name, -]); -if(!$history){ -thrownewException($error['message']); -} -return$history; -} - -functiontripal_galaxy_test_connection(array$connect){ -$library=libraries_load('blend4php'); -if(array_key_exists('error',$library)){ -drupal_set_message($library['errormessage'],'error'); -returnFALSE; -} - -if(array_key_exists('galaxy_id',$connect)){ -$galaxy_id=$connect['galaxy_id']; -$galaxy=tripal_galaxy_get_connection($galaxy_id); -$error=$galaxy->getError(); -if($error['message']){ -drupal_set_message('Couldnotconnect:'.$error['message'],'error'); -returnFALSE; -} -} -else{ -//Createanewgalaxyinstanceusingtheobtainedhostnameandport -//ThenAuthenticate. -$galaxy=newGalaxyInstance($connect['host'],$connect['port'],$connect['use_https']); -$error=$galaxy->getError(); -if($error['message']){ -drupal_set_message('Couldnotconnect:'.$error['message'],'error'); -returnFALSE; -} -} - -//Telltheuserwhethertheconnectionwassuccessfulbasedon -//getVersion() -$version=$galaxy->getVersion(); -if($version==NULL){ -$error=$galaxy->getError(); -drupal_set_message('Couldnotconnect:'.$error['message'],'error'); -returnFALSE; -} -else{ -drupal_set_message(t('SuccessfulconnectiontotheGalaxyserver(version%version)',[ -'%version'=>$version['version_major'], -])); +if(!$history){ +thrownewException('Pleaseprovideavalidhistoryargument.'); +} +if(!array_key_exists('name',$history)){ +thrownewException('Thehistoryargumentismissingthe"sid"property.'); +} + +$history_id=$history['id']; +//Createsomehandyvariablesforworkingwiththissubmission. +$workflow_id=$submission->workflow_id; +$uid=$submission->uid; +$sid=$submission->sid; + +//Invoketheworkflowandcheckforerrors. +$gworkflows=newGalaxyWorkflows($galaxy); +$params=[ +'workflow_id'=>$workflow_id, +'parameters'=>$parameters, +'inputs'=>$inputs, +'history_id'=>$history_id, +]; + +$invocation=$gworkflows->invoke($params); +if(!$invocation){ +$error=$galaxy->getError(); +$error_msg='ERROR:'.$error['message']; + +//Ifthemessageisanarraythanwe'llconvertitintoastring. +if(is_array($error['message'])){ +$error_msg='ERROR:'; +foreach($error['message']as$key=>$value){ +$error_msg.=$key.':'.$value; +} +} +drupal_set_message($error_msg,'error'); +db_update('tripal_galaxy_workflow_submission')->fields([ +'status'=>'Error', +'errors'=>serialize([ +'history'=>$history, +'history_contents'=>'', +'history_info'=>'', +'error'=>$error_msg]) +]) +->condition('sid',$sid) +->execute(); +tripal_galaxy_send_submission_failed_mail($sid); +} +else{ +db_update('tripal_galaxy_workflow_submission')->fields([ +'status'=>'Submitted', +'start_time'=>REQUEST_TIME, +'end_time'=>NULL, +'invocation_id'=>$invocation['id'], +'errors'=>serialize([ +'history'=>$history, +'history_contents'=>'', +'history_info'=>'', +'error'=>'']), +]) +->condition('sid',$sid) +->execute(); +tripal_galaxy_send_submission_start_mail($sid); +} +} + +//phpcs:enableDrupal.Commenting.DocComment.TagGroupSpacing +//phpcs:enableDrupal.Commenting.DocCommentAlignment.SpaceAfterStar +//phpcs:enableDrupal.Commenting.DocComment.TagsNotGrouped + +functiontripal_galaxy_upload_file($galaxy,int$fid,string$history_id,array$history_contents){ +if(!$fid){ +thrownewException('Cannotuploadafilewithoutanfid'); +} + +$file=file_load($fid); +$uploaded_file=NULL; + +foreach($history_contentsas$hfile){ +if(!$hfile['deleted']and$hfile['state']=='ok'and$hfile['name']==$file->filename){ +$uploaded_file=$hfile; +} +} + +//Onlyuploadthefileifitisn'talreadythere. +if(!$uploaded_file){ +$file_list=[]; +$file_list[]=[ +'name'=>$file->filename, +'path'=>drupal_realpath($file->uri), +]; +//Nowuploadthefiles. +$report="Uploading$file->filename..."; +if(is_object($job)){ +$job->logMessage($report); +} +else{ +print$report."\n"; +} +$gtool=newGalaxyTools($galaxy); +$tool=$gtool->create([ +'tool_id'=>'upload1', +'history_id'=>$history_id, +'files'=>$file_list, +]); +if(!$tool){ +$error=$galaxy->getError(); +thrownewException($error['message']); +} +return$tool['outputs'][0]; +} +else{ +$report="Filealreadyexistsinhistory:$file->filename..."; +if(is_object($job)){ +$job->logMessage($report); +} +else{ +print$report."\n"; +} +return$uploaded_file; +} +} + +functiontripal_galaxy_get_history(GalaxyInstance$galaxy,string$history_name){ + +$ghistories=newGalaxyHistories($galaxy); + +//Lookthroughexistinghistoriestofindwhatwe'relookingfor. +$histories=$ghistories->index(); +if(!$histories){ +thrownewException($error['message']); } -returnTRUE; -} - -functiontripal_galaxy_get_files_dir(){ -global$user; - -$user_uid=md5($user->uid); - -$site_dir='public://tripal/files/galaxy/'.$user_uid; -if(!file_prepare_directory($site_dir,FILE_CREATE_DIRECTORY)){ -$message='Couldnotaccessthedirectoryontheserverforstoringthisfile.'; -watchdog('tripal',$message,[],WATCHDOG_ERROR); -returnFALSE; -} - -return$site_dir; -} - -functiontripal_galaxy_delete_expired_histories(){ - -//Calculatethetimestampforhowoldahistoryhastobeinordertoget -//deleted.Thedefaultis60days. -//60secs*60mins*24hours*xdays==totalseconds. -$history_days_limit=variable_get('tripal_galaxy_history_days_limit',60)*60*60*24; -$history_max_age_date=time()-$history_days_limit; - -//Findworkflowsthatareasolderorolderthanourmaxhistoryage. -$old_workflows=db_select('tripal_galaxy_workflow_submission','tgws') -->fields('tgws') -->condition('start_time',$history_max_age_date,'<') -->condition('status','Deleted','!=') -->execute(); -while($old_workflow=$old_workflows->fetchObject()){ - -//Skipalreadydeletedworkflowinvocations. -if($old_workflow->status=='Deleted'){ -continue; -} - -$submission=tripal_galaxy_get_submission($old_workflow->sid); -$history_name=tripal_galaxy_get_history_name($submission); -$success=tripal_galaxy_delete_remote_history($submission->galaxy_id,$history_name); - -//Disabletheworkflowonthesiteifhistorydeletionwassuccessful. -if($success){ -drupal_set_message('Successfullydeletedworkflowinvocation:'.$old_workflow->invocation_id); -db_update('tripal_galaxy_workflow_submission') -->fields(array( -'status'=>'Deleted', -)) -->condition('invocation_id',$old_workflow->invocation_id) -->execute(); -} -else{ -drupal_set_message('Failedtodeletedhistoryforworkflowinvocation:'.$old_workflow->invocation_id,'error'); +foreach($historiesas$history){ +if($history['name']==$history_name){ +return$history; +} +} +returnFALSE; +} + +functiontripal_galaxy_create_history(GalaxyInstance$galaxy,string$history_name){ +$ghistories=newGalaxyHistories($galaxy); + +//Lookthroughexistinghistoriestofindwhatwe'relookingfor. +$histories=$ghistories->index(); +if(!is_array($histories)){ +$error=$galaxy->getError(); +thrownewException($error['message']); +} +foreach($historiesas$history){ +if($history['name']==$history_name){ +return$history; +} +} + +//Ifwe'reherethenthehistorydoesn'texist,socreateone. +$history=$ghistories->create([ +'name'=>$history_name, +]); +if(!$history){ +thrownewException($error['message']); +} +return$history; +} + +functiontripal_galaxy_test_connection(array$connect){ +$library=libraries_load('blend4php'); +if(array_key_exists('error',$library)){ +drupal_set_message($library['errormessage'],'error'); +returnFALSE; +} + +if(array_key_exists('galaxy_id',$connect)){ +$galaxy_id=$connect['galaxy_id']; +$galaxy=tripal_galaxy_get_connection($galaxy_id); +$error=$galaxy->getError(); +if($error['message']){ +drupal_set_message('Couldnotconnect:'.$error['message'],'error'); +returnFALSE; } } -} - -functiontripal_galaxy_delete_remote_history(int$galaxy_id,string$history_name){ -$error=[]; -try{ -$galaxy=tripal_galaxy_get_connection($galaxy_id); -$history=tripal_galaxy_get_history($galaxy,$history_name); -if(!$history){ -$error=$galaxy->getError(); -thrownewException("Cannotfindhistory,$history_name:".$error['message']); -} +else{ +//Createanewgalaxyinstanceusingtheobtainedhostnameandport +//ThenAuthenticate. +$galaxy=newGalaxyInstance($connect['host'],$connect['port'],$connect['use_https']); +$error=$galaxy->getError(); +if($error['message']){ +drupal_set_message('Couldnotconnect:'.$error['message'],'error'); +returnFALSE; +} +} + +//Telltheuserwhethertheconnectionwassuccessfulbasedon +//getVersion() +$version=$galaxy->getVersion(); +if($version==NULL){ +$error=$galaxy->getError(); +drupal_set_message('Couldnotconnect:'.$error['message'],'error'); +returnFALSE; +} +else{ +drupal_set_message(t('SuccessfulconnectiontotheGalaxyserver(version%version)',[ +'%version'=>$version['version_major'], +])); +} +returnTRUE; +} -$ghistories=newGalaxyHistories($galaxy); -$deleted=$ghistories->deleteHistory(array( -'history_id'=>$history['id'], -)); -if(!$deleted){ -$error=$galaxy->getError(); -thrownewException("Cannotdeletethehistory,$history_name:".$error['message']); -} -returnTRUE; -} -catch(Exception$e){ -drupal_set_message(t('Couldnotdeletetheremotehistory.Pleasecontactthewebsiteadministratortoreportthisissue.'),'error'); -watchdog_exception('tripal_galaxy',$e); -returnFALSE; -} -} +functiontripal_galaxy_get_files_dir(){ +global$user; + +$user_uid=md5($user->uid); -functiontripal_galaxy_get_datasets(stdClass$submission){ - -if(!$submission->status=='Completed'){ -thrownewException('Cannotretrieveresultsforaworkflowinvocationthathasnotyetcompleted.'); -} +$site_dir='public://tripal/files/galaxy/'.$user_uid; +if(!file_prepare_directory($site_dir,FILE_CREATE_DIRECTORY)){ +$message='Couldnotaccessthedirectoryontheserverforstoringthisfile.'; +watchdog('tripal',$message,[],WATCHDOG_ERROR); +returnFALSE; +} + +return$site_dir; +} + +functiontripal_galaxy_delete_expired_histories(){ + +//Calculatethetimestampforhowoldahistoryhastobeinordertoget +//deleted.Thedefaultis60days. +//60secs*60mins*24hours*xdays==totalseconds. +$history_days_limit=variable_get('tripal_galaxy_history_days_limit',60)*60*60*24; +$history_max_age_date=time()-$history_days_limit; -//ConnecttotheGalaxyserverandgetthehistory. -$galaxy_id=$submission->workflow->galaxy_id; -$galaxy=tripal_galaxy_get_connection($galaxy_id); -$history_name=$submission->errors['history']['name']; -$history=tripal_galaxy_get_history($galaxy,$history_name); -if(!$history){ -$error=$galaxy->getError(); -thrownewException("Cannotfindhistory,$history_name:".$error['message']); -} - -//Getthehistorycontents -$ghistory_contents=newGalaxyHistoryContents($galaxy); -$history_contents=$ghistory_contents->index([ -'history_id'=>$history['id'], -]); -if(!$history_contents){ -$error=$galaxy->getError(); -thrownewException("Cannotfindthehistorycontentswithname'$history_name':".$error['message']); -} - -//Iteratethroughthehistorycontentsandgetalistofdatasets. -$datasets=[]; -foreach($history_contentsas$entry){ -$dataset_id=$entry['dataset_id']; - -//Gettherecordforthedataset. -$gdataset=newGalaxyDatasets($galaxy); -$dataset=$gdataset->show(['dataset_id'=>$dataset_id]); -if(!$dataset){ -$error=$galaxy->getError(); -thrownewException("CannotfindthedatasetwithID'$dataset_id':".$error['message']); -} +//Findworkflowsthatareasolderorolderthanourmaxhistoryage. +$old_workflows=db_select('tripal_galaxy_workflow_submission','tgws') +->fields('tgws') +->condition('start_time',$history_max_age_date,'<') +->condition('status','Deleted','!=') +->execute(); +while($old_workflow=$old_workflows->fetchObject()){ + +//Skipalreadydeletedworkflowinvocations. +if($old_workflow->status=='Deleted'){ +continue; +} + +$submission=tripal_galaxy_get_submission($old_workflow->sid); +$history_name=tripal_galaxy_get_history_name($submission); +$success=tripal_galaxy_delete_remote_history($submission->galaxy_id,$history_name); + +//Disabletheworkflowonthesiteifhistorydeletionwassuccessful. +if($success){ +drupal_set_message('Successfullydeletedworkflowinvocation:'.$old_workflow->invocation_id); +db_update('tripal_galaxy_workflow_submission') +->fields(array( +'status'=>'Deleted', +)) +->condition('invocation_id',$old_workflow->invocation_id) +->execute(); +} +else{ +drupal_set_message('Failedtodeletedhistoryforworkflowinvocation:'.$old_workflow->invocation_id,'error'); +} +} +} -//Generateafilenameforthisfile. -$file_name=preg_replace('/[^\w]/','_',$dataset['name']).'.'.$dataset['file_ext']; -$datasets[]=$dataset; -} - -return$datasets; -} - -functiontripal_galaxy_get_dataset(stdClass$submission,$dataset_id){ -if(!$submission->status=='Completed'){ -thrownewException('Cannotretrieveresultsforaworkflowinvocationthathasnotyetcompleted.'); -} - -//ConnecttotheGalaxyserverandgetthehistory. -$galaxy_id=$submission->workflow->galaxy_id; -$galaxy=tripal_galaxy_get_connection($galaxy_id); - -//Gettherecordforthedataset. -$gdataset=newGalaxyDatasets($galaxy); -$dataset=$gdataset->show(['dataset_id'=>$dataset_id]); -if(!$dataset){ -$error=$galaxy->getError(); -thrownewException("CannotfindthedatasetwithID'$dataset_id':".$error['message']); -} - -return$dataset; -} - - -functiontripal_galaxy_download_file(stdClass$submission,array$dataset,int$uid){ - -$user=user_load($uid); - -//ConnecttotheremoteGalaxyserver. -$galaxy_id=$submission->workflow->galaxy_id; -$galaxy=tripal_galaxy_get_connection($galaxy_id); - -//Generatethefile_nameandURLfordownloading. -$file_name=preg_replace('/[^\w]/','_',$dataset['name']).'.'.$dataset['file_ext']; -$url=$galaxy->getURL().$dataset['download_url']; - -//Nowsavethefile -$history_name=$submission->errors['history']['name']; -$destination=tripal_get_user_files_dir($user).'/analyses/'.$history_name; -file_prepare_directory($destination,FILE_CREATE_DIRECTORY); -$file_uri=$destination.'/'.$file_name; -$url_fh=fopen($url,"r"); -if(!$url_fh){ -thrownewException("Unabletodownloadthefileat$url."); -} -$fh=fopen($file_uri,"w"); -if(!$url_fh){ -thrownewException("Unabletosavefile$file_name."); -} -while(!feof($url_fh)){ -fwrite($fh,fread($url_fh,255),255); -} -fclose($url_fh); -fclose($fh); - -//Seeifthisfileisalreadymanagedifso,thenithasbeenuploaded -//beforeandwedon'tneedtoaddamanageditemagain. -$fid=db_select('file_managed','fm') -->fields('fm',['fid']) -->condition('uri',$file_uri) -->execute() -->fetchField(); - -//NowregisterthefilewithDrupal -if(!$fid){ -$file=newstdClass(); -$file->uri=$file_uri; -$file->filename=$file_name; -$file->filemime=file_get_mimetype($file_uri); -$file->uid=$user->uid; -$file->status=FILE_STATUS_PERMANENT; -$file=file_save($file); -} -else{ -$file=file_load($fid); -} - -//SetthefileasbeingmanagedbyTripalandTripalGalaxy -file_usage_add($file,'tripal','galaxy_dataset',0); -file_usage_add($file,'tripal_galaxy','galaxy_dataset',0); -return$file; -} - -functiontripal_galaxy_get_proxy_url(stdClass$submission,array$dataset,int$uid,string$action){ - -if(!in_array($action,['viewer','viewer-full','download'])){ -thrownewException('Pleaseindicatetheactionas:"viewer"or"download".'); -} - -$galaxy_id=$submission->workflow->galaxy_id; -$galaxy=tripal_galaxy_get_connection($galaxy_id); - -if(!array_key_exists('tripal_galaxy_proxy_urls',$_SESSION)){ -$_SESSION['tripal_galaxy_proxy_urls']=[]; -}; -$id=uniqid('TGPX',TRUE); -$_SESSION['tripal_galaxy_proxy_urls'][$id]['dataset']=$dataset; -$_SESSION['tripal_galaxy_proxy_urls'][$id]['sid']=$submission->sid; -$_SESSION['tripal_galaxy_proxy_urls'][$id]['uid']=$uid; +functiontripal_galaxy_delete_remote_history(int$galaxy_id,string$history_name){ +$error=[]; +try{ +$galaxy=tripal_galaxy_get_connection($galaxy_id); +$history=tripal_galaxy_get_history($galaxy,$history_name); +if(!$history){ +$error=$galaxy->getError(); +thrownewException("Cannotfindhistory,$history_name:".$error['message']); +} + +$ghistories=newGalaxyHistories($galaxy); +$deleted=$ghistories->deleteHistory(array( +'history_id'=>$history['id'], +)); +if(!$deleted){ +$error=$galaxy->getError(); +thrownewException("Cannotdeletethehistory,$history_name:".$error['message']); +} +returnTRUE; +} +catch(Exception$e){ +drupal_set_message(t('Couldnotdeletetheremotehistory.Pleasecontactthewebsiteadministratortoreportthisissue.'),'error'); +watchdog_exception('tripal_galaxy',$e); +returnFALSE; +} +} + +functiontripal_galaxy_get_datasets(stdClass$submission){ + +if(!$submission->status=='Completed'){ +thrownewException('Cannotretrieveresultsforaworkflowinvocationthathasnotyetcompleted.'); +} + +//ConnecttotheGalaxyserverandgetthehistory. +$galaxy_id=$submission->workflow->galaxy_id; +$galaxy=tripal_galaxy_get_connection($galaxy_id); +$history_name=$submission->errors['history']['name']; +$history=tripal_galaxy_get_history($galaxy,$history_name); +if(!$history){ +$error=$galaxy->getError(); +thrownewException("Cannotfindhistory,$history_name:".$error['message']); +} + +//Getthehistorycontents +$ghistory_contents=newGalaxyHistoryContents($galaxy); +$history_contents=$ghistory_contents->index([ +'history_id'=>$history['id'], +]); +if(!$history_contents){ +$error=$galaxy->getError(); +thrownewException("Cannotfindthehistorycontentswithname'$history_name':".$error['message']); +} + +//Iteratethroughthehistorycontentsandgetalistofdatasets. +$datasets=[]; +foreach($history_contentsas$entry){ +$dataset_id=$entry['dataset_id']; + +//Gettherecordforthedataset. +$gdataset=newGalaxyDatasets($galaxy); +$dataset=$gdataset->show(['dataset_id'=>$dataset_id]); +if(!$dataset){ +$error=$galaxy->getError(); +thrownewException("CannotfindthedatasetwithID'$dataset_id':".$error['message']); +} + +//Generateafilenameforthisfile. +$file_name=preg_replace('/[^\w]/','_',$dataset['name']).'.'.$dataset['file_ext']; +$datasets[]=$dataset; +} + +return$datasets; +} + +functiontripal_galaxy_get_dataset(stdClass$submission,$dataset_id){ +if(!$submission->status=='Completed'){ +thrownewException('Cannotretrieveresultsforaworkflowinvocationthathasnotyetcompleted.'); +} + +//ConnecttotheGalaxyserverandgetthehistory. +$galaxy_id=$submission->workflow->galaxy_id; +$galaxy=tripal_galaxy_get_connection($galaxy_id); + +//Gettherecordforthedataset. +$gdataset=newGalaxyDatasets($galaxy); +$dataset=$gdataset->show(['dataset_id'=>$dataset_id]); +if(!$dataset){ +$error=$galaxy->getError(); +thrownewException("CannotfindthedatasetwithID'$dataset_id':".$error['message']); +} + +return$dataset; +} + + +functiontripal_galaxy_download_file(stdClass$submission,array$dataset,int$uid){ + +$user=user_load($uid); -//Createthelink. -$url='galaxy/'.$action.'/'.$id; -return$url; -} +//ConnecttotheremoteGalaxyserver. +$galaxy_id=$submission->workflow->galaxy_id; +$galaxy=tripal_galaxy_get_connection($galaxy_id); + +//Generatethefile_nameandURLfordownloading. +$file_name=preg_replace('/[^\w]/','_',$dataset['name']).'.'.$dataset['file_ext']; +$url=$galaxy->getURL().$dataset['download_url']; + +//Nowsavethefile +$history_name=$submission->errors['history']['name']; +$destination=tripal_get_user_files_dir($user).'/analyses/'.$history_name; +file_prepare_directory($destination,FILE_CREATE_DIRECTORY); +$file_uri=$destination.'/'.$file_name; +$url_fh=fopen($url,"r"); +if(!$url_fh){ +thrownewException("Unabletodownloadthefileat$url."); +} +$fh=fopen($file_uri,"w"); +if(!$url_fh){ +thrownewException("Unabletosavefile$file_name."); +} +while(!feof($url_fh)){ +fwrite($fh,fread($url_fh,255),255); +} +fclose($url_fh); +fclose($fh); + +//Seeifthisfileisalreadymanagedifso,thenithasbeenuploaded +//beforeandwedon'tneedtoaddamanageditemagain. +$fid=db_select('file_managed','fm') +->fields('fm',['fid']) +->condition('uri',$file_uri) +->execute() +->fetchField(); + +//NowregisterthefilewithDrupal +if(!$fid){ +$file=newstdClass(); +$file->uri=$file_uri; +$file->filename=$file_name; +$file->filemime=file_get_mimetype($file_uri); +$file->uid=$user->uid; +$file->status=FILE_STATUS_PERMANENT; +$file=file_save($file); +} +else{ +$file=file_load($fid); +} + +//SetthefileasbeingmanagedbyTripalandTripalGalaxy +file_usage_add($file,'tripal','galaxy_dataset',0); +file_usage_add($file,'tripal_galaxy','galaxy_dataset',0); +return$file; +} + - + diff --git a/docs/doxyxml/dir_0188a3e6da905bc60aceb35bf790b8c9.xml b/docs/doxyxml/dir_0188a3e6da905bc60aceb35bf790b8c9.xml index 17ae517..14ed7d3 100644 --- a/docs/doxyxml/dir_0188a3e6da905bc60aceb35bf790b8c9.xml +++ b/docs/doxyxml/dir_0188a3e6da905bc60aceb35bf790b8c9.xml @@ -1,12 +1,12 @@ - /local/home/ficklin/Projects/tripal_galaxy/api + /local/Projects/tripal_galaxy/api tripal_galaxy.api.inc - + diff --git a/docs/doxyxml/index.xml b/docs/doxyxml/index.xml index e10d35c..759a817 100644 --- a/docs/doxyxml/index.xml +++ b/docs/doxyxml/index.xml @@ -1,13 +1,7 @@ - PharIo::Version::OrVersionConstraintGroup - - phpDocumentor::Reflection::Types::This - - TheSeer::Tokenizer::Token - tripal_galaxy.api.inc - _tripal_galaxy_get_workflow_defaults + _tripal_galaxy_get_workflow_defaults tripal_galaxy_add_galaxy tripal_galaxy_add_workflow tripal_galaxy_check_submission_status @@ -24,9 +18,9 @@ tripal_galaxy_get_galaxy tripal_galaxy_get_history tripal_galaxy_get_history_name - tripal_galaxy_get_proxy_url tripal_galaxy_get_submission - tripal_galaxy_get_workflow_defaults + tripal_galaxy_get_user_submissions + tripal_galaxy_get_workflow_defaults tripal_galaxy_get_workflows tripal_galaxy_init_submission tripal_galaxy_invoke_workflow @@ -35,7 +29,6 @@ tripal_galaxy_upload_file tripal_galaxy_api - _tripal_galaxy_get_workflow_defaults tripal_galaxy_add_galaxy tripal_galaxy_add_workflow tripal_galaxy_check_submission_status @@ -52,15 +45,15 @@ tripal_galaxy_get_galaxy tripal_galaxy_get_history tripal_galaxy_get_history_name - tripal_galaxy_get_proxy_url tripal_galaxy_get_submission - tripal_galaxy_get_workflow_defaults + tripal_galaxy_get_user_submissions + tripal_galaxy_get_workflow_defaults tripal_galaxy_get_workflows tripal_galaxy_init_submission tripal_galaxy_invoke_workflow tripal_galaxy_test_connection tripal_galaxy_upload_file - /local/home/ficklin/Projects/tripal_galaxy/api + /local/Projects/tripal_galaxy/api diff --git a/docs/workflow_settings.rst b/docs/workflow_settings.rst index d688422..a786a91 100644 --- a/docs/workflow_settings.rst +++ b/docs/workflow_settings.rst @@ -16,4 +16,4 @@ You can change the text of the subject line and the body of the email by alterin .. image:: workflow_settings.3.png -For example, if you want to include the submission time of the workflow in the body of the email that gets sent when the workflow is invoked, you should add the token ``[submission_init]``. When the email is sent this token will be replaced with the submission time. Be sure to clicke the ``Submit`` button after making any changes. \ No newline at end of file +For example, if you want to include the submission time of the workflow in the body of the email that gets sent when the workflow is invoked, you should add the token ``[submission_init]``. When the email is sent this token will be replaced with the submission time. Be sure to click the ``Submit`` button after making any changes. diff --git a/includes/tripal_galaxy.results.inc b/includes/tripal_galaxy.results.inc index baa0faa..8d60e57 100644 --- a/includes/tripal_galaxy.results.inc +++ b/includes/tripal_galaxy.results.inc @@ -15,21 +15,12 @@ function tripal_galaxy_workflow_report(int $sid) { return FALSE; } - // Create a galaxy logo clickable image. - $galaxy_logo = l('', "https://galaxyproject.org/", [ - 'html' => TRUE, - 'attributes' => [ - 'target' => '_blank', - ], - ]); - // Get the submission details. $submission = tripal_galaxy_get_submission($sid); if (!$submission) { return FALSE; } - $title = $submission->workflow_name; // If this a webform submission make sure the user has access to the // node that this workflow belongs to. @@ -56,6 +47,7 @@ function tripal_galaxy_workflow_report(int $sid) { $galaxy_instance = tripal_galaxy_get_connection($submission->galaxy_id); $history_name = tripal_galaxy_get_history_name($submission); $error = []; + $content = []; $history = tripal_galaxy_get_history($galaxy_instance, $history_name); if ($history['deleted'] === TRUE) { @@ -81,6 +73,171 @@ function tripal_galaxy_workflow_report(int $sid) { $files_query->condition('fu.type', 'submission'); $input_files = $files_query->execute()->fetchCol(); + + // Add a warning message at the top if there are errors. + if ($submission->status == 'Error') { + $content['status_message'] = [ + '#type' => 'markup', + '#markup' => '
' . t('This analysis failed. Check for errors in the subission details or in the error section if result files are present.') . '
', + ]; + } + + // Print the analysis name. + $content['analysis_name'] = [ + '#type' => 'item', + '#title' => 'Analysis Name', + '#markup' => $title, + ]; + + // Add in a feildset for the submission details. + $content['submission_details'] = tripal_galaxy_workflow_report_build_submission($submission, $galaxy); + + + // Add a section now for errors, but it will only get shown if there + // are errors. + $content['errors'] = [ + '#type' => 'fieldset', + '#title' => t('Errors'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#attributes' => [ + 'class' => [ + 'collapsible', + 'collapsed', + ], + ], + '#attached' => [ + 'js' => [ + 'misc/collapse.js', + 'misc/form.js', + ], + ], + ]; + + + // Get details about the galaxy server and the connection to Galaxy. + $galaxy = tripal_galaxy_get_connection($submission->galaxy_id); + + // This is just a placeholder for the results text. It will get fully + // set below. + $content['results'] = [ + '#type' => 'item', + ]; + + $errors = []; + $has_any_results = FALSE; + if (is_array($submission->errors)) { + + // If the workflow status is 'Completed' then there will be a history and + // history contents. We'll get those out and remove them so we + // can more easily iterate through the steps. + if (array_key_exists('history_contents', $submission->errors)) { + $history_contents = $submission->errors['history_contents']; + } + if (array_key_exists('history_info', $submission->errors)) { + $history_info = $submission->errors['history_info']; + } + + if (isset($history_contents) and is_array($history_contents) and + isset($history_info) and is_array($history_info)) { + + $has_any_results = TRUE; + foreach ($history_contents as $index => $history_content) { + + // Skip history content items that should be hidden. + if ($history_content['visible'] != TRUE) { + continue; + } + + // Don't show input files. + if (in_array($history_content['name'], $input_files)) { + continue; + } + + $content_id = 'history_content_' . $history_content['id'] . '_' . $index; + + // Handle Dataset Collections. + if ($history_content['history_content_type'] == 'dataset_collection') { + $content[$content_id] = tripal_galaxy_workflow_report_build_result_dataset_collection( + $history_content, $submission, $node, $galaxy); + } + // Handle Datasets. + else { + // If there are any errors on any file creation, then we'll add + // the error message to our file_errors list. + if ($history_content['state'] == 'error') { + $errors[] = [ + [ + 'data' => $history_content['name'], + 'header' => TRUE, + ], + '
' . $history_content['creating_job']['stderr'] . '
' + ]; + continue; + } + $content[$content_id] = tripal_galaxy_workflow_report_build_result_dataset($history_content, + $submission, $node, $galaxy); + } + } + } + } + + if (count($errors) > 0) { + $content['errors']['table'] = [ + '#type' => 'markup', + '#markup' => theme_table([ + 'header' => [], + 'rows' => $errors, + 'attributes' => [ + 'class' => 'tripal-galaxy-error-table' + ], + 'sticky' => FALSE, + 'caption' => '', + 'colgroups' => [], + 'empty' => '', + ]), + ]; + } + else { + unset($content['errors']); + } + + if ($has_any_results) { + $content['results'] = [ + '#type' => 'item', + '#title' => t('Result Files'), + '#markup' => t('The result files are listed below. Click the filename to view or download results. Expand the field for the result below to + view the status, peek or download result files.'), + ]; + } + else { + $content['results'] = [ + '#type' => 'item', + '#title' => t('Results'), + '#markup' => t('Currently, no results exist for this job. The current state of the job is: %state.', [ + '%state' => $submission->status, + ]), + ]; + } + + return $content; +} + +/** + * Builds the submission fieldset for the report page. + */ +function tripal_galaxy_workflow_report_build_submission($submission, $galaxy) { + + // Create a galaxy logo clickable image. + $galaxy_logo = l('', "https://galaxyproject.org/", [ + 'html' => TRUE, + 'attributes' => [ + 'target' => '_blank', + ], + ]); + + $title = $submission->workflow_name; + $headers = []; $submission_details = []; $submission_details[] = [ @@ -168,39 +325,26 @@ function tripal_galaxy_workflow_report(int $sid) { $galaxy->servername . '
' . $galaxy_logo, ]; - if ($submission->status == 'Error') { - $content['status_message'] = [ - '#type' => 'markup', - '#markup' => '
' . t('This analysis failed. Check for errors in the subission details or in the error section if result files are present.') . '
', - ]; - } - - $content['analysis_name'] = [ - '#type' => 'item', - '#title' => 'Analysis Name', - '#markup' => $title, - ]; - - $content['submission_details'] = [ - '#type' => 'fieldset', - '#title' => t('Submission Details'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, - '#attributes' => [ - 'class' => [ - 'collapsible', - 'collapsed', + $submission_section = [ + '#type' => 'fieldset', + '#title' => t('Submission Details'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#attributes' => [ + 'class' => [ + 'collapsible', + 'collapsed', + ], ], - ], - '#attached' => [ - 'js' => [ - 'misc/collapse.js', - 'misc/form.js', + '#attached' => [ + 'js' => [ + 'misc/collapse.js', + 'misc/form.js', + ], ], - ], ]; - $content['submission_details']['table'] = [ + $submission_section['table'] = [ '#type' => 'markup', '#markup' => theme_table([ 'header' => $headers, @@ -215,208 +359,115 @@ function tripal_galaxy_workflow_report(int $sid) { ]), ]; - - $content['errors'] = [ + return $submission_section; +} +/** + * Creates a result elemnt for a single file. + */ +function tripal_galaxy_workflow_report_build_result_dataset_collection($history_content, $submission, $node, $galaxy) { + $step_content = [ '#type' => 'fieldset', - '#title' => t('Errors'), - '#collapsible' => TRUE, + '#title' => $history_content['name'] . ' (data collection)', '#collapsed' => TRUE, + '#collapsible' => TRUE, '#attributes' => [ - 'class' => [ - 'collapsible', - 'collapsed', - ], + 'class' => ['collapsible', 'collapsed'], ], '#attached' => [ 'js' => [ - 'misc/collapse.js', - 'misc/form.js', + 'misc/collapse.js', + 'misc/form.js', ], ], ]; + foreach ($history_content['elements'] as $element) { + $step_content[$element['element_identifier']] = tripal_galaxy_workflow_report_build_result_dataset( + $element['contents'], $submission, $node, $galaxy); + } + return $step_content; +} +/** + * Creates a result elemnt for a single file. + */ +function tripal_galaxy_workflow_report_build_result_dataset($history_content, $submission, $node, $galaxy) { - // Get details about the galaxy server and the connection to Galaxy. - $galaxy_server = tripal_galaxy_get_galaxy($submission->galaxy_id); - $galaxy = tripal_galaxy_get_connection($submission->galaxy_id); - - // This is just a placeholder for the results text. It will get fully - // set below. - $content['results'] = [ - '#type' => 'item', + $step_content = [ + '#type' => 'fieldset', + '#title' => $history_content['name'], + '#collapsed' => TRUE, + '#collapsible' => TRUE, + '#attributes' => [ + 'class' => ['collapsible', 'collapsed'], + ], + '#attached' => [ + 'js' => [ + 'misc/collapse.js', + 'misc/form.js', + ], + ], ]; - $errors = []; - $has_any_results = FALSE; - if (is_array($submission->errors)) { - - // If the workflow status is 'Completed' then there will be a history and - // history contents. We'll get those out and remove them so we - // can more easily iterate through the steps. - if (array_key_exists('history_contents', $submission->errors)) { - $history_contents = $submission->errors['history_contents']; - } - if (array_key_exists('history_info', $submission->errors)) { - $history_info = $submission->errors['history_info']; + // If this is a file add viewing and download links + if ($history_content['type'] == 'file') { +// if (!isset($history_content['content_link'])) { +// continue; +// } + + // If this is an HTML file then add a view link. + if ($history_content['extension'] == 'html') { + $dataset_id = $history_content['id']; + $dataset = tripal_galaxy_get_dataset($submission, $dataset_id); + $proxy_url = tripal_galaxy_get_proxy_url($submission, $dataset, $node->uid, 'download'); + $step_content['html_view'] = [ + '#type' => 'item', + '#title' => t('Download web page report'), + '#markup' => l($history_content['name'], $proxy_url, ['attributes' => ['target' => '_blank']]), + ]; } - - if (isset($history_contents) and is_array($history_contents) and - isset($history_info) and is_array($history_info)) { - foreach ($history_contents as $index => $history_content) { - - // Skip history items that does not have 'ok' or 'error' state. - if (!in_array($history_content['id'], array_merge($history_info['state_ids']['ok'], $history_info['state_ids']['error']))) { - continue; - } - - // If there are any errors on any file creation, then we'll add - // the error message to tour file_errors list. - if ($history_content['state'] == 'error') { - $errors[] = [ - [ - 'data' => $history_content['name'], - 'header' => TRUE, - ], - '
' . $history_content['creating_job']['stderr'] . '
' - ]; - continue; - } - - // Skip history content items that should be hidden. - if ($history_content['visible'] != TRUE) { - continue; - } - - // Don't show input files. - if (in_array($history_content['name'], $input_files)) { - continue; - } - - $has_any_results = TRUE; - - $step_content = []; - $step_content['history_content_' . $history_content['id'] . '_' . $index] = [ - '#type' => 'fieldset', - '#title' => $history_content['name'], - '#collapsed' => TRUE, - '#collapsible' => TRUE, - '#attributes' => [ - 'class' => ['collapsible', 'collapsed'], - ], - '#attached' => [ - 'js' => [ - 'misc/collapse.js', - 'misc/form.js', - ], - ], + // If this is not an HTML file then provide link to view the file + // unless it exceeds 1MB. If Galaxy provides a peek then show that. + else { + $link = $history_content['content_link']; + $dataset_id = $history_content['id']; + $dataset = tripal_galaxy_get_dataset($submission, $dataset_id); + $proxy_url = tripal_galaxy_get_proxy_url($submission, $dataset, $node->uid, 'download'); + $file_size = tripal_format_bytes($history_content['file_size']); + $step_content['download_link'] = [ + '#type' => 'item', + '#title' => t('Download File'), + '#markup' => l($history_content['name'], $proxy_url) . ' (' . $file_size . ')', + ]; + + /* // Any files that are smaller than 1MB can be shown in a browser. + if ($history_content['file_size'] < pow(10, 6)) { + // SPF: originally this was $history_content['dataset_id'] but for + // some reason that value seems off for v17.09 as it does not + // point to the correct dataset. Rather the 'id' does. However, + // in v18.01 it seems fixed and both 'id' and 'dataset_id' are the + // same. + $dataset_id = $history_content['id']; + $dataset = tripal_galaxy_get_dataset($submission, $dataset_id); + $proxy_url = tripal_galaxy_get_proxy_url($submission, $dataset, $node->uid, 'viewer-full'); + $step_content['html_view'] = [ + '#type' => 'item', + '#title' => t('View File'), + '#markup' => l($history_content['name'], $proxy_url, ['attributes' => ['target' => '_blank']] ), + ]; + } */ + + // Provide a peek if one is provided. + if ($history_content['peek'] != NULL) { + $step_content['peek'] = [ + '#type' => 'item', + '#title' => 'Peek', + '#description' => 'A short view of the first few lines of the output file.', + '#markup' => '
' . $history_content['peek'] . '
', ]; - - // If this is a file add viewing and download links - if ($history_content['type'] == 'file') { - if (!isset($history_content['content_link'])) { - break; - } - - // If this is an HTML file then add a view link. - if ($history_content['extension'] == 'html') { - $dataset_id = $history_content['id']; - $dataset = tripal_galaxy_get_dataset($submission, $dataset_id); - $proxy_url = tripal_galaxy_get_proxy_url($submission, $dataset, $node->uid, 'download'); - $step_content['history_content_' . $history_content['id'] . '_' . $index]['html_view'] = [ - '#type' => 'item', - '#title' => t('Download web page report'), - '#markup' => l($history_content['name'], $proxy_url, ['attributes' => ['target' => '_blank']]), - ]; - } - // If this is not an HTML file then provide link to view the file - // unless it exceeds 1MB. If Galaxy provides a peek then show that. - else { - $link = $history_content['content_link']; - $dataset_id = $history_content['id']; - $dataset = tripal_galaxy_get_dataset($submission, $dataset_id); - $proxy_url = tripal_galaxy_get_proxy_url($submission, $dataset, $node->uid, 'download'); - $file_size = tripal_format_bytes($history_content['file_size']); - $step_content['history_content_' . $history_content['id'] . '_' . $index]['download_link'] = [ - '#type' => 'item', - '#title' => t('Download File'), - '#markup' => l($history_content['name'], $proxy_url) . ' (' . $file_size . ')', - ]; - - // Any files that are smaller than 1MB can be shown in a browser. - if ($history_content['file_size'] < pow(10, 6)) { - // SPF: originally this was $history_content['dataset_id'] but for - // some reason that value seems off for v17.09 as it does not - // point to the correct dataset. Rather the 'id' does. However, - // in v18.01 it seems fixed and both 'id' and 'dataset_id' are the - // same. - $dataset_id = $history_content['id']; - $dataset = tripal_galaxy_get_dataset($submission, $dataset_id); - $proxy_url = tripal_galaxy_get_proxy_url($submission, $dataset, $node->uid, 'viewer-full'); - $step_content['history_content_' . $history_content['id'] . '_' . $index]['html_view'] = [ - '#type' => 'item', - '#title' => t('View File'), - '#markup' => l($history_content['name'], $proxy_url, ['attributes' => ['target' => '_blank']] ), - ]; - } - - // Provide a peek if one is provided. - if ($history_content['peek'] != NULL) { - $step_content['history_content_' . $history_content['id'] . '_' . $index]['peek'] = [ - '#type' => 'item', - '#title' => 'Peek', - '#description' => 'A short view of the first few lines of the output file.', - '#markup' => '
' . $history_content['peek'] . '
', - ]; - } - } - } // End switch ($history_content['extension']) { ... - // end foreach ($history_contents as $index => $history_content) { ... - $content['history_content_' . $history_content['id'] . '_' . $index] = $step_content['history_content_' . $history_content['id'] . '_' . $index]; } } } - - if (count($errors) > 0) { - $content['errors']['table'] = [ - '#type' => 'markup', - '#markup' => theme_table([ - 'header' => $headers, - 'rows' => $errors, - 'attributes' => [ - 'class' => 'tripal-galaxy-error-table' - ], - 'sticky' => FALSE, - 'caption' => '', - 'colgroups' => [], - 'empty' => '', - ]), - ]; - } - else { - unset($content['errors']); - } - - if ($has_any_results) { - $content['results'] = [ - '#type' => 'item', - '#title' => t('Result Files'), - '#markup' => t('The result files are listed below. Click the filename to view or download results. Expand the field for the result below to - view the status, peek or download result files.'), - ]; - } - else { - $content['results'] = [ - '#type' => 'item', - '#title' => t('Results'), - '#markup' => t('Currently, no results exist for this job. The current state of the job is: %state.', [ - '%state' => $submission->status, - ]), - ]; - } - - - - return $content; + return $step_content; }