import numpy as np
import names
import random
import os

random.seed(0)

def get_file_stats(file_path, output_path):
    line_lengths = []
    with open(file_path, 'r') as fi:
        for line in fi:
            line = line.split('",')[1].strip()
            line_lengths.append(len(line))
    return np.max(line_lengths), np.min(line_lengths), np.mean(line_lengths)

def make_reads(file_path, output_path, read_size = 1000, num_of_reads = 1000):
    i = 0
    with open(output_path, 'w') as fo:
        with open(file_path, 'r') as fi:
            for line in fi:
                line = line.split('",')[1].strip()
                possible_number_of_reads = (int)(len(line)/read_size)
                for k in range(0, possible_number_of_reads):
                    l = line[read_size*k:read_size*k+read_size]
                    if any(x not in ['A', 'T', 'C', 'G', 'N'] for x in set(list(l))):
                        continue
                    fo.write(l+'\n')
                    i += 1
                    if i == num_of_reads:
                        return

def make_payloads(payload_output_path, max_name_length, num_of_names):
    i = 0
    with open(payload_output_path, 'w') as fo:
        while True:
            name = names.get_full_name()
            if len(name) > 10:
                continue
            parts = name.split(" ")
            port = random.randint(1,9999)
            domain = parts[0]+'.'+parts[1]+':'+str(port)
            fo.write(domain+'\n')
            i += 1
            if i == num_of_names:
                break
def main():
    data_path = './experiment_data/seed_data/'
    os.makedirs(data_path, exist_ok=True)
    file_path = '../addgene_data_collection/all_plasmid_dna.txt'
    output_path = data_path+'/hosts.txt'
    read_size = 1000
    num_of_reads = 1000 # getting all reads use -1
    print('max, min and average sequence sizes: ', get_file_stats(file_path, output_path))
    make_reads(file_path, output_path, read_size, num_of_reads)

    payload_output_path = data_path+'/payloads.txt'
    max_name_length = 5
    num_of_names = 1000
    make_payloads(payload_output_path, max_name_length, num_of_names)


if __name__ == '__main__':
    main()



