<tool id="bayestme_bleed_correct_with_plotting" name="BayesTME: Bleeding Correction" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
    <description>Bleeding Correction and Plotting for Spatial Transcriptomics with BayesTME </description>
    <macros>
        <import>macros.xml</import>
    </macros>
	<expand macro="bayestme_requirements"/>
	<command detect_errors="aggressive"><![CDATA[
		
		## Bleeding Correction Step
	    bleeding_correction
		    --adata '${adata_raw}'
			--n-top '${bc_args.n_top_bc}'
			--max-steps '${bc_args.max_steps}'
			#if str($bc_args.local_weight)
	            --local-weight '${bc_args.local_weight}'
			#end if
			--adata-output 'dataset_corrected.h5ad'
			--bleed-out 'bleed_correction_results.h5'
		    #if $verbose
                -v
            #end if
			&&
			
		mkdir -p bleed_correction_plots &&
		
		## Bleeding Correction Results Step
	    plot_bleeding_correction
            --raw-adata '${adata_raw}'
		    --corrected-adata 'dataset_corrected.h5ad'
		    --bleed-correction-results 'bleed_correction_results.h5'
		    --n-top '${plot_args.n_top_plot}'
		    --output-dir bleed_correction_plots
		    #if $verbose
                -v
            #end if	
			
	]]></command>
    
	<inputs>
		
		<!-- Raw ST Input Data (shared) -->
		<param name="adata_raw" argument="--adata" type="data" format="h5ad" label="Input file, AnnData in h5 format"/>
		
		<!-- Bleeding Correction Arguments -->
		<section name="bc_args" title="Bleeding Correction Parameters" expanded="false"> 
		    <param name="n_top_bc" argument="--n-top" type="integer" value="50" label="Use N top genes by standard deviation to calculate the bleeding functions. Genes will not be filtered from output dataset."/>
	        <param argument="--max-steps" type="integer" value="5" label="Number of EM steps"/>
		    <param argument="--local-weight" type="float" optional="true" label="Initial value for local weight, a tuning parameter for bleed correction. rho_0g from equation 1 in the paper. By default will be set to sqrt(N tissue spots)"/>
		</section>
		   
		<!-- Plot Bleeding Correction Arguments -->
		<section name="plot_args" title="Plotting Arguments" expanded="false"> 
	        <param name="n_top_plot" argument="--n-top" type="integer" value="10" label="Plot top n genes by stddev"/>
		</section>
		
		<!-- Additional Arguments (shared) -->
		<param argument="--verbose" type="boolean" checked="false" label="Enable verbose logging"/>
				
	</inputs>
	
    <outputs>
		
		<!-- Bleeding Correction Outputs -->
        <data name="adata_output" format="h5ad" label="BayesTME Bleeding Correction on ${on_string}: dataset_corrected.h5ad" from_work_dir="dataset_corrected.h5ad"/>
		<data name="bleed_out" format="h5" label="BayesTME Bleeding Correction on ${on_string}: bleed_correction_results.h5" from_work_dir="bleed_correction_results.h5"/>
		
		<!-- Plot Bleeding Correction Outputs -->
		<collection name="bleeding_correction_plots" type="list" label="BayesTME Plot Bleeding Correction on ${on_string}">
		    <discover_datasets pattern="__name_and_ext__" directory="bleed_correction_plots" ext="pdf" />
		</collection>
		
    </outputs>
	
    <tests>
		<test>
			<!-- Test Raw ST Input Data (shared) -->
			<param name="adata_raw" value="visium_test_dataset.h5ad"/>
			
			<!-- Bleeding Correction Arguments -->
			<section name="bc_args">
			    <param name="n_top_bc" value="20"/>
			    <param name="max_steps" value="3"/>
			</section>

			<!-- Plot Bleeding Correction Arguments -->
			<section name="plot_args">
			    <param name="n_top_plot" value="5"/>
			</section>

			<!-- Bleeding Correction Outputs -->			
	        <output name="adata_output" file="bleed_correct_test_data/dataset_corrected.h5ad" compare="sim_size" delta="50000">
				<assert_contents>
					<has_h5_keys keys="obs,var"/>
				</assert_contents>
			</output>
			<output name="bleed_out" file="bleed_correct_test_data/bleed_correction_results.h5" compare="sim_size" delta="50000">
				<assert_contents>
					<has_h5_keys keys="basis_functions,corrected_reads,global_rates,weights"/>
				</assert_contents>
			</output>

			<!-- Plot Bleeding Correction Outputs -->
			<output_collection name="bleeding_correction_plots" type="list" count="11">
		        <element name="AGR2_bleed_vectors" file="bleed_correct_test_data/AGR2_bleed_vectors.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="AGR2_bleeding_plot" file="bleed_correct_test_data/AGR2_bleeding_plot.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="CXCL14_bleed_vectors" file="bleed_correct_test_data/CXCL14_bleed_vectors.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="CXCL14_bleeding_plot" file="bleed_correct_test_data/CXCL14_bleeding_plot.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="IER3_bleed_vectors" file="bleed_correct_test_data/IER3_bleed_vectors.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="IER3_bleeding_plot" file="bleed_correct_test_data/IER3_bleeding_plot.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="KRT19_bleed_vectors" file="bleed_correct_test_data/KRT19_bleed_vectors.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="KRT19_bleeding_plot" file="bleed_correct_test_data/KRT19_bleeding_plot.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="KRT8_bleed_vectors" file="bleed_correct_test_data/KRT8_bleed_vectors.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="KRT8_bleeding_plot" file="bleed_correct_test_data/KRT8_bleeding_plot.pdf" compare="sim_size" delta="20000" ftype="pdf"/>
				<element name="basis-functions" file="bleed_correct_test_data/basis-functions.pdf" compare="sim_size" delta="20000" ftype="pdf"/>			
		    </output_collection>
		</test>	
    </tests>
	
    <help>
		
<![CDATA[

usage: bleeding_correction [-h] [--adata ADATA] [--bleed-out BLEED_OUT] [--adata-output ADATA_OUTPUT] [-i] [--n-top N_TOP] [--max-steps MAX_STEPS]
							[--local-weight LOCAL_WEIGHT] [-v]

Perform bleeding correction

options:
	-h, --help            show this help message and exit
	--adata ADATA         Input file, AnnData in h5 format
	--bleed-out BLEED_OUT
						Output file, BleedCorrectionResult in h5 format
	--adata-output ADATA_OUTPUT
						A new AnnData in h5 format created using the bleed corrected counts
	-i, --inplace         If provided, overwrite the input file --adata
	--n-top N_TOP         Use N top genes by standard deviation to calculate the bleeding functions. Genes will not be filtered from output dataset.
	--max-steps MAX_STEPS
						Number of EM steps
	--local-weight LOCAL_WEIGHT
						Initial value for local weight, a tuning parameter for bleed correction. rho_0g from equation 1 in the paper. By default will be set
						to sqrt(N tissue spots)
	-v, --verbose         Enable verbose logging
	
	
usage: plot_bleeding_correction [-h] [--raw-adata RAW_ADATA] [--corrected-adata CORRECTED_ADATA] [--bleed-correction-results BLEED_CORRECTION_RESULTS]
								[--output-dir OUTPUT_DIR] [--n-top N_TOP] [-v]

Plot bleeding correction results

options:
	-h, --help            show this help message and exit
	--raw-adata RAW_ADATA
						Input file, AnnData in h5 format
	--corrected-adata CORRECTED_ADATA
						Input file, AnnData in h5 format
	--bleed-correction-results BLEED_CORRECTION_RESULTS
						Input file, BleedCorrectionResult in h5 format
	--output-dir OUTPUT_DIR
						Output directory
	--n-top N_TOP         Plot top n genes by stddev
	-v, --verbose         Enable verbose logging

]]>
    </help>
    <expand macro="citations"/>
</tool>