"""
Remove Fragments not in Knowledgebase
"""

__author__ = "Michael Suarez"
__email__ = "masv@connect.ust.hk"
__copyright__ = "Copyright 2019, Hong Kong University of Science and Technology"
__license__ = "3-clause BSD"

from argparse import ArgumentParser
import numpy as np
import pickle

parser = ArgumentParser(description="Build Files")
parser.add_argument("--datadir", type=str, default="Data", help="input - XXX.YYY ")
parser.add_argument("--envNewAcronym", type=str, default="PRT.SNW", help="input - XXX.YYY ")

args = parser.parse_args()


# Check the Bound Fragments

BoundFrags = np.loadtxt("../%s/%s/%s.Homogenised.boundfrags_zeros.txt" %(args.datadir, args.envNewAcronym, args.envNewAcronym), delimiter=',')

normalDF = pickle.load(open("../%s/GrandCID.dict" %(args.datadir), "rb"))

binding = np.full(BoundFrags.shape,-1)
mlength = 0
for r, i in enumerate(BoundFrags):
    for c, j in enumerate(i[i!=0]):
        try:
            # Checks whether the Fragment can be found in the 59k Fragment Base
            binding[r,c]=normalDF.index.get_loc(int(j))
        except:
            continue
    temp = binding[r]
    if temp[temp!=-1].shape[0] > mlength:
        mlength = temp[temp!=-1].shape[0]
        print(mlength) #Finds the maximum number of Fragments per environment -> 705
        
indices = np.empty(binding.shape[0])
red_binding = np.full((binding.shape[0], mlength), -1)
for j, i in enumerate(binding):
    indices[j] = i[i!=-1].shape[0]
    red_binding[j][:int(indices[j])] = i[i!=-1]
red_binding = np.delete(red_binding, np.where(indices==0), axis=0)

pickle.dump(red_binding, open("../%s/%s/%s.binding.mtr"  %(args.datadir, args.envNewAcronym, args.envNewAcronym), "wb"))

# Removes environments without binding Fragments
Features_all = pickle.load(open("../%s/%s/%s.Homogenised.property.pvar"  %(args.datadir, args.envNewAcronym, args.envNewAcronym), "rb"))
Features_all = np.delete(Features_all, np.where(indices==0), axis=0)
pickle.dump(Features_all, open("../%s/%s/%s.Homogenised.property.pvar"  %(args.datadir, args.envNewAcronym, args.envNewAcronym), "wb"))

# Removes environment annotiation without binding fragments
with open("../%s/%s/%s.Homogenised.annotation.txt" %(args.datadir, args.envNewAcronym, args.envNewAcronym), "r+") as f:
    lines = f.readlines()
    for i in np.where(indices==0)[0][::-1]:
        del lines[i]
    f.seek(0)
    f.truncate()
    f.writelines(lines)

[((301, 342), 'argparse.ArgumentParser', 'ArgumentParser', ([], {'description': '"""Build Files"""'}), "(description='Build Files')\n", (315, 342), False, 'from argparse import ArgumentParser\n'), ((592, 726), 'numpy.loadtxt', 'np.loadtxt', (["('../%s/%s/%s.Homogenised.boundfrags_zeros.txt' % (args.datadir, args.\n    envNewAcronym, args.envNewAcronym))"], {'delimiter': '""","""'}), "('../%s/%s/%s.Homogenised.boundfrags_zeros.txt' % (args.datadir,\n    args.envNewAcronym, args.envNewAcronym), delimiter=',')\n", (602, 726), True, 'import numpy as np\n'), ((808, 837), 'numpy.full', 'np.full', (['BoundFrags.shape', '(-1)'], {}), '(BoundFrags.shape, -1)\n', (815, 837), True, 'import numpy as np\n'), ((1316, 1342), 'numpy.empty', 'np.empty', (['binding.shape[0]'], {}), '(binding.shape[0])\n', (1324, 1342), True, 'import numpy as np\n'), ((1357, 1397), 'numpy.full', 'np.full', (['(binding.shape[0], mlength)', '(-1)'], {}), '((binding.shape[0], mlength), -1)\n', (1364, 1397), True, 'import numpy as np\n'), ((1550, 1572), 'numpy.where', 'np.where', (['(indices == 0)'], {}), '(indices == 0)\n', (1558, 1572), True, 'import numpy as np\n'), ((1928, 1950), 'numpy.where', 'np.where', (['(indices == 0)'], {}), '(indices == 0)\n', (1936, 1950), True, 'import numpy as np\n'), ((2314, 2336), 'numpy.where', 'np.where', (['(indices == 0)'], {}), '(indices == 0)\n', (2322, 2336), True, 'import numpy as np\n')]