#!/usr/bin/env python
import sys
import os
import argparse
import csv
import pandas as pd

def file_is_empty(path):
    return os.stat(path).st_size==0

def main():
    parser = argparse.ArgumentParser(description='Simple script to combine the results from the 3 counts.')
    parser.add_argument('--in_cbass','-c', dest='in_cbass', required=True, help='count for combined assembly')
    parser.add_argument('--in_ssu','-s', dest='in_ssu', required=True, help='count for SILVA SSU')
    parser.add_argument('--in_lsu','-l', dest='in_lsu', required=True, help='count for SILVA LSU')
    parser.add_argument('--out','-o', dest='out_file', required=True, help='output file')
    args = vars(parser.parse_args())
    ## Collect the 3 data frame, check they are ok, then combined in a single
    if not file_is_empty(args['in_cbass']):
        df_cbass = pd.read_csv(args['in_cbass'], sep='\t')
    else:
        sys.exit(f"{args['in_cbass']} is empty, we stop here")
    if not file_is_empty(args['in_ssu']):
        df_ssu = pd.read_csv(args['in_ssu'], sep='\t')
    else:
        sys.exit(f"{args['in_ssu']} is empty, we stop here")
    if not file_is_empty(args['in_lsu']):
        df_lsu = pd.read_csv(args['in_lsu'], sep='\t')
    else:
        sys.exit(f"{args['in_lsu']} is empty, we stop here")
    df_out = pd.concat([df_cbass, df_ssu, df_lsu], axis=0).sort_values(['sample','cell','ref'])
    df_out.to_csv(args['out_file'], sep="\t", index=False, float_format='%.3f')



if __name__ == "__main__":
	output = main()
