O seguinte deve funcionar com o python 2 e 3, salvar como xyz.py
e executar com% python xyz.py file_1 file_2 file_3
:
import sys
import csv
names = set() # to keep track of all sequence names
files = {} # map of file_name to dict of sequence_names mapped to counts
# counting
for file_name in sys.argv[1:]:
# lookup the file_name create a new dict if not in the files dict
b = files.setdefault(file_name, {})
with open(file_name) as fp:
for line in fp:
x = line.strip().split() # split the line
names.add(x[1]) # might be a new sequence name
# retrieve the sequence name or set it if not there yet
# what would not work is "i += 1" as you would need to assign
# that to b[x[1]] again. The list "[0]" however is a reference
b.setdefault(x[1], [0])[0] += 1
# output
names = sorted(list(names)) # sort the unique sequence names for the columns
grid = []
# create top line
top_line = ['taxa']
grid.append(top_line)
for name in names:
top_line.append(name)
# append each files values to the grid
for file_name in sys.argv[1:]:
data = files[file_name]
line = [file_name]
grid.append(line)
for name in names:
line.append(data.get(name, [0])[0]) # 0 if sequence name not in file
# dump the grid to CSV
with open('out.csv', 'w') as fp:
writer = csv.writer(fp)
writer.writerows(grid)
Usar [0]
para os contadores facilita a atualização do valor do que usar inteiros diretamente. Se os arquivos de entrada forem mais complexos, é melhor lê-los com a biblioteca CSV do Python