forked from nf-core/eager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_kraken_res.py
More file actions
executable file
·59 lines (43 loc) · 1.36 KB
/
merge_kraken_res.py
File metadata and controls
executable file
·59 lines (43 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
import argparse
import os
import pandas as pd
import numpy as np
def _get_args():
'''This function parses and return arguments passed in'''
parser = argparse.ArgumentParser(
prog='merge_kraken_res',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Merging csv count files in one table')
parser.add_argument(
'-o',
dest="output",
default="kraken_count_table.csv",
help="Output file. Default = kraken_count_table.csv")
args = parser.parse_args()
outfile = args.output
return(outfile)
def get_csv():
tmp = [i for i in os.listdir() if ".csv" in i]
return(tmp)
def _get_basename(file_name):
if ("/") in file_name:
basename = file_name.split("/")[-1].split(".")[0]
else:
basename = file_name.split(".")[0]
return(basename)
def merge_csv(all_csv):
df = pd.read_csv(all_csv[0], index_col=0)
for i in range(1, len(all_csv)):
df_tmp = pd.read_csv(all_csv[i], index_col=0)
df = pd.merge(left=df, right=df_tmp, on='TAXID', how='outer')
df.fillna(0, inplace=True)
return(df)
def write_csv(pd_dataframe, outfile):
pd_dataframe.to_csv(outfile)
if __name__ == "__main__":
OUTFILE = _get_args()
all_csv = get_csv()
resdf = merge_csv(all_csv)
write_csv(resdf, OUTFILE)
print(resdf)