#!/usr/bin/env python
# rename_contigs 0.0.1
# Generated by dx-app-wizard.
#
# Basic execution pattern: Your app will run on a single machine from
# beginning to end.
#
# See https://wiki.dnanexus.com/Developer-Portal for documentation and
# tutorials on how to modify this file.
#
# DNAnexus Python Bindings (dxpy) documentation:
#   http://autodoc.dnanexus.com/bindings/python/current/

import os

import dxpy
import dx_utils

@dxpy.entry_point('main')
def main(input_fasta, prefix='contig'):
    fn = dxpy.describe(input_fasta)['name']
    ofn = fn.replace('.fasta.gz', '.renamed.fasta.gz')
    cmd = 'dx cat {0} | gunzip | python rename_contigs.py -p {1} | gzip | dx upload - --destination {2} --brief'
    cmd = cmd.format(input_fasta['$dnanexus_link'], prefix, ofn)
    fid = dx_utils.run_cmd(cmd, returnOutput=True)

    output = {}
    output['output_fasta'] = dxpy.dxlink(fid)

    return output

dxpy.run()
