【问题标题】:Create a table from the frequencies of taxa found in multiple CSV files根据在多个 CSV 文件中找到的分类群频率创建一个表
【发布时间】:2016-09-03 14:21:14
【问题描述】:

我有 12 个.csv 文件,其中包含提取的分类名称及其频率(提取每个名称的次数)。我创建了一个主 .txt 文件,列出了在 12 个文件中至少一次找到的所有独特分类群。我需要以csv 格式制作一个连接表,其中行的标题是每个文件的名称,列的标题是主.txt 文件中列出的所有唯一分类群。该表必须填充每个.csv 输入文件中每个分类单元旁边的频率。尽管主列表包含 12 个文件中所有可能的分类单元,但并非所有文件都包含所有分类单元。当分类单元丢失时,我需要放置一个“0”。

.csv 输入:

$cat file_1

1,Salmo salar
12,Solanum pennellii
18,Staphylococcus xylosus
...

$cat file_2

1,Salmo salar
14,Staphylococcus xylosus
123,Strongyloides stercoralis
...

$cat file_3

123,Solanum pennellii
11,Staphylococcus xylosus
41,Strongyloides stercoralis
...

.txt主列表:

$cat master

Salmo salar
Solanum pennellii
Staphylococcus xylosus
Strongyloides stercoralis
...

.csv 输出(我需要的):

Sample,Salmo salar,Solanum pennellii,Staphylococcus xylosus,Strongyloides stercoralis
File_1,1,12,18,0    
File_2,1,0,14,123    
File_3,0,123,11,41   

我之前尝试制作一个没有主列表的小型 Python 脚本,并使用包含重复分类群名称而不是频率的 .tsv 输入文件。我无法获得显示每个文件缺少分类群的表格,因此我决定创建一个主列表并折叠输入文件。我对python很陌生,所以任何帮助都将不胜感激。

【问题讨论】:

  • 我尝试在不使用主列表或折叠 csv 输入文件的情况下制作脚本(而不是具有频率,文件具有重复多次的相同名称),但它没有像我一样工作希望。

标签: python bash shell csv bioinformatics


【解决方案1】:

您根本不需要主文件。我只是动态生成决赛桌。假设您将输入文件名作为命令行参数传递给 Python 脚本:

import sys
from collections import defaultdict

data = defaultdict(dict) # { taxon: { filename: count } }                                                               

for filename in sys.argv[1:]:
    with open(filename) as infile:
        for line in infile:
            count, taxon = line.rstrip().split(',')
            data[taxon][filename] = count

现在您有了data,这是您输出文件所需的一切。然后你可以像这样打印它:

taxa = data.keys()
print "Sample,{}".format(','.join(taxa))
for filename in sys.argv[1:]:
    print filename,
    for taxon in taxa:
        count = data[taxon].get(filename, "0")
        sys.stdout.write("," + count)
    print

【讨论】:

  • 我可以在 shell 中将其称为python xyz.py file_1 file_2 file_3 吗? (我已经知道我安装了当前和以前版本的 python)。
  • @LuciaO:是的,这就是你调用它的方式。
  • 似乎当它到达print "Sample,{}".format(','.join(taxa)) 时,它没有要打印的信息。另外,脚本如何创建输出文件?我没有完全按照那一步,(对不起,我是python的新手)。
【解决方案2】:

对于那些即将awk的人,我们向你致敬!

awk 是为此类处理创建的。

试试这个:

awk -F "," -v OFS="," '
 FNR==1 {samples[++fni]=FILENAME}
 {if (!taxakeys[$2]) {taxakeys[$2]=1; taxas[++ti]=$2};frequencies[samples[fni],$2]+=$1}
 END {
   printf("Sample"); for (j=1;j<=ti;j++) { printf("%s%s",OFS,taxas[j])}; printf("\n") 
   for (i=1; i<=fni; i++) {
     printf("%s",samples[i]); for (j=1;j<=ti;j++) { printf("%s%d",OFS,frequencies[samples[i],taxas[j]])}; printf("\n")
   }
 }'

测试:

$ awk -F "," -v OFS="," '
 FNR==1 {samples[++fni]=FILENAME}
 {if (!taxakeys[$2]) {taxakeys[$2]=1; taxas[++ti]=$2};frequencies[samples[fni],$2]+=$1}
 END {
   printf("Sample"); for (j=1;j<=ti;j++) { printf("%s%s",OFS,taxas[j])}; printf("\n") 
   for (i=1; i<=fni; i++) {
     printf("%s",samples[i]); for (j=1;j<=ti;j++) { printf("%s%d",OFS,frequencies[samples[i],taxas[j]])}; printf("\n")
   }
 }' file_*

Sample,Salmo salar,Solanum pennellii,Staphylococcus xylosus,Strongyloides stercoralis
file_1,1,12,18,0
file_2,1,0,14,123
file_3,0,123,11,41

【讨论】:

    【解决方案3】:

    尝试使用 csv.Dictwriter

    1. 将您拥有的 12 个文件读入格式为 filename = {species_name: count, species_name:count} 的字典中。
    2. 将master_list txt文件读入列表
    3. 使用 csv.Dictwriter 从您创建的字典中写入一个 csv 文件。如果文件中没有物种的数据,您可以将其指定为 0。您的标题将是 master_list 中的物种列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 2011-02-06
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多