【发布时间】:2017-05-03 19:51:50
【问题描述】:
我正在从我当前的工作目录运行我的脚本。使用我的脚本,我遍历当前工作目录的子目录。每个子目录包含脚本中提到的 3 个文件,对于每个子目录,我将这 3 个文件合并到一个数据帧中。就像我的脚本现在一样,它只将一个子目录的合并数据框写入当前工作目录。我想要的是 csv 文件,其中每个子目录的合并数据帧保存在该子目录中,或者每个子目录的数据帧连接到一个大输出文件的文件。 使用我的脚本,我的输出文件中只有一个子目录的输出。
我的脚本如下:
print('Start merging contig files')
for root, dirs, files in os.walk(os.getcwd()):
filepath = os.path.join(root, 'genes.faa.genespercontig.csv')
if os.path.isfile(filepath):
with open(filepath, 'r') as f1:
df1 = pd.read_csv(f1, header=None, delim_whitespace=True, names = ["contig", "genes"])
df1['genome'] = os.path.basename(os.path.dirname(filepath))
filepath = os.path.join(root, 'hmmer.analyze.txt.results.txt')
if os.path.isfile(filepath):
with open(filepath, 'r') as f2:
df2 = pd.read_csv(f2, header=None, delim_whitespace=True, names = ["contig", "SCM"])
df2['genome'] = os.path.basename(os.path.dirname(filepath))
filepath = os.path.join(root, 'genes.fna.output_blastplasmiddb.out.count_plasmiddbhit.out')
if os.path.isfile(filepath):
with open(filepath, 'r') as f3:
df3 = pd.read_csv(f3, header=None, delim_whitespace=True, names = ["contig", "plasmid_genes"])
df3['genome'] = os.path.basename(os.path.dirname(filepath))
#merge dataframes
dfmerge1 = pd.merge(df1, df2, on=['genome', 'contig'], how='outer')
df_end = pd.merge(dfmerge1, df3, on=['genome', 'contig'], how='outer')
df_end.to_csv('outputgenesdf.csv')
【问题讨论】: