# Path: seqcluster/libs/read.py
# def load_data(in_file):
#     """load json file from seqcluster cluster"""
#     with open(in_file) as in_handle:
#         return json.load(in_handle)
# 
# def get_sequences_from_cluster(c1, c2, data):
#     """get all sequences from on cluster"""
#     seqs1 = data[c1]['seqs']
#     seqs2 = data[c2]['seqs']
#     seqs = list(set(seqs1 + seqs2))
#     names = []
#     for s in seqs:
#         if s in seqs1 and s in seqs2:
#             names.append("both")
#         elif s in seqs1:
#             names.append(c1)
#         else:
#             names.append(c2)
#     return seqs, names
# 
# def map_to_precursors(seqs, names, loci, out_file, args):
#     """map sequences to precursors with razers3"""
#     with make_temp_directory() as temp:
#         pre_fasta = os.path.join(temp, "pre.fa")
#         seqs_fasta = os.path.join(temp, "seqs.fa")
#         out_sam = os.path.join(temp, "out.sam")
#         pre_fasta = get_loci_fasta(loci, pre_fasta, args.ref)
#         out_precursor_file = out_file.replace("tsv", "fa")
#         seqs_fasta = get_seqs_fasta(seqs, names, seqs_fasta)
# 
#         # print(open(pre_fasta).read().split("\n")[1])
#         if find_cmd("razers3"):
#             cmd = "razers3 -dr 2 -i 80 -rr 90 -f -o {out_sam} {temp}/pre.fa  {seqs_fasta}"
#             run(cmd.format(**locals()))
#             out_file = read_alignment(out_sam, loci, seqs, out_file)
#             shutil.copy(pre_fasta, out_precursor_file)
#     return out_file
# 
# def get_precursors_from_cluster(c1, c2, data):
#     loci1 = data[c1]['loci']
#     loci2 = data[c2]['loci']
#     return {c1:loci1, c2:loci2}


import os
import logging
from seqcluster.libs.read import load_data, get_sequences_from_cluster, map_to_precursors, get_precursors_from_cluster
from __future__ import print_function
#import sys
#from os import listdir
#from os.path import isfile, join


logger = logging.getLogger('explore')


def explore(args):
    """Create mapping of sequences of two clusters
    """
    logger.info("Reading sequences")
    data = load_data(args.json)
    logger.info("Get sequences from json")
    #get_sequences_from_cluster()
    c1, c2 = args.names.split(",")
    seqs, names = get_sequences_from_cluster(c1, c2, data[0])

# Path: seqcluster/libs/read.py
# def load_data(in_file):
#     """load json file from seqcluster cluster"""
#     with open(in_file) as in_handle:
#         return json.load(in_handle)
# 
# def get_sequences_from_cluster(c1, c2, data):
#     """get all sequences from on cluster"""
#     seqs1 = data[c1]['seqs']
#     seqs2 = data[c2]['seqs']
#     seqs = list(set(seqs1 + seqs2))
#     names = []
#     for s in seqs:
#         if s in seqs1 and s in seqs2:
#             names.append("both")
#         elif s in seqs1:
#             names.append(c1)
#         else:
#             names.append(c2)
#     return seqs, names
# 
# def map_to_precursors(seqs, names, loci, out_file, args):
#     """map sequences to precursors with razers3"""
#     with make_temp_directory() as temp:
#         pre_fasta = os.path.join(temp, "pre.fa")
#         seqs_fasta = os.path.join(temp, "seqs.fa")
#         out_sam = os.path.join(temp, "out.sam")
#         pre_fasta = get_loci_fasta(loci, pre_fasta, args.ref)
#         out_precursor_file = out_file.replace("tsv", "fa")
#         seqs_fasta = get_seqs_fasta(seqs, names, seqs_fasta)
# 
#         # print(open(pre_fasta).read().split("\n")[1])
#         if find_cmd("razers3"):
#             cmd = "razers3 -dr 2 -i 80 -rr 90 -f -o {out_sam} {temp}/pre.fa  {seqs_fasta}"
#             run(cmd.format(**locals()))
#             out_file = read_alignment(out_sam, loci, seqs, out_file)
#             shutil.copy(pre_fasta, out_precursor_file)
#     return out_file
# 
# def get_precursors_from_cluster(c1, c2, data):
#     loci1 = data[c1]['loci']
#     loci2 = data[c2]['loci']
#     return {c1:loci1, c2:loci2}


# Path: seqcluster/explore_cluster.py
import os
import logging
from seqcluster.libs.read import load_data, get_sequences_from_cluster, map_to_precursors, get_precursors_from_cluster

from __future__ import print_function
#import sys
#from os import listdir
#from os.path import isfile, join


logger = logging.getLogger('explore')


def explore(args):
    """Create mapping of sequences of two clusters
    """
    logger.info("Reading sequences")
    data = load_data(args.json)
    logger.info("Get sequences from json")
    #get_sequences_from_cluster()
    c1, c2 = args.names.split(",")
    seqs, names = get_sequences_from_cluster(c1, c2, data[0])

    loci = get_precursors_from_cluster(c1, c2, data[0])