# Path: ob_pipelines/s3.py
# def swap_args(args, rm_local_outpath=False):
#     """Swap S3 paths in arguments with local paths
#     
#     If the S3 path exists, it's an input, download first and swap the arg
#     with a temporary filepath. Otherwise, it's an output, save for upload 
#     after the command.
# 
#     Returns: 
#         tuple of (local_args, s3_downloads, s3_uploads)
# 
#         where new_args contains the new argument list with local paths, 
#         and s3_outputs is a dict mapping local filepaths to s3 paths to 
#         transfer after execution.
#     """
#     s3_uploads = {}
#     s3_downloads = {}
#     local_args = []
#     for arg in args:
#         if not arg.startswith('s3://'):
#             local_args.append(arg)
#             continue
#         
#         src_bucket, src_key = path_to_bucket_and_key(arg)
#         local_tmp = create_tmp_from_key(src_key)
#         
#         # If key exists, add path to downloads, otherwise add path to 
#         # uploads and remove the file or folder
#         if key_exists(src_bucket, src_key):
#             s3_downloads[arg] = local_tmp
#         else:
#             s3_uploads[arg] = local_tmp
#             if rm_local_outpath:
#                 remove_file_or_folder(local_tmp)
# 
#         local_args.append(local_tmp)
# 
#     return local_args, s3_downloads, s3_uploads
# 
# def upload_prefix(local_prefix, s3_prefix, fpath_templates):
#     """Upload all files matching a prefix template to S3
# 
#     Params
#         local_prefix: absolute filepath prefix of local files
#         s3_prefix: full S3 URI prefix for S3 files
#         fpath_templates: list of filepath strings containing {prefix} template
#     """
#     local_fpaths = [fpath.format(prefix=local_prefix) for fpath in fpath_templates]
#     s3_fpaths = [fpath.format(prefix=s3_prefix) for fpath in fpath_templates]
#     for local_fpath, s3_fpath in zip(local_fpaths, s3_fpaths):
#         bucket, key = path_to_bucket_and_key(s3_fpath)
#         s3.upload_file(local_fpath, bucket, key,
#             ExtraArgs={'ServerSideEncryption': 'AES256'})
# 
# def download_file_or_folder(s3_path, local_path):
#     """Dispatch S3 download depending on key path"""
#     bucket, key = path_to_bucket_and_key(s3_path)
#     if key.endswith('/'):
#         download_folder(bucket, key, local_path)
#     else:
#         s3.download_file(bucket, key, local_path)
#         if key.endswith('.bam'):
#             # Always download a BAM index if it exists
#             possible_index_keys = [key + '.bai', key.replace('.bam', '.bai')]
#             for index_key in possible_index_keys:
#                 if key_exists(bucket, index_key):
#                     s3.download_file(bucket, index_key, local_path + '.bai')
# 
# def remove_file_or_folder(fpath):
#     if op.isdir(fpath):
#         shutil.rmtree(fpath)
#     else:
#         os.remove(fpath)
# 
# SCRATCH_DIR = os.environ.get('SCRATCH_DIR') or '/tmp'


import logging
import os.path as op
import shutil
import subprocess
import click
from tempfile import mkdtemp
from ob_pipelines.s3 import (
    swap_args, upload_prefix, download_file_or_folder, remove_file_or_folder, SCRATCH_DIR
)



logger = logging.getLogger('__name__')

GENE_COVERAGE_OUTPUTS = {
    'png': '{prefix}.geneBodyCoverage.curves.png',
    'txt': '{prefix}.geneBodyCoverage.txt',
    'r': '{prefix}.geneBodyCoverage.r'
}


@click.command()
@click.argument('bam')
@click.argument('ref')
@click.argument('prefix')

# Path: ob_pipelines/s3.py
# def swap_args(args, rm_local_outpath=False):
#     """Swap S3 paths in arguments with local paths
#     
#     If the S3 path exists, it's an input, download first and swap the arg
#     with a temporary filepath. Otherwise, it's an output, save for upload 
#     after the command.
# 
#     Returns: 
#         tuple of (local_args, s3_downloads, s3_uploads)
# 
#         where new_args contains the new argument list with local paths, 
#         and s3_outputs is a dict mapping local filepaths to s3 paths to 
#         transfer after execution.
#     """
#     s3_uploads = {}
#     s3_downloads = {}
#     local_args = []
#     for arg in args:
#         if not arg.startswith('s3://'):
#             local_args.append(arg)
#             continue
#         
#         src_bucket, src_key = path_to_bucket_and_key(arg)
#         local_tmp = create_tmp_from_key(src_key)
#         
#         # If key exists, add path to downloads, otherwise add path to 
#         # uploads and remove the file or folder
#         if key_exists(src_bucket, src_key):
#             s3_downloads[arg] = local_tmp
#         else:
#             s3_uploads[arg] = local_tmp
#             if rm_local_outpath:
#                 remove_file_or_folder(local_tmp)
# 
#         local_args.append(local_tmp)
# 
#     return local_args, s3_downloads, s3_uploads
# 
# def upload_prefix(local_prefix, s3_prefix, fpath_templates):
#     """Upload all files matching a prefix template to S3
# 
#     Params
#         local_prefix: absolute filepath prefix of local files
#         s3_prefix: full S3 URI prefix for S3 files
#         fpath_templates: list of filepath strings containing {prefix} template
#     """
#     local_fpaths = [fpath.format(prefix=local_prefix) for fpath in fpath_templates]
#     s3_fpaths = [fpath.format(prefix=s3_prefix) for fpath in fpath_templates]
#     for local_fpath, s3_fpath in zip(local_fpaths, s3_fpaths):
#         bucket, key = path_to_bucket_and_key(s3_fpath)
#         s3.upload_file(local_fpath, bucket, key,
#             ExtraArgs={'ServerSideEncryption': 'AES256'})
# 
# def download_file_or_folder(s3_path, local_path):
#     """Dispatch S3 download depending on key path"""
#     bucket, key = path_to_bucket_and_key(s3_path)
#     if key.endswith('/'):
#         download_folder(bucket, key, local_path)
#     else:
#         s3.download_file(bucket, key, local_path)
#         if key.endswith('.bam'):
#             # Always download a BAM index if it exists
#             possible_index_keys = [key + '.bai', key.replace('.bam', '.bai')]
#             for index_key in possible_index_keys:
#                 if key_exists(bucket, index_key):
#                     s3.download_file(bucket, index_key, local_path + '.bai')
# 
# def remove_file_or_folder(fpath):
#     if op.isdir(fpath):
#         shutil.rmtree(fpath)
#     else:
#         os.remove(fpath)
# 
# SCRATCH_DIR = os.environ.get('SCRATCH_DIR') or '/tmp'


# Path: ob_pipelines/apps/rseqc/rseqc.py
import logging
import os.path as op
import shutil
import subprocess
import click
from tempfile import mkdtemp
from ob_pipelines.s3 import (
    swap_args, upload_prefix, download_file_or_folder, remove_file_or_folder, SCRATCH_DIR
)




logger = logging.getLogger('__name__')

GENE_COVERAGE_OUTPUTS = {
    'png': '{prefix}.geneBodyCoverage.curves.png',
    'txt': '{prefix}.geneBodyCoverage.txt',
    'r': '{prefix}.geneBodyCoverage.r'
}


@click.command()
@click.argument('bam')
@click.argument('ref')
@click.argument('prefix')

def gene_coverage(bam, ref, prefix):