所以我有一个基因名称和探测ID的.txt列表,originalFile.txt,如下所示:
GENE_ID PROBE_ID
10111 19873
10112 284, 19983
10113 187
Run Code Online (Sandbox Code Playgroud)
此文本文件中有大约30,000行.我想在第二列中创建一个没有逗号的新文本文件,例如:
GENE_ID PROBE_ID
10111 19873
10112 284
10112 19983
10113 187
Run Code Online (Sandbox Code Playgroud)
...而且,我希望所有的PROBE_ID来自另一个文本文件probes.txt,它看起来像:
19873
284
187
Run Code Online (Sandbox Code Playgroud)
...这样我就可以制作一个看起来如下的finalProduct.txt文件:
GENE_ID PROBE_ID
10111 19873
10112 284
10113 187
Run Code Online (Sandbox Code Playgroud)
如果我想手动输入每行probe.txt,我想我可以通过以下方式实现这个结果:
awk -F"/t" '{for(i=1;i<=NF;i++){if ($i ~ /probeID#/){print $i}}}' myGenes > test.txt
Run Code Online (Sandbox Code Playgroud)
但是,当然,这不会将逗号分隔的探测ID放在不同的行上,我必须手动输入数千个probeID中的每一个.
有没有人有任何提示或更好的建议?谢谢!
编辑清晰度
所以我认为我要问的是两个步骤.我想使用probe.txt获取originalFile.txt并最终生成finalProduct.txt.这有两个步骤:
对于probe.txt中列出的每个探测器,查看它是否存在于originalFile.txt中; 如果探针确实存在,则打印仅包含探针和相应GENE_ID的行.
或者您可以将它视为使用probes.txt在originalFile.txt上的过滤器之间的某种连接,其中输出文件将PROBE_ID列作为probes.txt中的探测器和来自originalFile.txt的相应GENE_ID.
或者您可以将其视为:1.制作一个中间文件,其中GENE_ID和PROBE_ID之间存在多对一的对应关系2.删除该中间文件的所有行,其中PROBE_ID与探针中的条目不对应.文本
编辑2
目前正试图重新利用这个 - 没有结果,但也许链接将有所帮助.
如果probes.txt
足够小,可以容纳在内存中,您可以尝试以下awk
脚本:
BEGIN {
OFS="\t";
# this is to handle the given input that has spaces after the comma
# and tabs between gene and probes
FS="[\t, ]+";
# load probes into an array
while ((getline probe < "probes.txt") > 0) {
probes[probe] = 1;
}
close ("probes.txt");
}
{
# for each probe, check if it's in the array
# and skip it if not
for (i=2; i <= NF; i++) {
if (probes[$i] == 1) {
print $1, $i;
}
}
}
Run Code Online (Sandbox Code Playgroud)