-2

遺伝子 ID を含む newick 形式のファイルのコレクションがあります。

((gene1:1,gene2:1)100:1,gene3:1)100;
((gene4:1,gene5:1)100:1,gene6:1)100;

遺伝子 ID と種名の同等性のリストがあります。

speciesA=(gene1,gene4)
speciesB=(gene2,gene5)
speciesC=(gene3,gene6)

次の出力を取得したいと思います。

((speciesA:1,speciesB:1)100:1,speciesC:1)100;
((speciesA:1,speciesB:1)100:1,speciesC:1)100;

どうすれば進められるか考えていますか?理想的にはbashで素晴らしいでしょう:)

4

2 に答える 2

-1

入力.txt

((gene1:1,gene2:1)100:1,gene3:1)100;
((gene4:1,gene5:1)100:1,gene6:1)100;

equivs.txt

speciesA=(gene1,gene4)
speciesB=(gene2,gene5)
speciesC=(gene3,gene6)

convert.sh

#!/bin/bash


function replace() {
    output=$1
    for line in $(cat equivs.txt)  #this will fail if there is whitespace in your lines!
    do
        #gets the replacement string
        rep=$(echo $line | cut -d'=' -f1)

        #create a regex of all the possible matches we want to replace with $rep 
        targets=$(echo $line | cut -d'(' -f2- | cut -d')' -f1) 
        regex="($(echo $targets | sed -r 's/,/|/g'))"

        #do the replacements   
        output=$(echo $output | sed -r "s/${regex}/${rep}/g")
    done
    echo $output
}

#step through the input, file calling the above function on each line.
#assuming all lines are formatted like the example!
for line in $(cat input.txt)
do
    replace $line
done

出力:

((speciesA:1,speciesB:1)100:1,speciesC:1)100;
((speciesA:1,speciesB:1)100:1,speciesC:1)100;
于 2015-03-27T14:39:05.523 に答える