#!/usr/bin/python
#-*-coding: utf-8-*-

"""Name : supported_organisms_ensembl
Version: v0.1
Description: this program was developed in order to get informations 
on all the studied species available on the Genoscope server. It runs only in Python 2.7.
Authors: alexandra.bomane@laposte.net, mel.colombier@free.fr
Usage: python supported_organisms_ensembl.py [-o outfile_name] [--output_file outfile_name]
Options: 
    -h  Display full help message
    -help
        Same as -h
    -o outfile_name
        If no output file is specified, the standard output is used.
    --output_file outfile_name
        Same as -o
"""

import httplib2 #To send the request using http protocol
import sys
import json #For parsing the server response
import time #To add the current date to the standard file name
import argparse #To add options

if __name__=='__main__':

    print "; Job started", time.strftime("%Y-%m-%d_%Hh%M")
    
    #To check the role of this line: 
    http = httplib2.Http(".cache")
    
    #Specify the url of the server  
    resp, content = http.request("http://mgalileo.genoscope.cns.fr/api/microscope/organisms/list", method="GET", headers={"Content-Type":"application/json"})
    
    #Check the status of the request: 
    if not resp.status == 200:
        print "Invalid response: ", resp.status
        sys.exit()
    
    #Response return as a JSON object : here an array containing dictionaries with each organism's informations 
    decoded = json.loads(content)         
    
    #Current date
    date = time.strftime("%Y_%m_%d")
    
    #Creation of arguments
    parser = argparse.ArgumentParser(add_help = True)
    parser.add_argument('-o','--output_file', action = 'store', dest = 'outfile_n', default = False, help = 'The user can choose the outfile name')
    args = parser.parse_args()
    
    outfile_n, outfileN = args.outfile_n, args.outfileN
    
    #Choose of the outfile name
    if outfile_n:
        outfile = outfile_n #File name chosen by the user
    elif outfileN:
            outfile = outfileN  #File name chosen by the user
    else:
        outfile = "supported_organisms_genoscope_" + date + ".tab" #Standard file name according to the date
    
    
    #Creation of the tabulated file
    f = open(outfile, "w")
    
    f.write("Species Code" + '\t' + "DirName" + '\t' + "GRAM" + '\t' + "ID" + '\t' + "Synonym" + '\t' + "Taxon" + '\t' + "Nombre de replic" + '\t' + "Kingdom" + '\t' + "Name" + '\t' + "Strain" + '\n')
    
    for i in range(len(decoded)):
        dict_species = decoded[i]
    
    #file_content variable contents all strings matching to each value of the dictionary for a given species separated by tabulations    
        file_content = str(dict_species[u'ospeciesCode']) + '\t' + str(dict_species[u'odirname']) + '\t' + str(dict_species[u'ogram']) + '\t' + str(dict_species[u'oid']) + '\t' + str(dict_species[u'osynonym']) + '\t' + str(dict_species[u'otaxon']) + '\t' + str(dict_species[u'oreplicNb']) + '\t' + str(dict_species[u'okingdom']) + '\t' + str(dict_species[u'oname']) + '\t' + str(dict_species[u'ostrain'])
        
        f.write(file_content + '\n')
    
    f.close()
    
    print "; Job done, Job name :", outfile, time.strftime("%Y-%m-%d_%Hh%M")
