"""
This script is for creating a sample manifest with the forward
and reverse reads and sample names all contained in a tab delimited file

"""

import os, sys, Levenshtein

for idx, an_arg in enumerate(sys.argv):
    if an_arg == "-i":
        baseDir = sys.argv[idx+1]
    if an_arg == "-h" or an_arg == "--help":
        print("""
This script takes all the FASTQ files in a provided directory
and generates a sample manifest for use in a mapping loop
later on. It accepts `-i` as an argument flag, and the 
absolute path of the directory as an argument\n""")

try:
    print("Trying to locate "+ baseDir)
except NameError:
    print("Input improperly specified. See -h option")

if os.path.exists(baseDir):
    files = sorted(os.listdir(baseDir))

sample_pairs_1 = []

for i1 in files:
    for i2 in files:
        this_dist = Levenshtein.distance(i1, i2)
        if this_dist > 0 and this_dist < 2:
            sample_pairs_1.append(sorted([i1, i2]))

sample_pairs = set(tuple(i) for i in sample_pairs_1)
print("{} files in provided directory".format(len(files)))
print("{} sample pairs located".format(len(sample_pairs)))

with open("samples.txt", "w") as fh:
    for sp in sample_pairs:
        f1, f2 = sp[0], sp[1]
        s_ = f1.split(".")[0]
        p1, p2 = os.path.join(baseDir, f1), os.path.join(baseDir, f2)
        this_line = "{}\t{}\t{}\n".format(s_, p1, p2)
        fh.write(this_line)
