#this program uses a batch file created using mkbatch.py and uses this batchfile to create a batch feature array and batch label vector for further anaylsis
#arg 1 is used here to specifiy the ID number of the batch file and of the output feat array and label vector files
#__________________IMPORTS_________________________________________________________
import io
import os
import sys
import shutil
import numpy as np
import pandas as pd

#__________________MAIN_________________________________________________________


print("Starting "+sys.argv[1]+"...")

#read features which appaer in over 100 files
feats = pd.read_csv("o100freq6g.csv", header=None).to_numpy()[:,0]

#read batch file which contains list IDs of samples in the batch
batch = pd.read_csv(sys.argv[1], header=None).to_numpy(dtype=str)

numFeats = len(feats)
numSamps = len(batch)

#Save labels for batch
outY = open(sys.argv[1]+"Labels.csv", "w")
outY.write("\n".join(batch[:,1]))
outY.close()

#Creat file for batch vector array
out = open(sys.argv[1]+"Arr.csv", "w")
buf = io.StringIO()

#read sample vectors and combine them in an array outputted to file point to by "out"
for i in range(numSamps):
	vec = pd.read_csv("6vecs/"+batch[i][0]+".csv", header=None).to_numpy()
	for j in range(len(vec)-1): #add vector as row to matrix
		buf.write(str(vec[j, 0])+",")
	buf.write(str(vec[j+1,0])+"\n")
	buf.seek(0)
	shutil.copyfileobj(buf, out)
	buf.close()
	buf = io.StringIO()
buf.close()
out.close()
