#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# iMembrane : Annotation projector
#
# Author: Sebastian Kelm
# Created: 09/05/2010
#

import sys, os

if __name__ == "__main__":
  # Make sure we can import stuff
  sys.path.append(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "..", "lib-python"))


from prosci.imembrane.projector import Projector
from prosci.shell import Params


################################
# Command line option handling #
################################

params = Params(withargument=("dbdir", "dbid", "seq", "inputid", "struc", "outdir", "insubdir", "outsubdir", "outputparent"), allowed=("help", "dbdir", "dbid", "seq", "inputid", "struc", "outdir", "insubdir", "outsubdir", "skipalign", "write_fasta", "joy"))

if not params.opts or params.args or "help" in params.opts:
    print """
iMembrane - annotation projection component

Input:  A protein sequence or structure, the ID of an iMembraneDB entry,
    (optionally) an alignment between the input and database proteins
Output: Projected JOY-style annotation for the input protein


USAGE:
"""+params.scriptname+""" OPTIONS

--dbdir DIRECTORY
          The path to the main directory of iMembrane DB

--dbid ID
          ID of the database protein to use as an annotation template.

--seq FILE
          Input sequence, in PIR/JOY format.
          Can also be an alignment between input and database proteins.
          (FILE can be '-', for STDIN.)

--inputid ID
          ID of the input protein in the file provided by --seq.
          Not necessary if the file contains only one sequence/structure
          entry.

--struc FILE
          Input structure, in PDB format.

--outdir DIRECTORY
          Directory in which to create the output sub-directory. Defaults to the
          current working directory.

--outputparent MODE  ("copy" OR "link" OR "none"; Default: "copy")
          Should each hit contain a copy of / link to the parent entry in the database?

--skipalign
          The sequences/structures may already be aligned.
          Don't re-align unless you have to.
          
--write_fasta
          Write a file called 'sequence.fasta' containing the input protein's
          sequence.

--insubdir SUBDIRECTORY
          Optional. If the dbid is not the same as the name of the subdirectory
          in which the database entry is to be found, use this option.

--outsubdir SUBDIRECTORY
          Optional. This is the name of the subdirectory the output files go in.
          If this option is not provided, it defaults to the inputid.

--joy
          Include JOY annotation in the output file query_annotation.tem
          NOTE: This requires the executable "joy" to be in your PATH. Otherwise
                this option results in an error."""
    sys.exit(0)


outdir = params.getOpt("outdir")
if None==outdir:
  outdir = "."

outputparent = params.getOpt("outputparent", "copy")
if outputparent:
  outputparent = outputparent.lower()
  assert outputparent in ("copy", "link", "none")

p = Projector(params.getOpt("dbdir"), params.getOpt("dbid"), params.getOpt("inputid"), params.getOpt("seq"), params.getOpt("struc"), skipalign=params.isOpt("skipalign"), insubdir=params.getOpt("insubdir"), run_joy=params.isOpt("joy"))
p.annotate()
p.write_output(outdir, subdir=params.getOpt("outsubdir"), copy_parent=(outputparent=="copy"), link_parent=(outputparent=="link"), write_fasta=params.isOpt("write_fasta"))



