# Copyright 2019 École Polytechnique Fédér<NAME>. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#!/usr/bin/env python3
import tensorflow as tf
import os
from ..common.service import Service
from ..common.parse import *

persona_ops = tf.contrib.persona.persona_ops()

class DisplayService(Service):
   
    #default inputs
    def get_shortname(self):
        return "display"

    def add_graph_args(self, parser):
        parser.add_argument("start", type=int, help="The absolute index at which to start printing records")
        parser.add_argument("finish", type=int, help="The absolute index at which to stop printing records")
        parser.add_argument("-d", "--dataset-dir", help="The directory with the AGD dataset chunk files reside.")
        parser.add_argument("-u", "--unpack", default=True, action='store_false', help="Whether or not to unpack binary bases")

    def distributed_capability(self):
        return False

    def output_dtypes(self, args):
        return []

    def output_shapes(self, args):
        return []

    def extract_run_args(self, args):
        # doesnt really have any effect, since display just runs once 
        
        return []

    def make_graph(self, in_queue, args):
        """ Make the graph for this service. Returns two 
        things: a list of tensors which the runtime will 
        evaluate, and a list of run-once ops"""
        # make the graph

        run_once = run(args)

        return [], run_once

def run(args):

  dataset_params = args.dataset
  records = dataset_params['records']
  first_record = records[0]
  chunk_size = first_record["last"] - first_record["first"]
  chunknames = []
  dataset_dir = os.path.dirname(args.dataset[filepath_key])
  for record in records:
    chunknames.append(dataset_dir + "/" + record['path'])

  if (args.finish <= args.start):
    args.finish = args.start + 1

  if 'reference' not in dataset_params:
    raise Exception("No reference data in manifest {}. Unaligned BAM not yet supported. Please align dataset first.".format(args.dataset))

  """
  key: tensor with chunk key string
  local_directory: the "base path" from which these should be read
  column_grouping_factor: the number of keys to put together
  parallel_parse: the parallelism for processing records (decomp)
  """
  
  ref_index = []
  ref_seqs = []
  for contig in dataset_params['reference_contigs']:
    ref_index.append(contig['index'])
    ref_seqs.append(contig['name'])


  start = tf.constant(args.start, dtype=tf.int32)
  finish = tf.constant(args.finish, dtype=tf.int32)
  names = tf.constant(chunknames)

  size = tf.constant(chunk_size)
  columns = dataset_params['columns']
  
  output = persona_ops.agd_base_decompression(chunk_names=names, chunk_size=size, unpack=args.unpack, ref_sequences=ref_seqs,ref_index=ref_index,
      start=start, finish=finish, columns=columns)

  return [output]


# Copyright 2019 École Polytechnique Fédér<NAME>. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
#!/usr/bin/env python3
import tensorflow as tf
import os
from ..common.service import Service
from ..common.parse import *

persona_ops = tf.contrib.persona.persona_ops()

class DisplayService(Service):
   
    #default inputs
    def get_shortname(self):
        return "display"

    def add_graph_args(self, parser):
        parser.add_argument("start", type=int, help="The absolute index at which to start printing records")
        parser.add_argument("finish", type=int, help="The absolute index at which to stop printing records")
        parser.add_argument("-d", "--dataset-dir", help="The directory with the AGD dataset chunk files reside.")
        parser.add_argument("-u", "--unpack", default=True, action='store_false', help="Whether or not to unpack binary bases")

    def distributed_capability(self):
        return False

    def output_dtypes(self, args):
        return []

    def output_shapes(self, args):
        return []

    def extract_run_args(self, args):
        # doesnt really have any effect, since display just runs once 
        
        return []

    def make_graph(self, in_queue, args):
        """ Make the graph for this service. Returns two 
        things: a list of tensors which the runtime will 
        evaluate, and a list of run-once ops"""
        # make the graph

        run_once = run(args)

        return [], run_once

def run(args):

  dataset_params = args.dataset
  records = dataset_params['records']
  first_record = records[0]
  chunk_size = first_record["last"] - first_record["first"]
  chunknames = []
  dataset_dir = os.path.dirname(args.dataset[filepath_key])
  for record in records:
    chunknames.append(dataset_dir + "/" + record['path'])

  if (args.finish <= args.start):
    args.finish = args.start + 1

  if 'reference' not in dataset_params:
    raise Exception("No reference data in manifest {}. Unaligned BAM not yet supported. Please align dataset first.".format(args.dataset))

  """
  key: tensor with chunk key string
  local_directory: the "base path" from which these should be read
  column_grouping_factor: the number of keys to put together
  parallel_parse: the parallelism for processing records (decomp)
  """
  
  ref_index = []
  ref_seqs = []
  for contig in dataset_params['reference_contigs']:
    ref_index.append(contig['index'])
    ref_seqs.append(contig['name'])


  start = tf.constant(args.start, dtype=tf.int32)
  finish = tf.constant(args.finish, dtype=tf.int32)
  names = tf.constant(chunknames)

  size = tf.constant(chunk_size)
  columns = dataset_params['columns']
  
  output = persona_ops.agd_base_decompression(chunk_names=names, chunk_size=size, unpack=args.unpack, ref_sequences=ref_seqs,ref_index=ref_index,
      start=start, finish=finish, columns=columns)

  return [output]


# Copyright 2019 École Polytechnique Fédér<NAME>. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== #!/usr/bin/env python3 #default inputs # doesnt really have any effect, since display just runs once Make the graph for this service. Returns two          things: a list of tensors which the runtime will          evaluate, and a list of run-once ops # make the graph key: tensor with chunk key string   local_directory: the "base path" from which these should be read   column_grouping_factor: the number of keys to put together   parallel_parse: the parallelism for processing records (decomp)