开始和结束

开始和结束

我有以下数据块(多个)

chr1.trna4 (17188416-17188486)  Length: 71 bp
Type: Gly   Anticodon: CCC at 33-35 (17188448-17188450) Score: 78.3
HMM Sc=56.60    Sec struct Sc=21.70
         *    |    *    |    *    |    *    |    *    |    *    |    *    |
Seq: GCATTGGTGGTTCAGTGGTAGAATTCTCGCCTCCCACGCGGGAGaCCCGGGTTCAATTCCCGGCCAATGCA
Str: >>>>>>>..>>>>.......<<<<.>>>>>.......<<<<<....>>>>>.......<<<<<<<<<<<<.

对于每个块,我需要在该块的最后一行找到以 开头的第 8 个模式Str。在上述情况下,第8个模式是.......(7个周期)。这是因为第一组>符号构成一个图案,第二组句点构成第二个图案,依此类推。

现在我需要从Seq模式行正上方的行中提取这 7 个字符。在示例中,这对应于子序列CTCCCAC

输出应该是Seq is CTCCCAC and Anticodon: CCC

bash这在任何 shell中都可能吗?

数据块的更多示例

chr19.trna11 (4724719-4724647)  Length: 73 bp
Type: Val   Anticodon: CAC at 34-36 (4724686-4724684)   Score: 79.2
HMM Sc=49.10    Sec struct Sc=30.10
         *    |    *    |    *    |    *    |    *    |    *    |    *    |  
Seq: GTTTCCGTAGTGTAGCGGTtATCACATTCGCCTCACACGCGAAAGGtCCCCGGTTCGATCCCGGGCGGAAACA
Str: >>>>>>>..>>>..........<<<.>>>>>.......<<<<<.....>>>>>.......<<<<<<<<<<<<.


chr19.trna12 (1383433-1383361)  Length: 73 bp
Type: Phe   Anticodon: GAA at 34-36 (1383400-1383398)   Score: 88.9
HMM Sc=68.40    Sec struct Sc=20.50
         *    |    *    |    *    |    *    |    *    |    *    |    *    |  
Seq: GCCGAAATAGCTCAGTTGGGAGAGCGTTAGACTGAAGATCTAAAGGtCCCTGGTTCGATCCCGGGTTTCGGCA
Str: >>>>>>>..>>>>........<<<<.>>>>>.......<<<<<.....>>>>>.......<<<<<<<<<<<<.


chr21.trna1 (18827177-18827107) Length: 71 bp
Type: Gly   Anticodon: GCC at 33-35 (18827145-18827143) Score: 80.9
HMM Sc=60.10    Sec struct Sc=20.80
         *    |    *    |    *    |    *    |    *    |    *    |    *    |
Seq: GCATGGGTGGTTCAGTGGTAGAATTCTCGCCTGCCACGCGGGAGGCCCGGGTTCGATTCCCGGCCCATGCA
Str: >>>>>>>..>>>>.......<<<<.>>>>>.......<<<<<....>>>>>.......<<<<<<<<<<<<.



chrX.trna4 (18693101-18693029)  Length: 73 bp
Type: Val   Anticodon: TAC at 34-36 (18693068-18693066) Score: 82.9
HMM Sc=54.70    Sec struct Sc=28.20
         *    |    *    |    *    |    *    |    *    |    *    |    *    |  
Seq: GGTTCCATAGTGTAGTGGTtATCACGTCTGCTTTACACGCAGAAGGtCCTGGGTTCGAGCCCCAGTGGAACCA
Str: >>>>>>>..>>>..........<<<.>>>>>.......<<<<<.....>>>>>.......<<<<<<<<<<<<.


chrX.trna6 (3833344-3833271)    Length: 74 bp
Type: Ile   Anticodon: GAT at 35-37 (3833310-3833308)   Score: 75.5
HMM Sc=50.20    Sec struct Sc=25.30
         *    |    *    |    *    |    *    |    *    |    *    |    *    |   
Seq: GGCCGGTTAGCTCAGTTGGTaAGAGCGTGGTGCTGATAACACCAAGGtCGCGGGCTCGACTCCCGCACCGGCCA
Str: >>>>>>>..>>>>.........<<<<.>>>>>.......<<<<<.....>>>>>.......<<<<<<<<<<<<.


chrX.trna8 (3794915-3794842)    Length: 74 bp
Type: Ile   Anticodon: GAT at 35-37 (3794881-3794879)   Score: 75.5
HMM Sc=50.20    Sec struct Sc=25.30
         *    |    *    |    *    |    *    |    *    |    *    |    *    |   
Seq: GGCCGGTTAGCTCAGTTGGTaAGAGCGTGGTGCTGATAACACCAAGGtCGCGGGCTCGACTCCCGCACCGGCCA
Str: >>>>>>>..>>>>.........<<<<.>>>>>.......<<<<<.....>>>>>.......<<<<<<<<<<<<.



chrX.trna10 (3756491-3756418)   Length: 74 bp
Type: Ile   Anticodon: GAT at 35-37 (3756457-3756455)   Score: 75.5
HMM Sc=50.20    Sec struct Sc=25.30
         *    |    *    |    *    |    *    |    *    |    *    |    *    |   
Seq: GGCCGGTTAGCTCAGTTGGTaAGAGCGTGGTGCTGATAACACCAAGGtCGCGGGCTCGACTCCCGCACCGGCCA
Str: >>>>>>>..>>>>.........<<<<.>>>>>.......<<<<<.....>>>>>.......<<<<<<<<<<<<.

chr19.trna8 (45981945-45981859) Length: 87 bp
Type: SeC   Anticodon: TCA at 36-38 (45981910-45981908) Score: 146.9
HMM Sc=0.00 Sec struct Sc=0.00
         *    |    *    |    *    |    *    |    *    |    *    |    *    |    *    |    * 
Seq: GCCCGGATGATCCTCAGTGGTCTGGGGTGCAGGCTTCAAACCTGTAGCTGTCTAGCGACAGAGTGGTTCAATTCCACCTTTCGGGCG
Str: >>>>>>>.>..>>>>>>....<<<<<<<<<<<<.......<<<<<<.>>>>>....<<<<<.>>>>.......<<<<<.<<<<<<<.

答案1

使用awk

$ awk -f script.awk file
Sequence: CTCACAC, Anticodon: CAC, Type: Val
Sequence: CTGAAGA, Anticodon: GAA, Type: Phe
Sequence: CTGCCAC, Anticodon: GCC, Type: Gly
Sequence: TTTACAC, Anticodon: TAC, Type: Val
Sequence: CTGATAA, Anticodon: GAT, Type: Ile
Sequence: CTGATAA, Anticodon: GAT, Type: Ile
Sequence: CTGATAA, Anticodon: GAT, Type: Ile
Sequence: CTTCAAA, Anticodon: TCA, Type: SeC

script.awk下面的程序在哪里awk

/^Type:/ {
        type = $2
        anticodon = $4
        split($6, pos, "-")
}

/^Seq:/ {
        seq = substr($2, pos[1]-2, length(anticodon) + 4)
        # or: seq = substr($2, pos[1]-2, pos[2]-pos[1]+5)
        printf "Sequence: %s, Anticodon: %s, Type: %s\n", seq, anticodon, type
}

第一个块由以字符串开头的任何行触发Type:,它从第二个和第四个空格分隔的字段中挑选出类型和反密码子序列,并将第 6 个这样的字段拆分-为序列中的开始和结束坐标。

第二个块由以字符串开头的行触发Seq:,它使用反密码子的起始位置和从最新行读取的反密码子长度从第二个空格分隔的字段中挑选出序列Type:,确保获得几个碱基- 两侧成对。

然后产生输出。


以下sed脚本使用该行中的第 8 个“模式”Str:来提取所需的序列,而不是该Type:行中给出的反密码子的数字位置。

/^Type:[[:blank:]]*/ {
        s/.*Type: \([^[:blank:]]*\)[[:blank:]]*Anticodon: \([^[:blank:]]*\).*/ Anticodon: \2, Type: \1/
        h
}

/^Seq:[[:blank:]]*/ {
        s//Sequence: /
        G
        y/\n/,/
        w data.tmp
}

/^Str:[[:blank:]]*/ {
        s///
        s,\(\(\([<>.]\)\3*\)\{7\}\)\(\([<>.]\)\5*\).*,s/: \1\\(\4\\)[^\,]*/: \\1/;n,
        y/<>/../
        w pass2.sed
}

d

(尾随d不是拼写错误)。

它分两次完成。

在第一遍中,创建了两个新文件,data.tmp并且pass2.sed.

$ sed -f script.sed file

(这里没有终端输出)

对于给定的数据,data.tmp看起来像

Sequence: GTTTCCGTAGTGTAGCGGTtATCACATTCGCCTCACACGCGAAAGGtCCCCGGTTCGATCCCGGGCGGAAACA, Anticodon: CAC, Type: Val
Sequence: GCCGAAATAGCTCAGTTGGGAGAGCGTTAGACTGAAGATCTAAAGGtCCCTGGTTCGATCCCGGGTTTCGGCA, Anticodon: GAA, Type: Phe
Sequence: GCATGGGTGGTTCAGTGGTAGAATTCTCGCCTGCCACGCGGGAGGCCCGGGTTCGATTCCCGGCCCATGCA, Anticodon: GCC, Type: Gly
Sequence: GGTTCCATAGTGTAGTGGTtATCACGTCTGCTTTACACGCAGAAGGtCCTGGGTTCGAGCCCCAGTGGAACCA, Anticodon: TAC, Type: Val
Sequence: GGCCGGTTAGCTCAGTTGGTaAGAGCGTGGTGCTGATAACACCAAGGtCGCGGGCTCGACTCCCGCACCGGCCA, Anticodon: GAT, Type: Ile
Sequence: GGCCGGTTAGCTCAGTTGGTaAGAGCGTGGTGCTGATAACACCAAGGtCGCGGGCTCGACTCCCGCACCGGCCA, Anticodon: GAT, Type: Ile
Sequence: GGCCGGTTAGCTCAGTTGGTaAGAGCGTGGTGCTGATAACACCAAGGtCGCGGGCTCGACTCCCGCACCGGCCA, Anticodon: GAT, Type: Ile
Sequence: GCCCGGATGATCCTCAGTGGTCTGGGGTGCAGGCTTCAAACCTGTAGCTGTCTAGCGACAGAGTGGTTCAATTCCACCTTTCGGGCG, Anticodon: TCA, Type: SeC

whilepass2.sed是一个sed对此进行后处理的脚本:

s/: ...............................\(.......\)[^,]*/: \1/;n
s/: ...............................\(.......\)[^,]*/: \1/;n
s/: ..............................\(.......\)[^,]*/: \1/;n
s/: ...............................\(.......\)[^,]*/: \1/;n
s/: ................................\(.......\)[^,]*/: \1/;n
s/: ................................\(.......\)[^,]*/: \1/;n
s/: ................................\(.......\)[^,]*/: \1/;n
s/: .................................\(.......\)[^,]*/: \1/;n

应用到pass2.sed会给data.sed你最终的结果:

$ sed -f pass2.sed data.tmp
Sequence: CTCACAC, Anticodon: CAC, Type: Val
Sequence: CTGAAGA, Anticodon: GAA, Type: Phe
Sequence: CTGCCAC, Anticodon: GCC, Type: Gly
Sequence: TTTACAC, Anticodon: TAC, Type: Val
Sequence: CTGATAA, Anticodon: GAT, Type: Ile
Sequence: CTGATAA, Anticodon: GAT, Type: Ile
Sequence: CTGATAA, Anticodon: GAT, Type: Ile
Sequence: CTTCAAA, Anticodon: TCA, Type: SeC

注意:我不确定第二步的执行情况非常大型数据集。

答案2

鉴于我们可以提取起始索引和反密码子:

len=7
prior=2

while IFS= read  -r line; do
    if [[ $line =~ Anticodon:" "([[:alpha:]]+)" at "([0-9]+) ]]; then
        anticodon=${BASH_REMATCH[1]}
        start=$(( BASH_REMATCH[2] - 1))  # string indexing is zero-based
    elif [[ $line == "Seq: "* ]]; then
        seq=${line#Seq: }
        printf "Seq: %s, Anticodon: %s\n" "${seq:start-prior:len}" "$anticodon"
    fi
done < file

一个更复杂的解决方案,每次解析“Str:”行,但不会将长度硬编码为 7(它会硬编码“nth”模式):

8thSeq() {
    local seq=$1 str=$2
    local last=${str:0:1}
    local nth=8 n=1 start

    for (( i=1; i < ${#str}; i++)); do
        if [[ "${str:i:1}" != "$last" ]]; then
            ((n++))
            if ((n == nth)); then
                start=$i
            elif ((n == nth+1)); then
                echo "${seq:start:i-start}"
                break
            fi
        fi
        last=${str:i:1}
    done
}

while IFS= read  -r line; do
    if [[ $line =~ Anticodon:" "([[:alpha:]]+) ]]; then
        anticodon=${BASH_REMATCH[1]}
    elif [[ $line == "Seq: "* ]]; then
        seq=${line#Seq: }
    elif [[ $line == "Str: "* ]]; then
        str=${line#Str: }
        printf "Seq: %s, Anticodon: %s\n" "$(8thSeq "$seq" "$str")" "$anticodon"
    fi
done < file

使用“更多”数据,两种解决方案都输出

Seq: CTCACAC, Anticodon: CAC
Seq: CTGAAGA, Anticodon: GAA
Seq: CTGCCAC, Anticodon: GCC
Seq: TTTACAC, Anticodon: TAC
Seq: CTGATAA, Anticodon: GAT
Seq: CTGATAA, Anticodon: GAT
Seq: CTGATAA, Anticodon: GAT
Seq: CTTCAAA, Anticodon: TCA

答案3

假设您需要解析 Str 字符串的重复:

开始和结束

由于每个块的模式序列可能会发生变化,因此我们需要一种方法来找到第 8 个模式。

可以提取每个重复的“模式”(从您的描述中任何以字符开头并以相同字符结束的内容) 使用 (GNU) grep 从 str 中获取:

$ str='>>>>>>>..>>>>.......<<<<.>>>>>.......<<<<<....>>>>>.......<<<<<<<<<<<<.'

$ grep -Eo '(.)\1+' <<<"$str"
>>>>>>>
..
>>>>
.......
<<<<
>>>>>
.......
<<<<<
....
>>>>>
.......
<<<<<<<<<<<<

因此,模式的开始和长度8(使用 shell)是:

pattern=8
splitstr=( $(grep -Eo '(.)\1+' <<<"$str") )
for((i=1;i<=pattern-2;i++)); do
    start=$((start+${#splistr[i]}))
done
len=${splitstr[pattern-1]}

对于任何模式(具有 8 次或更多重复)。

或者,更短地说,开始和结束:

start=$(echo "$str" | grep -Eo '^((.)\2+|.){7}'); start=${#start}
  end=$(echo "$str" | grep -Eo '^((.)\2+|.){8}');   end=${#end}

在 AWK 中:可以通过设置RS为 empty将文件分成块(由空行分隔的行) ""

领域

如果RS""每个块由 awk 自动进一步划分为字段。作为最后一个字段($NF用 awk 的说法),str 包含重复的字符。

所以,在 awk 中:

$ awk -vRS="" '{str=$NF; pat=8
cmd1="echo \"" str "\" | grep -Eo '\''^((.)\\2+|.){" pat-1 "}'\''";
cmd2="echo \"" str "\" | grep -Eo '\''^((.)\\2+|.){" pat   "}'\''";
cmd1 | getline start ; close(cmd1) ; start=length(start)
cmd2 | getline end   ; close(cmd2) ;   end=length(end)
print "Start:",start,"End:",end,"Sequence:",substr($(NF-2),start,end-start),"Anticodon:",$9,"Type:",$7
}' biopattern.txt


Start: 30 End: 37 Sequence: CCTCCCA Anticodon: CCC Type: Gly
Start: 31 End: 38 Sequence: CCTCACA Anticodon: CAC Type: Val
Start: 31 End: 38 Sequence: ACTGAAG Anticodon: GAA Type: Phe
Start: 30 End: 37 Sequence: CCTGCCA Anticodon: GCC Type: Gly
Start: 31 End: 38 Sequence: CTTTACA Anticodon: TAC Type: Val
Start: 32 End: 39 Sequence: GCTGATA Anticodon: GAT Type: Ile
Start: 32 End: 39 Sequence: GCTGATA Anticodon: GAT Type: Ile
Start: 32 End: 39 Sequence: GCTGATA Anticodon: GAT Type: Ile
Start: 33 End: 40 Sequence: GCTTCAA Anticodon: TCA Type: SeC

根据后面的数字,其他答案的结果不一样at

也许:这就是你的意思吗?

答案4

perl在段落模式下操作-00,将所有段落一一循环-n。首先,我们通过查看当前 para(又名 )中的属性来填充类型、反密码子、序列和 str 变量$_

$ perl -n00e '
   my($type, $anticodon, $seq, $str) = 
      / (?= .*\nType:      \h+ (\S+)  )
        (?= .*\hAnticodon: \h+ (\S+)  )
        (?= .*\nSeq:       \h+ (\S+)$ )
        (?= .*\nStr:       \h+ (\S+)$ )
      /xms;
   $str =~ /^((.)\2*){7}((.)\4*)/g;
   my($pos_codon, $len_codon) = (pos($str), length($3));
   my $codon = substr($seq, $pos_codon-$len_codon, $len_codon);
   print "Codon:[$codon] Anticodon:[$anticodon] Type:[$type]\n";
' file

结果:

Codon:[CTCACAC] Anticodon:[CAC] Type:[Val]
Codon:[CTGAAGA] Anticodon:[GAA] Type:[Phe]
Codon:[CTGCCAC] Anticodon:[GCC] Type:[Gly]
Codon:[TTTACAC] Anticodon:[TAC] Type:[Val]
Codon:[CTGATAA] Anticodon:[GAT] Type:[Ile]
Codon:[CTGATAA] Anticodon:[GAT] Type:[Ile]
Codon:[CTGATAA] Anticodon:[GAT] Type:[Ile]
Codon:[CTTCAAA] Anticodon:[TCA] Type:[SeC]

相关内容