#!/usr/bin/env python

import arvados
import os
import re
import sys
import pyrtg

arvados.job_setup.one_task_per_input_stream(if_sequence=0, and_end_task=True)

this_job = arvados.current_job()
this_task = arvados.current_task()
in_dir = os.path.join(this_task.tmpdir, 'input')
arvados.util.run_command(['rm', '-rf', in_dir], stderr=sys.stderr)
in_dir = arvados.util.stream_extract(
    stream = arvados.StreamReader(this_task['parameters']['input']),
    path = in_dir,
    decompress = False)
ref_dir = arvados.util.collection_extract(
    collection = this_job['script_parameters']['reference'],
    path = 'reference',
    decompress = False)

out_dir = os.path.join(arvados.current_task().tmpdir, 'out')
arvados.util.run_command(['rm', '-rf', out_dir], stderr=sys.stderr)

# map reads
pyrtg.run_rtg('map', out_dir,
              ['-i', in_dir,
               '-t', ref_dir,
               '-a', '2',
               '-b', '1',
               '--sam-rg', '@RG\\tID:NA\\tSM:NA\\tPL:ILLUMINA'])

# store output
out = arvados.CollectionWriter()
out.write_directory_tree(out_dir, this_task['parameters']['input'][0], 0)
this_task.set_output(out.finish())