按“外部”选项卡合并子目录中的文件

2024-05-17 19:42:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件夹(split \u libs),其中的子文件夹根据SraRunTable3.txt第9列和第32列中描述的示例\u名称命名,每个都与sra \u研究相关。每个子文件夹中都有一个序号fna文件,不幸的是我无法更改其名称-它是一个QIIME命令的输出。你知道吗

我想合并序号fna根据sra\U研究,读取子文件夹名称(=示例\U名称),子文件夹中的文件。e、 g.所有序号fna来自同一个SRA的研究将被合并。你知道吗

目录概述示例:

split_libs
    sample1
      seqs.fna
    sample2
      seqs.fna
    sample3
      seqs.fna

SraRunTable的示例概述:

(...)Sample_Name(...)SRA_Study(...)
     sample_1        study_1
     sample_2        study_1 
     sample_3        study_2

以下是我迄今为止尝试过的:

import os
from operator import itemgetter

fields = itemgetter(9, 32)

with open('/home/andre/Desktop/PRJEB0000/SraRunTable3.txt') as csvfile:
next(csvfile)
for line in csvfile:
    sample_name, sra_study = fields(line.split())
for folder in os.listdir('./split_libs'):
    if folder == sample_name:
        open('seqs.fna') as infile, open('/home/andre/Desktop/PRJEB0000/cat_fna/' + sra_study + ".fna", 'a') as outfile:
            outfile.write(infile.read())

这个问题源于Joining files by corresponding columns in outside table

任何贡献将不胜感激!你知道吗


Tags: samplecsvfilein文件夹名称示例asopen
1条回答
网友
1楼 · 发布于 2024-05-17 19:42:59
import os
from operator import itemgetter

fields = itemgetter(9, 32)

with open('/home/andre/Desktop/PRJEB0000/SraRunTable3.txt') as csvfile:
next(csvfile)
for line in csvfile:
    sample_name, sra_study = fields(line.split())
    #open the folder corresponding to sample_name and add the seqs to the appropriate study file
    with open('split_libs/'+sample_name+'/seqs.fna') as infile, open('/home/andre/Desktop/PRJEB0000/cat_fna/' + sra_study + ".fna", 'a') as outfile:
            outfile.write(infile.read())

阿曼达·克莱尔的所有信用卡(未在Stackoverflow注册)!你知道吗

相关问题 更多 >