#!/usr/bin/python
#
# Simple example script how to use the Bellerophon web services
#

from BellerophonServer_client import *
import sys, time


if (len(sys.argv) < 2):
    sys.stderr.write('\n\nusage: python %s <FASTA input file> <output file>\n\n' % sys.argv[0])
    sys.stderr.write('<FASTA input file> = filename of file with input r16S sequences in FASTA format\n')
    sys.stderr.write('<output file>      = filename where Bellerophon output is saved.\n')
    sys.stderr.write('                     If no output filename is provided then the output is written\n')
    sys.stderr.write('                     to stdout\n\n')
    sys.stderr.write('i.e. python %s stein0.fasta\nYou can download stein0.fasta from\nhttp://comp-bio.anu.edu.au/Bellerophon/ws_doc/stein0.fasta\n\n')
    sys.exit()



TRACE=None
loc = BellerophonServerLocator()

port = loc.getBellerophonServer(url='http://comp-bio.anu.edu.au:8000/bellerophon', tracefile=TRACE)



#
# Build a request dictionary
#
request = {}

# your Bellerophon key
request['key'] = 'INSERT-YOUR-BELLEROPHON-KEY-HERE'

# read FASTA file
f = open(sys.argv[1])
request['sequences'] = f.read()
f.close()

# window size. only 200, 300 or 400 allowed (default = 300)
request['windowsize'] = 300

# correction. allowed values are 'none', 'Kimura', 'Jukes-Cantor', or 'Huber-Hugenholtz' (default = 'Huber-Hugenholtz')
request['correction'] = 'Huber-Hugenholtz'

# wether sequences should be aligned. allowed values 'yes', or 'no' (default = sequences are NOT aligned by Bellerophon)
request['align'] = 'yes'

# wether sequences are from a PCR library. allowed values 'yes' or 'no' (default = sequences are from PCR library)
request['pcr_library'] = 'yes'


# please notice the submission limits!
#   - not more than 1000 unaligned sequence, or
#   - not more than 1500 aligned sequences
# when using classical Bellerophon calculations within the given PCR library
#
#   - not more than 100 sequences
# when using Bellerophon3 calculations agains the greengene database



# send request off to Bellerophon
msg = BellerophonRequest()
msg.Value = request
rsp = port.Bellerophon(msg)


# write response to file
if (len(sys.argv) > 2):
    f = open(sys.argv[2], 'w')
    f.write(rsp.Value)
    f.close()
else:
    sys.stdout.write(rsp.Value)

sys.exit()


