我有两个要比较的fastq文件。 fastq文件的结构如下(每次读取):
我正在使用的两个文件是:
File1 =包含我感兴趣的碱基的特定序列,以及它们对应的标题行(每次读取 2行)
File2 =用于比较的普通fastq文件(每次读取 4行)
我需要比较文件1和2以找到所有匹配的标题行,然后从file2保存相应的序列行。
文件1:
@HWI-D00461:137:C9H2FACXX:3:1101:1239:1968 1:N:0:GGCTAC
NTGTGTAATAGATTTTACTTTTGCCTTTAAGCCCAAGGTCCTGGACTTGAAACATCCAAGGGATGGAAAATGCCGTATAACAGGGTGGAAGAGAGATTTGA
@HWI-D00461:137:C9H2FACXX:3:1101:1117:1968 1:N:0:GGCTAC
NAAAGTCTACCAATTATACTTAGTGTGAAGAGGTGGGAGTTAAATATGACTTCCATTAATAGTTTCATTGTTTGGAAAACAGAGGTAATTTTTGATACAGA
@HWI-D00461:137:C9H2FACXX:3:1101:1087:1973 1:N:0:GGCTAC
NTAATCCAACTAACTAAAAATAAAAAGATTCAAATAGGTACAGAAAACAATGAAGGTGTAGAGGTGAGAAATCAACAGGATGTTCAGAAGCCTGTGTATGA
文件2:
@HWI-D00461:137:C9H2FACXX:3:1101:1239:1968 1:N:0:GGCTAC
NTGTGTAATAGATTTTACTTTTGCCTTTAAGCCCAAGGTCCTGGACTTGAAACATCCAAGGGATGGAAAATGCCGTATAACAGGGTGGAAGAGAGATTTGN
+
#1=BDDFFHHHFHIJJJJJJJJJJJJJJJJJJJJJIJJIJJJJJHJIIJHGIJJJJJJIHJJBGHJHIIJJJHHHHFFFFEEEDD;?BACDDDA?@CDDDC
@HWI-D00461:137:C9H2FACXX:3:1101:1117:1968 1:N:0:GGCTAC
NAAAGTCTACCAATTATACTTAGTGTGAAGAGGTGGGAGTTAAATATGACTTCCATTAATAGTTTCATTGTTTGGAAAACAGAGGTAATTTTTGATACNNN
+
#1=DDDFDFHHHGHIIGJJJJHIJIHHDIHHIJGGEI@GFGHIHIJHEFHIIIIGIJGHHGECFGIDHGIHIIEGIIJHHEEFFF7?ACEECCBBDEDDDC
@HWI-D00461:137:C9H2FACXX:3:1101:1200:1972 1:N:0:GGCTAC
NTACGTTTAGTAGAGACAGTGTCTTGCTATGTTGCCCAGGCTGGTCTCAAACTCCTGAGCTCTAGCAAGCCTTCCACCTCTGCCTCCCAGTGTTCTGGGAT
+
#1=DDDDFHHHBHGIGIIJHCDHHIJJJHEGFIIHFHGEGHJEIFHHHEFHHGIGIJEHIIJJJJIJIJIJGIIH.?CEFFFFDCEDD3>>@CDDDDDD<@
换句话说,对于每个匹配的标题行,我要保存其配对的顺序行。
最后,我想将所有这些标头和序列写到一个新文件中,以进行下游分析。
我当前的输出是:
@HWI-D00461:137:C9H2FACXX:3:1101:1357:1984 1:N:0:GGCTAC
@HWI-D00461:137:C9H2FACXX:3:1101:1755:2000 1:N:0:GGCTAC
@HWI-D00461:137:C9H2FACXX:3:1101:1260:1977 1:N:0:GGCTAC
@HWI-D00461:137:C9H2FACXX:3:1101:1917:1984 1:N:0:GGCTAC
我想要的输出是:
@HWI-D00461:137:C9H2FACXX:3:1101:1239:1968 1:N:0:GGCTAC
NTGTGTAATAGATTTTACTTTTGCCTTTAAGCCCAAGGTCCTGGACTTGAAACATCCAAGGGATGGAAAATGCCGTATAACAGGGTGGAAGAGAGATTTGA
@HWI-D00461:137:C9H2FACXX:3:1101:1117:1968 1:N:0:GGCTAC
NAAAGTCTACCAATTATACTTAGTGTGAAGAGGTGGGAGTTAAATATGACTTCCATTAATAGTTTCATTGTTTGGAAAACAGAGGTAATTTTTGATACAGA
@HWI-D00461:137:C9H2FACXX:3:1101:1087:1973 1:N:0:GGCTAC
NTAATCCAACTAACTAAAAATAAAAAGATTCAAATAGGTACAGAAAACAATGAAGGTGTAGAGGTGAGAAATCAACAGGATGTTCAGAAGCCTGTGTATGA
这是我到目前为止所拥有的。
ids = ''
with open(no_adapter_file, 'r') as file1:
with open(comparison_file, 'r') as file2:
common = set(file1).intersection(file2)
for line in common:
if line[0] == '@'
ids += line
with open(comparison_file, 'r') as file2:
ids_seq = ''
for line in file2:
if line == ids:
line += ids_seq
with open(new_file, 'w') as file_out:
for line in ids_seq:
file_out.write(line)
print(new_file + " was created.")
该代码成功提取了所有匹配的标题行,但是我不知道如何提取随后的序列行。
编辑:添加了一些输入和预期输出的示例。
答案 0 :(得分:1)
有几种方法可以解决您的问题。这是不假设文件排序的一种方法(对于避免潜在的高内存消耗可能非常有用)。它还没有关于具有重复ID的可能性的任何假设(如果不保证读取顺序相同,这也可以减少内存占用)。
# First, get the IDs we are interested in
ids = set()
with open(no_adapter_file, 'r') as file1:
line_number = 0
for line in file1:
line_number += 1
if line_number % 2 == 1:
ids.add(line.rstrip())
# Now, read the second file and print desired info if the ID is
# one of the interesting ones
with open(comparison_file, 'r') as file2, open(new_file, 'w') as file_out:
line_number = 0
for line in file2:
line_number += 1
if line_number % 4 == 1:
candidate_id = line.rstrip()
elif line_number % 4 == 2:
candidate_seq = line.rstrip()
if candidate_id in ids:
file_out.write("{}\n{}\n".format(candidate_id, candidate_seq))
print(new_file + " was created.")
在此代码中,我们首先读取第一个文件并将ID存储在一组中。然后,使用该集检查是否必须写入来自第二个文件的读取。