require 'forwardable'

module SequenceServer
  module BLAST
    # Formats BLAST+ archive file format into other file formats.
    class Formatter
      class << self
        alias_method :run, :new
      end

      extend Forwardable
      def_delegators SequenceServer, :config, :logger, :sys

      def initialize(job, type)
        @job = job
        @format, @mime, @specifiers = OUTFMT[type]
        @type = type

        run
      end

      attr_reader :job, :type

      attr_reader :format, :mime, :specifiers

      def file
        @file ||= File.join(job.dir, filename)
      end

      def filename
        @filename ||=
          "sequenceserver-#{type}_report.#{mime}"
      end

      private

      def run
        return if File.exist?(file)
        command =
          "blast_formatter -archive '#{job.stdout}'" \
          " -outfmt '#{format} #{specifiers}'" \
          " -out '#{file}'"
        sys(command, path: config[:bin], dir: DOTDIR)
      rescue CommandFailed => e
        fail SystemError, e.stderr
      end
    end
  end
end

# References
# ----------
# [1]: http://www.ncbi.nlm.nih.gov/books/NBK1763/

require 'forwardable'

module SequenceServer
  module BLAST
    # Formatter is invoked during report generation or for results download to
    # convert BLAST+ archive file to other formats. Formatter generates output
    # in Job#dir. Output files persist till the job itself is deleted. Calling
    # Formatter a second time (for the same input job and output format) will
    # return saved ouput.
    class Formatter
      class << self
        alias_method :run, :new
      end

      extend Forwardable
      def_delegators SequenceServer, :config, :sys

      def initialize(job, type)
        @job = job
        @type = type

        @format, @mime, @specifiers = OUTFMT[type]
        run
      end

      attr_reader :format, :mime, :specifiers

      def file
        @file ||= File.join(job.dir, filename)
      end

      def filename
        @filename ||= "sequenceserver-#{type}_report.#{mime}"
      end

      private

      attr_reader :job, :type

      def run
        return if File.exist?(file)
        command = "blast_formatter -archive '#{job.stdout}'" \
          " -outfmt '#{format} #{specifiers}'"
        sys(command, path: config[:bin], dir: DOTDIR,
            stdout: file)
      rescue CommandFailed => e
        # Mostly we will never get here: empty archive file,
        # file permissions, broken BLAST binaries, etc. will
        # have been caught before reaching here.
        fail SystemError, e.stderr
      end
    end
  end
end

# References
# ----------
# [1]: http://www.ncbi.nlm.nih.gov/books/NBK1763/

Use sys's stdout option in BLAST::Formatter
Use sys's stdout option in BLAST::Formatter

Instead of blast_formatter's -out option. Just being consistent
- we don't use -out option while running the search either.

Increase documentation comments and fix funny line breaks.

Signed-off-by: Anurag Priyam <6e6ab5ea9cb59fe7c35d2a1fc74443577eb60635@gmail.com>
