import sys
import os
import pandas as pd

def usage():
    '''
    This script only works for XXX.exon.gene_name.counts.txt.TPM.txt file which generated by featureCounts2TPM.py.

    usage:
    python merge.tpm.py /path/work/to/*.counts.TPM.txt > test.txt

    update:
    20221103  fix TPM numeric type to float. e.g.(0.00 to 0)
    20210713. fix bug: GB001003-Tumor-pc-P6 key error.
    20210204. added usage description.
    2020xxxx. created.
    '''
    pass


def main():
    # pd.options.display.float_format = '{:,.0f}'.format
    big_DataFrame = pd.DataFrame()
    for each in sys.argv[1:]:
        sample_name = os.path.basename(each).split(".")[0] # this may be a bug if name format does not fit the nameformat.
        df1 = pd.read_table(each, sep="\t", header=None, names=["Gene", sample_name])
        # df1[sample_name] = df1[sample_name].astype(str)
        if big_DataFrame.empty :
            big_DataFrame = df1
        else:
            big_DataFrame = pd.merge(big_DataFrame, df1, on='Gene', how='outer')
    print(big_DataFrame.to_csv(sep="\t", index=False))


if __name__ == '__main__':
    main()
