首页 > 解决方案 > 在非标签行上重复上一个标签并增加计数

问题描述

我有以下包含基因名称和外显子位置的字符串。

gene1
250405..250551
251490..251884
195620..195641
154254..155469
156319..156548
gene2
171224..171481
403914..403970
147436..147627
149077..149157
11635..12021
gene3
107657..107844
105642..106214
103531..103691
407044..407435
405691..405843

我想编写一个脚本,列出所有基因名称和输出位置应该如下所示:

gene1_exon1 250405..250551
gene1_exon2 251490..251884
geneN_exonN X..Y 

任何想法我该怎么做?

我尝试了以下脚本

grep [a-z] temp3 > names
cat names |
while read line; do
  cat temp3 |
  while read position; do
    if [ "$line" != "$position" ] ; then
      echo "$line" ${position} >> names_and_positions
    else
      break
    fi
  done
done

标签: bashshellunixbioinformatics

解决方案


拉出基因名称并在每个外显子线之前重复。

awk '!/^[0-9]+\.\.[0-9]+$/ { gene=$1; i=1; next }
    { print gene "_exon" i++, $0 }' input >output

在看起来不像外显子行的行上,捕获第一个标记作为基因名称,并从 1 开始计算外显子行。在其他行(即外显子行)上打印带有捕获字符串和外显子行号前置的行,并增加数字。

这显然取决于正确使用正则表达式。如果您的外显子信息可能包含其周围的空格或其他变体,则您需要相应地调整正则表达式。


推荐阅读