【发布时间】:2015-08-22 23:25:43
【问题描述】:
我正在尝试编写一个操作两个文件的 bash 脚本:
文件1
Region Coords. RsId Position Alleles Disease PValue OddsRatio RegionID
1p13.2 1:113839149..114551845 rs2476601 114377568 G>A Alopecia Areata 8.90E-08 1.34 869
2q13 2:111444884..111809030 rs3789129 111698040 A>C Alopecia Areata 1.50E-08 0.76 871
2q33.2 2:204611195..204817281 rs3096851 204763882 A>C Alopecia Areata 3.58E-08 1.32 802
2q33.2 2:204611195..204817281 rs1024161 204721752 G>A Alopecia Areata 3.55E-13 1.44 802
2q33.2 2:204611195..204817281 rs231775 204732714 A>G Alopecia Areata 2.20E-20 1.39 802
4q27 4:122982314..123605528 rs7682241 123523875 C>A Alopecia Areata 4.27E-08 1.34 803
4q27 4:122982314..123605528 rs7682481 123524026 G>C Alopecia Areata 4.80E-09 1.23 803
5q31.1 5:131783213..132135372 rs848 131996500 C>A Alopecia Areata 4.80E-09 1.27 872
文件2
CHR_A BP_A SNP_A CHR_B BP_B SNP_B R2
2 204721752 rs1024161 2 204732714 rs231775 0.849535
2 204721752 rs1024161 2 204763882 rs3096851 0.68029
2 204732714 rs231775 2 204763882 rs3096851 0.739633
4 123523875 rs7682241 4 123524026 rs7682481 1
我想读取 file1,如果 file2 的第 3 列 (SNP_A) 或第 4 列 (SNP_B) 中不存在第 3 列 (RsId) 值,则将该行写入输出。我尝试了以下方法:
#this executable file is called filter_file.sh
#!/bin/bash
file1=$1
file2=$2
outfile=$3
while read CHR_A BP_A SNP_A CHR_B BP_B SNP_B R2; do
cat $file2 | awk "(\$3!~/$SNP_A|$SNP_B/) {print}"
done < $file1 > $outfile
./filter_file.sh file1 file2 out
当我单独测试 awk 语句时它有效,但是当我将它添加到 bash while 循环时,它打印了所有 file1,包括标题,四次。 这一步的代码有什么问题?
一旦这工作正常,如果 file1 的第 3 列 (RsId) 值存在于 file2 的第 3 列 (SNP_A) 或第 4 列 (SNP_B) 中,我想将具有最低值的行写入 file1 第 7 列的输出(P值)。
我不确定如何开始任务的第二部分。通过阅读其他 awk 问题,我想我可以尝试使用 if 语句设置如下内容:
#!/bin/bash
file=$1
file2=$2
outfile=$3
while read CHR_A BP_A SNP_A CHR_B BP_B SNP_B R2; do
cat $file2 | awk "{
if ((\$3!~/$SNP_A|$SNP_B/))
print $0;
else
#Statement that prints only the row with the lowest value for column 7
}"
done < $file1 > $outfile
我可以使用哪些方法来执行此步骤?
如果人们可以指出一些可能对解决这类问题有帮助的教程,我们将不胜感激。
所需的输出文件将如下所示(顺序无关紧要):
Region Coords. RsId Position Alleles Disease PValue OddsRatio RegionID
1p13.2 1:113839149..114551845 rs2476601 114377568 G>A Alopecia Areata 8.90E-08 1.34 869
2q13 2:111444884..111809030 rs3789129 111698040 A>C Alopecia Areata 1.50E-08 0.76 871
2q33.2 2:204611195..204817281 rs231775 204732714 A>G Alopecia Areata 2.20E-20 1.39 802
4q27 4:122982314..123605528 rs7682481 123524026 G>C Alopecia Areata 4.80E-09 1.23 803
5q31.1 5:131783213..132135372 rs848 131996500 C>A Alopecia Areata 4.80E-09 1.27 872
【问题讨论】:
-
目前还不清楚最小值的计算应该如何工作 - 看起来第 4 行和第 5 行是出于这个原因而包含的行。你能解释一下为什么选择它们吗?
-
第 1、2 和 5 行被包括在内,因为它们不存在于文件 2 中,而第 3 和 4 行被包括在内,因为它们在文件 2 中具有最低的 p 值。@987654327 @ 与文件 2 中的
rs7682241配对。比较文件 1 中的PValue列rs7682481(4.80E-09) 和rs7682241(4.27E-08),rs7682481具有较低的PValue所以它被打印出来。rs231775与rs1024161和rs3096851配对,并且PValue比这些行中的任何一个都低。