<commit_before>
#!/usr/bin/env python

# standard library imports
import os
import sys
import traceback
import argparse
import json
import logging
import io
import pprint
import string

# 3rd party imports
import requests

# KBase imports
import biokbase.Transform.script_utils as script_utils 
import biokbase.workspace.client 


def insert_newlines(string, every):
    lines = []
    for i in xrange(0, len(string), every):
        lines.append(string[i:i+every])
    return '\n'.join(lines)+"\n"

# conversion method that can be called if this module is imported
# Note the logger has different levels it could be run.  See: https://docs.python.org/2/library/logging.html#logging-levels
# The default level is set to INFO which includes everything except DEBUG
def convert(workspace_service_url, shock_service_url, handle_service_url, workspace_name, object_name, object_id, object_version_number, working_directory, level=logging.INFO, logger=None): 
 
    """
    Converts KBaseAssembly.SingleEndLibrary to a Fasta file of assembledDNA.
    Args:
        workspace_service_url :  A url for the KBase Workspace service 
        shock_service_url: A url for the KBase SHOCK service.
        handle_service_url: A url for the KBase Handle Service.
        workspace_name : Name of the workspace
        object_name : Name of the object in the workspace 
        object_version_number : Version number of workspace object (ContigSet), defaults to most recent version
        working_directory : The working directory for where the output file should be stored.
        level: Logging level, defaults to logging.INFO.
    """ 

    if logger is None:
        logger = script_utils.getStderrLogger(__file__)
    
    logger.info("Starting conversion of KBaseGenomes.ContigSet to FASTA.DNA.Assembly")
    token = os.environ.get('KB_AUTH_TOKEN')
    
    if not os.path.isdir(args.working_directory): 
        raise Exception("The working directory does not exist {0} does not exist".format(working_directory)) 

    logger.info("Grabbing Data.")
 
    try:
        ws_client = biokbase.workspace.client.Workspace(workspace_service_url) 
        if object_version_number and object_name :
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'name':object_name, 'ver':object_version_number}])[0] 
        elif object_name:
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'name':object_name}])[0]
        elif object_version_number and object_id :
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'objid':object_id, 'ver':object_version_number}])[0]
        else:
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'objid':object_id}])[0] 
    except Exception, e: 
        logger.exception("Unable to rerieve ws object.")
        logger.exception("".join(traceback.format_exc()))
        raise 

    shock_id = None 
    if "fasta_ref" in contig_set : 
        shock_id  = contig_set['data']['fasta_ref'] 
        logger.info("Retrieving data from Shock.")
        script_utils.download_file_from_shock(logger, shock_service_url, shock_id, working_directory, token) 
        
    else: 
        ws_object_name = contig_set['info'][1]
        valid_chars = "-_.(){0}{1}"format(string.ascii_letters, string.digits)
        output_file_name = ''
        output_file_chars = list()
        for character in ws_object_name:
            if character in valid_chars:
                output_file_chars.append(character)
            else:
                output_file_chars.append('_')
        if len(output_file_chars) == 0:
            output_file_name = "ContigSetFile"
        else:
            output_file_name = ''.join(output_file_chars)

        logger.warning("The ContigSet does not have a fasta_ref to shock.  The fasta file will be attempted to be built from contig sequences in the object.")
        
        output_file =  os.path.join(working_directory,output_file_name)
 
        outFile = open(output_file, 'w') 
        for contig in contig_set['data']['contigs']:
            outFile.write(">{}\n".format(contig['name']))
            #do 80 nucleotides per line
            if contig['sequence'] != '':
                outFile.write(insert_new_lines(contig['sequence'],80))
            else:
                logger.warning("The ContigSet does not have a fasta_ref to shock or sequences in the contigs. A fasta file can not be created.")
                outFile.close()    
                return
        outFile.close()    

    logger.info("Conversion completed.")


# called only if script is run from command line
if __name__ == "__main__":	
    parser = argparse.ArgumentParser(prog='trns_transform_KBaseGenomes_ContigSet_to_FASTA_DNA_Assembly.py', 
                                     description='Converts ContigSet json string into a FASTA file.',
                                     epilog='Authors: <NAME>, <NAME>')
    # The following 8 arguments should be fairly standard to all uploaders
    parser.add_argument('--workspace_service_url', help='Workspace service url', action='store', type=str, nargs='?', required=True)<commit_msg>Did testing fixed minor bugs<commit_after>
#!/usr/bin/env python

# standard library imports
import os
import sys
import traceback
import argparse
import json
import logging
import io
import pprint
import string

# 3rd party imports
import requests

# KBase imports
import biokbase.Transform.script_utils as script_utils 
import biokbase.workspace.client 


def insert_newlines(string, every):
    lines = []
    for i in xrange(0, len(string), every):
        lines.append(string[i:i+every])
    return '\n'.join(lines)+"\n"

# conversion method that can be called if this module is imported
# Note the logger has different levels it could be run.  See: https://docs.python.org/2/library/logging.html#logging-levels
# The default level is set to INFO which includes everything except DEBUG
def convert(workspace_service_url, shock_service_url, handle_service_url, workspace_name, object_name, object_id, object_version_number, working_directory, level=logging.INFO, logger=None): 
 
    """
    Converts KBaseAssembly.SingleEndLibrary to a Fasta file of assembledDNA.
    Args:
        workspace_service_url :  A url for the KBase Workspace service 
        shock_service_url: A url for the KBase SHOCK service.
        handle_service_url: A url for the KBase Handle Service.
        workspace_name : Name of the workspace
        object_name : Name of the object in the workspace 
        object_version_number : Version number of workspace object (ContigSet), defaults to most recent version
        working_directory : The working directory for where the output file should be stored.
        level: Logging level, defaults to logging.INFO.
    """ 

    if logger is None:
        logger = script_utils.getStderrLogger(__file__)
    
    logger.info("Starting conversion of KBaseGenomes.ContigSet to FASTA.DNA.Assembly")
    token = os.environ.get('KB_AUTH_TOKEN')
    
    if not os.path.isdir(args.working_directory): 
        raise Exception("The working directory does not exist {0} does not exist".format(working_directory)) 

    logger.info("Grabbing Data.")
 
    try:
        ws_client = biokbase.workspace.client.Workspace(workspace_service_url) 
        if object_version_number and object_name :
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'name':object_name, 'ver':object_version_number}])[0] 
        elif object_name:
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'name':object_name}])[0]
        elif object_version_number and object_id :
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'objid':object_id, 'ver':object_version_number}])[0]
        else:
            contig_set = ws_client.get_objects([{'workspace':workspace_name,'objid':object_id}])[0] 
    except Exception, e: 
        logger.exception("Unable to rerieve ws object.")
        logger.exception("".join(traceback.format_exc()))
        raise 

    shock_id = None 
    if "fasta_ref" in contig_set : 
        shock_id  = contig_set['data']['fasta_ref'] 
        logger.info("Retrieving data from Shock.")
        script_utils.download_file_from_shock(logger, shock_service_url, shock_id, working_directory, token) 
        
    else: 
        ws_object_name = contig_set['info'][1]
        valid_chars = "-_.(){0}{1}".format(string.ascii_letters, string.digits)
        output_file_name = ''
        output_file_chars = list()
        for character in ws_object_name:
            if character in valid_chars:
                output_file_chars.append(character)
            else:
                output_file_chars.append('_')
        if len(output_file_chars) == 0:
            output_file_name = "ContigSetFile"
        else:
            output_file_name = ''.join(output_file_chars)

        logger.warning("The ContigSet does not have a fasta_ref to shock.  The fasta file will be attempted to be built from contig sequences in the object.")
        
        output_file =  os.path.join(working_directory,output_file_name)

        outFile = open(output_file, 'w') 
        for contig in contig_set['data']['contigs']:
            outFile.write(">{}\n".format(contig['id']))
            #do 80 nucleotides per line
            if contig['sequence'] != '':
                outFile.write(insert_newlines(contig['sequence'],80))
            else:
                logger.warning("The ContigSet does not have a fasta_ref to shock or sequences in the contigs. A fasta file can not be created.")
                outFile.close()    
                return
        outFile.close()    

    logger.info("Conversion completed.")


# called only if script is run from command line
if __name__ == "__main__":	
    parser = argparse.ArgumentParser(prog='trns_transform_KBaseGenomes_ContigSet_to_FASTA_DNA_Assembly.py', 
                                     description='Converts ContigSet json string into a FASTA file.',
                                     epilog='Authors: <NAME>, <NAME>')
    # The following 8 arguments should be fairly standard to all uploaders
    parser.add_argument('--workspace_service_url', help='Workspace service url', action='store', type=str, nargs='?', required=True)
{'fraction_non_alphanumeric': 0.09116450300329393, 'fraction_numerical': 0.003293935283859717, 'mean_word_length': 3.0466483731869856, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '830544', 'n_tokens_mistral': 3022, 'n_tokens_neox': 2875, 'n_words': 791}