# Path: augur/utils.py
# def write_json(data, file_name, indent=(None if os.environ.get("AUGUR_MINIFY_JSON") else 2), include_version=True):
#     """
#     Write ``data`` as JSON to the given ``file_name``, creating parent directories
#     if necessary. The augur version is included as a top-level key "augur_version".
# 
#     Parameters
#     ----------
#     data : dict
#         data to write out to JSON
#     file_name : str
#         file name to write to
#     indent : int or None, optional
#         JSON indentation level. Default is `None` if the environment variable `AUGUR_MINIFY_JSON`
#         is truthy, else 1
#     include_version : bool, optional
#         Include the augur version. Default: `True`.
# 
#     Raises
#     ------
#     OSError
#     """
#     #in case parent folder does not exist yet
#     parent_directory = os.path.dirname(file_name)
#     if parent_directory and not os.path.exists(parent_directory):
#         try:
#             os.makedirs(parent_directory)
#         except OSError: #Guard against race condition
#             if not os.path.isdir(parent_directory):
#                 raise
# 
#     if include_version:
#         data["generated_by"] = {"program": "augur", "version": get_augur_version()}
# 
#     with open(file_name, 'w', encoding='utf-8') as handle:
#         json.dump(data, handle, indent=indent, sort_keys=True)
# 
# def get_json_name(args, default=None):
#     if args.output_node_data:
#         return args.output_node_data
#     else:
#         if default:
#             print("WARNING: no name for the output file was specified. Writing results to %s."%default, file=sys.stderr)
#             return default
#         else:
#             raise ValueError("Please specify a name for the JSON file containing the results.")


import sys
import numpy as np
    import numpy as np
    import pandas as pd
from treetime.vcf_utils import read_vcf
from collections import defaultdict
from .utils import write_json, get_json_name
    from Bio import SeqIO

    '''
    print("This method may change in future! Please use 'augur sequence-traits -h' to check the latest options.")
    print("Unfortunately this method currently only works with VCF input.")
    ## check file format and read in sequences
    is_vcf = False
    if ( (args.ancestral_sequences and any([args.ancestral_sequences.lower().endswith(x) for x in ['.vcf', '.vcf.gz']])) or
        (args.translations and any([args.translations.lower().endswith(x) for x in ['.vcf', '.vcf.gz']])) ):
        if ((args.ancestral_sequences and not args.vcf_reference) or
            (args.translations and not args.vcf_translate_reference)):
            print("ERROR: a reference Fasta is required with VCF-format alignments")
            return 1
        is_vcf = True
        compress_seq = defaultdict(dict)
        if args.translations:
            compress_seq = read_in_translate_vcf(args.translations, args.vcf_translate_reference)
        if args.ancestral_sequences:
            compress_seq["nuc"] = read_vcf(args.ancestral_sequences, args.vcf_reference)
    else:
        # TO-DO fill in fasta-format processing
        aln = args.ancestral_sequences
        print("\nERROR: Unfortunately this feature currently only works with VCF input! It will be expanded to work with Fasta-input soon.")
        return 1

    features = read_in_features(args.features)
    annotations = annotate_strains(features, compress_seq)
    #convert the annotations into string label that auspice can display
    seq_features = attach_features(annotations, args.label, args.count)

    #write out json
    out_name = get_json_name(args)

# Path: augur/utils.py
# def write_json(data, file_name, indent=(None if os.environ.get("AUGUR_MINIFY_JSON") else 2), include_version=True):
#     """
#     Write ``data`` as JSON to the given ``file_name``, creating parent directories
#     if necessary. The augur version is included as a top-level key "augur_version".
# 
#     Parameters
#     ----------
#     data : dict
#         data to write out to JSON
#     file_name : str
#         file name to write to
#     indent : int or None, optional
#         JSON indentation level. Default is `None` if the environment variable `AUGUR_MINIFY_JSON`
#         is truthy, else 1
#     include_version : bool, optional
#         Include the augur version. Default: `True`.
# 
#     Raises
#     ------
#     OSError
#     """
#     #in case parent folder does not exist yet
#     parent_directory = os.path.dirname(file_name)
#     if parent_directory and not os.path.exists(parent_directory):
#         try:
#             os.makedirs(parent_directory)
#         except OSError: #Guard against race condition
#             if not os.path.isdir(parent_directory):
#                 raise
# 
#     if include_version:
#         data["generated_by"] = {"program": "augur", "version": get_augur_version()}
# 
#     with open(file_name, 'w', encoding='utf-8') as handle:
#         json.dump(data, handle, indent=indent, sort_keys=True)
# 
# def get_json_name(args, default=None):
#     if args.output_node_data:
#         return args.output_node_data
#     else:
#         if default:
#             print("WARNING: no name for the output file was specified. Writing results to %s."%default, file=sys.stderr)
#             return default
#         else:
#             raise ValueError("Please specify a name for the JSON file containing the results.")


# Path: augur/sequence_traits.py
import sys
import numpy as np
    import numpy as np
    import pandas as pd
from treetime.vcf_utils import read_vcf
from collections import defaultdict
from .utils import write_json, get_json_name
    from Bio import SeqIO

    '''
    print("This method may change in future! Please use 'augur sequence-traits -h' to check the latest options.")
    print("Unfortunately this method currently only works with VCF input.")
    ## check file format and read in sequences
    is_vcf = False
    if ( (args.ancestral_sequences and any([args.ancestral_sequences.lower().endswith(x) for x in ['.vcf', '.vcf.gz']])) or
        (args.translations and any([args.translations.lower().endswith(x) for x in ['.vcf', '.vcf.gz']])) ):
        if ((args.ancestral_sequences and not args.vcf_reference) or
            (args.translations and not args.vcf_translate_reference)):
            print("ERROR: a reference Fasta is required with VCF-format alignments")
            return 1
        is_vcf = True
        compress_seq = defaultdict(dict)
        if args.translations:
            compress_seq = read_in_translate_vcf(args.translations, args.vcf_translate_reference)
        if args.ancestral_sequences:
            compress_seq["nuc"] = read_vcf(args.ancestral_sequences, args.vcf_reference)
    else:
        # TO-DO fill in fasta-format processing
        aln = args.ancestral_sequences
        print("\nERROR: Unfortunately this feature currently only works with VCF input! It will be expanded to work with Fasta-input soon.")
        return 1

    features = read_in_features(args.features)
    annotations = annotate_strains(features, compress_seq)
    #convert the annotations into string label that auspice can display
    seq_features = attach_features(annotations, args.label, args.count)

    #write out json
    out_name = get_json_name(args)

    write_json({"nodes":seq_features},out_name)
