
    5i4                     p    d Z ddlmZmZmZmZ ddlZddlmZm	Z	m
Z
mZ ddlmZ ddlmZ  G d	 d
ee
      Zy)z4
DXGlobalWorkflow Handler
++++++++++++++++++++++++

    )print_functionunicode_literalsdivisionabsolute_importN   )DXObject
DXAnalysisDXExecutableverify_string_dxid   )DXError)
basestringc                   |     e Zd ZdZdZddZddZd Zd ZddZ	d Z
dd	Zdd
Zd ZddZddZd Z fdZ xZS )DXGlobalWorkflowz0
    Remote global workflow object handler.
    globalworkflowc                 z    t        j                  |        ||| j                  |||       i | _        i | _        y )N)dxidnamealias)r   __init__set_id_workflows_by_region_workflow_desc_by_regionselfr   r   r   s       /home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/bindings/dxglobalworkflow.pyr   zDXGlobalWorkflow.__init__*   s?    $t/KKTEK: %'!(*%    c                 :   d| _         d| _        d| _        |-||t        d      t	        || j
                         || _         y|U|| _        t        |t              st        d|      |&t        |t              st        d|      || _        yd| _        yy)aj  
        :param dxid: Global workflow ID
        :type dxid: string
        :param name: Global workflow name
        :type name: string
        :param alias: Global workflow version or tag
        :type alias: string
        :raises: :exc:`~dxpy.exceptions.DXError` if *dxid* and some other input are both given or if neither *dxid* nor *name* are given

        Discards the currently stored ID and associates the handler
        with the requested parameters.  Note that if *dxid* is given,
        the other fields should not be given, and if *name* is given,
        *alias* has default value "default".

        Nz9Did not expect name or alias to be given if dxid is givenz+Global workflow name needs to be a string: z,Global workflow alias needs to be a string: default)_dxid_name_aliasr   r   _class
isinstancer   r   s       r   r   zDXGlobalWorkflow.set_id4   s      

5#4YZZtT[[1DJDJdJ/QUWXX !%4!V["]^^#' r   c                 p    | j                   | j                   S d| j                  z   dz   | j                  z   S )z
        :returns: Object ID of associated global workflow
        :rtype: string

        Returns the object ID of the global workflow that the handler is currently
        associated with.
        globalworkflow-/)r    r!   r"   )r   s    r   get_idzDXGlobalWorkflow.get_idW   s5     ::!::$tzz1C7$++EEr   c                    i }dD ]6  }||vr%t        | j                  j                  d|d      ||   ||<   ||= 8 d|v r|d   |d<   |d= t        j                  j
                  |fi |}| j                  |d          y)	a,  

        .. note:: It is highly recommended that `dx build --create-global-workflow
           <https://documentation.dnanexus.com/user/helpstrings-of-sdk-command-line-utilities#build>`_
           be used instead for global workflow creation.

        Creates a workflow in the global space with the given parameters by using
        the specified workflow(s) in regionalOptions as a base.

        The workflow is available only to its developers until
        :meth:`publish()` is called, and is not run until :meth:`run()`
        is called.

        )versionr   regionalOptionsz: Keyword argument z is requiredbill_tobillToid)r   N)r   	__class____name__dxpyapiglobal_workflow_newr   )r   kwargsdx_hashfieldresps        r   newzDXGlobalWorkflow.newd   s     ; 	EF"t~~G^G^`efgg#E]GENu		  &y 1GHy!xx++G>v>d$r   c                 <   i }|r||d<   | j                   =t        j                  j                  | j                   fd|i|| _        | j                  S t        j                  j                  d| j
                  z   f| j                  |d|| _        | j                  S )aX  
        :param fields: Hash where the keys are field names that should be returned, and values should be set to True (default is that all fields are returned)
        :type fields: dict
        :returns: Description of the remote global workflow object
        :rtype: dict

        Returns a dict with a description of the workflow.

        fieldsinput_paramsr&   r   r;   )r    r1   r2   global_workflow_describe_descr!   r"   )r   r:   r4   describe_inputs       r   describezDXGlobalWorkflow.describe   s     '-N8$::!::4::mTbmflmDJ
 zz ::;Ltzz;Y baealalHVbZ`bDJ zzr   c                     | j                   *t        j                  j                  | j                   fi |S t        j                  j                  d| j                  z   fd| j
                  i|S )z
        Publishes the global workflow, so all users can find it and use it on the platform.

        The current user must be a developer of the workflow.
        r&   r   )r    r1   r2   global_workflow_publishr!   r"   )r   r4   s     r   publishzDXGlobalWorkflow.publish   s]     ::!8833DJJI&II88334E

4RpZ^ZeZepioppr   c                    ||j                  dd      dk(  sJ |t        d      || j                  v r| j                  |   S |s| j                         }||d   j	                         vr)t        dj                  | j                         |            |d   |   d   }t        j                  |      }|j                         }|| _        |S )aN  
        :param region: region name
        :type region: string
        :param describe_output: description of a global workflow
        :type describe_output: dict
        :returns: object description of a workflow
        :rtype: : dict

        Returns an object description of an underlying workflow from a given region.
        class r   GDXGlobalWorkflow: region must be provided to get an underlying workflowr+   DDXGlobalWorkflow: the global workflow {} is not enabled in region {}workflow)	getr   r   r@   keysformatr(   r1   
DXWorkflow)r   regiondescribe_outputunderlying_workflow_id
dxworkflowdxworkflow_descs         r   describe_underlying_workflowz-DXGlobalWorkflow.describe_underlying_workflow   s     &/*=*=gr*JN^*^_^>Y[ [ T2220088"mmoO):;@@BB`ggv' ( ( "11B!CF!KJ!W__%;<
$--/(7%r   c                    ||j                  d      dk(  sJ |t        d      || j                  v r| j                  |   S |s| j                         }||d   j	                         vr)t        dj                  | j                         |            |d   |   d   }t        j                  |      | _	        t        j                  |      S )am  
        :param region: region name
        :type region: string
        :param describe_output: description of a global workflow
        :type describe_output: dict
        :returns: object handler of a workflow
        :rtype: :class:`~dxpy.bindings.dxworkflow.DXWorkflow`

        Returns an object handler of an underlying workflow from a given region.
        rE   r   rG   r+   rH   rI   )
rJ   r   r   r@   rK   rL   r(   r1   rM   r   )r   rN   rO   rP   s       r   get_underlying_workflowz(DXGlobalWorkflow.get_underlying_workflow   s     &/*=*=g*FJZ*Z[Z>Y[ [ T...,,V44"mmoO):;@@BB`ggv' ( ( "11B!CF!KJ!W(,8N(O%566r   c                 z    ||j                  d      dk(  sJ | j                  ||      }dD ]
  }||   ||<    |S )a  
        :param region: region name
        :type region: string
        :param describe_output: description of a global workflow
        :type describe_output: dict
        :returns: object description of the global workflow
        :rtype: : dict

        Appends stages, inputs, outputs and other workflow-specific metadata to a global workflow describe output.

        Note: global workflow description does not contain functional metadata (stages, IO), since this data
        is region-specific (due to applets and bound inputs) and so reside only in region-specific underlying
        workflows. We add them to global_workflow_desc so that it can be used for a workflow or a global workflow
        rE   r   rO   )inputsoutputs	inputSpec
outputSpecstages)rJ   rS   )r   rO   rN   underlying_workflow_descr6   s        r   append_underlying_workflow_descz0DXGlobalWorkflow.append_underlying_workflow_desc   sf     &/*=*=g*FJZ*Z[Z#'#D#DVUd $E $f O 	EE%=e%DOE"	Er   c                 J    | j                  ||      }|j                  |      S )NrW   )rU   _get_input_name)r   	input_strrN   rO   rQ   s        r   r`   z DXGlobalWorkflow._get_input_name   s(    11&/1Z
)))44r   c                     t         j                  j                  |dddii      d   }| j                  |      } |j                  |fd|i|S )z
        Checks the region in which the global workflow is run
        and returns the input associated with the underlying workflow
        from that region.
        r:   rN   T)r;   project)r1   r2   project_describerU   _get_run_input)r   workflow_inputrc   r4   rN   rQ   s         r   re   zDXGlobalWorkflow._get_run_input   sc     **79AHdCS8T + VV^`11&9
(z((SSFSSr   c                    | j                   8t        t        j                  j                  | j                   fd|i|d         S t        t        j                  j                  d| j
                  z   f| j                  |d|d         S )Nr;   r.   r&   r<   )r    r	   r1   r2   global_workflow_runr!   r"   )r   	run_inputr4   s      r   	_run_implzDXGlobalWorkflow._run_impl  s    ::!dhh::4::hT]haghimnoodhh::;Ltzz;Y EaealalHQE=CEEIK L Lr   c                 2    t        t        | 
  |g|i |S )a  
        :param workflow_input: Dictionary of the workflow's input arguments; see below for more details
        :type workflow_input: dict
        :param instance_type: Instance type on which all stages' jobs will be run, or a dict mapping function names to instance types. These may be overridden on a per-stage basis if stage_instance_types is specified.
        :type instance_type: string or dict
        :param stage_instance_types: A dict mapping stage IDs, names, or indices to either a string (representing an instance type to be used for all functions in that stage), or a dict mapping function names to instance types.
        :type stage_instance_types: dict
        :param stage_folders: A dict mapping stage IDs, names, indices, and/or the string "*" to folder values to be used for the stages' output folders (use "*" as the default for all unnamed stages)
        :type stage_folders: dict
        :param rerun_stages: A list of stage IDs, names, indices, and/or the string "*" to indicate which stages should be run even if there are cached executions available
        :type rerun_stages: list of strings
        :returns: Object handler of the newly created analysis
        :rtype: :class:`~dxpy.bindings.dxanalysis.DXAnalysis`

        Run the workflow. See :meth:`dxpy.bindings.dxapplet.DXExecutable.run` for additional args.

        When providing input for the workflow, keys should be of one of the following forms:

        * "N.name" where *N* is the stage number, and *name* is the
          name of the input, e.g. "0.reads" if the first stage takes
          in an input called "reads"

        * "stagename.name" where *stagename* is the stage name, and
          *name* is the name of the input within the stage

        * "stageID.name" where *stageID* is the stage ID, and *name*
          is the name of the input within the stage

        * "name" where *name* is the name of a workflow level input
          (defined in inputs) or the name that has been
          exported for the workflow (this name will appear as a key
          in the "inputSpec" of this workflow's description if it has
          been exported for this purpose)

        )superr   run)r   rf   argsr4   r/   s       r   rm   zDXGlobalWorkflow.run  s#    H %t0Q$Q&QQr   )NNN)N)NN)r0   
__module____qualname____doc__r#   r   r   r(   r8   r@   rC   rS   rU   r^   r`   re   rj   rm   __classcell__)r/   s   @r   r   r   #   s^     F+!(FF%<,	q D7@.5	TL$R $Rr   r   )rq   
__future__r   r   r   r   r1   rF   r   r	   r
   r   
exceptionsr   compatr   r    r   r   <module>rw      s4   " S R  D D   TRx TRr   