-
Notifications
You must be signed in to change notification settings - Fork 0
/
listtotable_rev.py
54 lines (39 loc) · 1.41 KB
/
listtotable_rev.py
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
# -*- coding: utf-8 -*-
"""
Reads lists and then make an abundance table from it.
"""
import argparse
from pandas import DataFrame
import ntpath
def makelist(filename):
with open(filename) as listfile:
genuslist = []
for line in listfile:
genuslist.append(line[:-1])
genuslist.sort()
return genuslist
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
parser = argparse.ArgumentParser(description="Read lists and make a table.")
parser.add_argument("-f", "--file", dest="fpath", nargs='+', help="Filepath of lists")
parser.add_argument("-o", "--output", dest="ofile", help="Specify output file.")
args = parser.parse_args()
genustable = []
genuscount = {}
for i in args.fpath:
genustable.append(makelist(i))
for j in range(len(genustable)):
for i in range(len(genustable[j])):
temp_str = str(genustable[j][i])
if temp_str not in genuscount:
genuscount.setdefault(temp_str, [])
for k in range(len(genustable)):
genuscount[temp_str].append(0)
genuscount[temp_str][j] += 1
else:
genuscount[temp_str][j] += 1
filenames = [path_leaf(path) for path in args.fpath]
genusdata = DataFrame(data=genuscount, index=filenames)
genusdata = genusdata.transpose()
genusdata.to_csv(args.ofile, sep='\t')