
    5iW!                         d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
mZ d dlmZmZmZmZmZmZmZ d dlmZ d dlmZ d dlmZ  ej4                  d	
      Z	 	 	 	 	 	 	 	 	 	 ddZ	 	 ddZddZy)    N)glob)partial)get_nextflow_dxappget_nextflow_src)get_template_dir
write_execwrite_dxappget_importer_namecreate_readme
get_nested get_allowed_extra_fields_mapping)	parse_obj)try_call)resolve_existing_pathzUploads a DNAnexus App.)descriptionc
           
         d }
|	xs i }	t         j                  }d} |
|	      }| |d<   |df|df|df|df|t        t        d      f|t        t        d      fd}|j	                         D ]  \  }\  }}|s|r ||      n|||<    |rt        t        |d      \  }}}|t        j                  d	       t        j                  t               
      j                  |||d| z  d      }|st        d|j                                |j                  d       t        j                  |j!                  ddi      d   d         \  }}|st        d|z         |S t        t#        j$                  t'        |                   |S )a  
    :param repository: URL to a Git repository
    :type repository: string
    :param tag: tag of a given Git repository. If it is not provided, the default branch is used.
    :type tag: string
    :param cache_docker: Pull a remote docker image and store it on the platform
    :type cache_docker: bool
    :param docker_secrets: Dx file id with the private docker registry credentials
    :type docker_secrets: string
    :param nextflow_pipeline_params: Custom Nextflow pipeline parameters
    :type nextflow_pipeline_params: string
    :param profile: Custom Nextflow profile, for more information visit https://www.nextflow.io/docs/latest/config.html#config-profiles
    :type profile: string
    :param brief: Level of verbosity
    :type brief: boolean
    :returns: ID of the created applet

    Runs the Nextflow Pipeline Importer app, which creates a Nextflow applet from a given Git repository.
    c                 f    t               D ci c]  \  }}t        | |      x}|| c}}S c c}}w )zq
        :param args: extra args from command input
        :returns: overridable fields from extra_args
        )r   r   )argsarg_path
target_keyvals       /home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/nextflow/nextflow_builder.pyparse_extra_argsz1build_pipeline_with_npi.<locals>.parse_extra_args6   sB     )I(J
$*!$11> O
 	
 
s   -Nrepository_urlfile)klass)repository_tagconfig_profilecache_dockernextflow_pipeline_paramsdocker_secretsgithub_credentialsfolder)expectedzCan't create an applet without specifying a destination project; please use the -d/--destination flag to explicitly specify a project)namezNextflow build of %sT)	app_inputprojectr#   r%   detachzStarted builder job    )intervaloutput)fieldsoutput_appletzCreated Nextflow pipeline %s)id)dxpyWORKSPACE_IDr   r   itemsr   r   parsererrorDXAppr
   runprintget_idwait_on_doneget_dxlink_idsdescribejsondumpsdict)
repositorytagr   r!   r    profile	git_credsbriefdestination
extra_argsr   build_project_idbuild_folder
input_hashinput_updateskey	raw_value	transform_nf_builder_job	applet_ids                        r   build_pipeline_with_npirO      s   @	
 !rJ((L!*-J#-J  +"D/%t,%=t$D)79F+KL(')6*JKM (5':':'< O##i6?i	2YJsOO ,45JKbj,k), T	UZZ%6%89==
\lEQCY]gChqu > wN .*?*?*ACD+&&~'>'>xQUFV'>'WX`'abq'rsLIq,	:;  	djj+,-    c                 $   t         j                  j                  |       sJ t        t         j                  j	                  | d            s"t
        j                  j                  d| z        t        j                  d      }t        t         j                  j	                  | d            }t        || ||||      }t        |||       }t        j                  t               |d	
       t!        ||       t#        ||       t%        | |       |S )az  
    :param resources_dir: Directory with all resources needed for the Nextflow pipeline. Usually directory with user's Nextflow files.
    :type resources_dir: str or Path
    :param profile: Custom Nextflow profile. More profiles can be provided by using comma separated string (without whitespaces).
    :type profile: str
    :param region: The region in which the applet will be built.
    :type region: str
    :param cache_docker: Perform pipeline analysis and cache the detected docker images on the platform
    :type cache_docker: boolean
    :param nextflow_pipeline_params: Custom Nextflow pipeline parameters
    :type nextflow_pipeline_params: string
    :returns: Path to the created dxapp_dir
    :rtype: Path

    Creates files necessary for creating an applet on the Platform, such as dxapp.json and a source file. These files are created in '.dx.nextflow' directory.
    z*.nfzRDirectory %s does not contain Nextflow file (*.nf): not a valid Nextflow directoryz.dx.nextflow)prefixznextflow_schema.json)schema_file)custom_inputsresources_dirregionr@   r   r    )rT   r@   rU   T)dirs_exist_ok)ospathexistsr   joinr/   app_builderAppBuilderExceptiontempfilemkdtempprepare_custom_inputsr   r   shutilcopytreer   r	   r   r   )	rU   r@   rV   r   r    	dxapp_dirrT   dxapp_contentexec_contents	            r   prepare_nextflowrf   h   s    . 77>>-(((]F3422`cppr 	r  7I)bggll=Rh6ijM&##!!9M $-`mnL
OO$&	F	=)y,'-+rP   c                    dd}g }t         j                  j                  |       s|S t        | d      5 }t	        j
                  |      }ddd       dv rdnd|v rdni }|j                  |      j                         D ]Y  \  }}|j                  dg       }|j                  di       j                         D ]  \  }	}
i }|	|d<   |d   |d	<   d
|
v r$dj                  |
j                  d
d            |d<   d|
v r(|j                  dd      |
j                  dd      z   |d<   |
j                  dd      |d<    ||
j                  d      |
j                  d            |d<   d|d<   |	|vr6dj                  |j                  dd            |d<   |j                  |       dj                  |j                  dd            |d<   |j                  d|         \ |S # 1 sw Y   xY w)a$  
    :param schema_file: path to nextflow_schema.json file
    :type schema_file: str or Path
    :returns: list of custom inputs defined with DNAnexus datatype 
    :rtype: list
    Creates custom input list from nextflow_schema.json that
    will be added in dxapp.json inputSpec field
    Nc                     dddddd}dddd}| dk(  r	||v r||   S | |v r||    S t        d	j                  |             )
Nstringintfloatbooleanhash)ri   integernumberrl   objectr   )z	file-pathzdirectory-pathrY   z$type {} is not supported by DNAnexus)	Exceptionformat)nf_type	nf_formattypes	str_typess       r   get_dx_typez*prepare_custom_inputs.<locals>.get_dx_type   so     
  &
	
 h9	#9Y''>!>EEgNOOrP   rdefinitionsz$defsrequired
propertiesr%   titledefaultzDefault value:{}
 help	help_texthiddenFtyperr   classToptionalz(Nextflow pipeline optional) {}z(Nextflow pipeline required) {}r   )N)rX   rY   rZ   openr;   loadgetr1   rr   appendinsert)rS   rw   inputsfhschemadefs_keyd_keyd_schemarequired_inputsproperty_keypropertydx_inputs               r   r`   r`      s   P& F77>>+&	k3	 22 - 7}SYHYW_aH!::h/557 +x",,z26&.ll<&D&J&J&L 	+"L(H+HV ( 0HWH$#7#>#>x||IWY?Z#[ h&#+<<#;hll;XZ>[#[ !)h!>HX +HLL,@(,,xBX YHW#'HZ ?2#D#K#KHLLY_acLd#e h'#D#K#KHLLY_acLd#e a*!	++( M/ s   GG()
NNFNr~   r~   NFNN)Fr~   )z./nextflow_schema.json)rX   r/   r;   argparser   ra   r^   	functoolsr    dxpy.nextflow.nextflow_templatesr   r   dxpy.nextflow.nextflow_utilsr   r   r	   r
   r   r   r   dxpy.cli.exec_ior   dxpy.clir   dxpy.utils.resolverr   ArgumentParserr2   rO   rf   r`    rP   r   <module>r      s    	        Sg g g &  5 
!	 	 -F	G !#Ol !#+\8rP   