User's input are the names of the experiments and a file with data. After data manipulation I have saved in a dict which sample (row) in the file corresponds to which experiment. For example, (sometimes sample can belong to either several experiments, or one or none)
用户输入是实验的名称和包含数据的文件。在数据操作之后,我保存在一个字典中,文件中的样本(行)对应于哪个实验。例如,(有时样本可以属于多个实验,或者一个或没有)
exp_sample["sample1"]=['experiment5','experiment6']
exp_sample["sample3"]=['experiment5']
While parsing the datafile again (for the second time) I want to write the sample row into the corresponding experiment file. Meaning, all experiment files should be opened while I am parsing through the datafile. My idea is following:
在再次解析数据文件时(第二次),我想将样本行写入相应的实验文件中。这意味着,我在解析数据文件时应该打开所有实验文件。我的想法如下:
experiment_files = {exp: open(exp+".fastq",'w') for exp in experiments}
for read in SeqIO.parse(fastq, 'fastq'):
experiment = exp_sample[sample.id]
#if sample belongs only to one experiment
#or sample belongs to two the same experiments
if len(experiment)==1 or (len(exp)==2 and (exp[0]==exp[1]))
SeqIO.write(read,exp_files[experiment[0]],'fastq')
(x.close() for x in experiment_files.values())
My question is, is it legit to open the files saved in the dict
and then close them in that way? Or is there any other cleverer way of doing it?
我的问题是,打开保存在dict中的文件然后以这种方式关闭它们是否合法?或者还有其他更聪明的方法吗?
PS. I know, I could have saved the sample rows into the lists of corresponding experiments and then write all experiment record in an experiment file but the datafile can be of several GB.
PS。我知道,我可以将样本行保存到相应实验的列表中,然后将所有实验记录写入实验文件,但数据文件可以是几GB。
1 个解决方案
#1
0
After we discussed at the comment I update my original answer.
我们在评论中讨论后,我更新了原来的答案。
If you want to open multiple files, you can do it with the context manager ExitStack
.
如果要打开多个文件,可以使用上下文管理器ExitStack来完成。
Here is an example code:
这是一个示例代码:
with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames]
Each element in the files
list represent one file that you open.
文件列表中的每个元素代表您打开的一个文件。
#1
0
After we discussed at the comment I update my original answer.
我们在评论中讨论后,我更新了原来的答案。
If you want to open multiple files, you can do it with the context manager ExitStack
.
如果要打开多个文件,可以使用上下文管理器ExitStack来完成。
Here is an example code:
这是一个示例代码:
with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames]
Each element in the files
list represent one file that you open.
文件列表中的每个元素代表您打开的一个文件。