This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Join multiple sequence lines in to one.

I have a file seq.txt which consists of multiple aligned sequences:

B_phora_cucurbitarum    -----------------------------------------------------MSHIKRD
E_aceosorus_bombacis    RAQAPPGSHNDQPPLLDPLSGILSPLGLGGLTPRSDSLPEHLEMQRRHILERLNERDEDV
A_nfragosa_RCEF_1005    ---------------------------------------------------MKYSILHLA
X_crodochium_bolleyi    -------------------------------------------MRLSNIAGQLAVGAACL
R_cillium_camemberti    --------------------------------------MRILTTGLLLWLLSLINLVSAF
[Bin                                                                               ]

B_phora_cucurbitarum    LSRISGGIGGFLSSIANNIYVFSWDFSLFLLNLVAFKRKVGKVTLEGNPGFGGKWPEYIP
E_aceosorus_bombacis    RAQAPPGSHNDQPPL--------QLAVGAACLEHLEM------------LERLNERDEDV
A_nfragosa_RCEF_1005    ---------MTENALSAEDLAKRG---LDKREVSYTGRITTTFDAAAQLVSNTGVHAFQA
X_crodochium_bolleyi    NDQPPLLDPLSGILSPLGLGGLTP------------------MRL-SNIAGQLAVGAACL
R_cillium_camemberti    -------------------------MS------DIPVHQHSDGRCPVTGISGSNPHPFCP
[Bin                                                                               ]

I want to combine them in a single line according to their names like this:

>B_phora_cucurbitarum   -----------------------------------------------------MSHIKRDLSRISGGIGGFLSSIANNIYVFSWDFSLFLLNLVAFKRKVGKVTLEGNPGFGGKWPEYIP
>E_aceosorus_bombacis   RAQAPPGSHNDQPPLLDPLSGILSPLGLGGLTPRSDSLPEHLEMQRRHILERLNERDEDVRAQAPPGSHNDQPPL--------QLAVGAACLEHLEM------------LERLNERDEDV
>A_nfragosa_RCEF_1005   ---------------------------------------------------MKYSILHLA---------MTENALSAEDLAKRG---LDKREVSYTGRITTTFDAAAQLVSNTGVHAFQA
>X_crodochium_bolleyi   -------------------------------------------MRLSNIAGQLAVGAACLNDQPPLLDPLSGILSPLGLGGLTP------------------MRL-SNIAGQLAVGAACL
>R_cillium_camemberti   --------------------------------------MRILTTGLLLWLLSLINLVSAF-------------------------MS------DIPVHQHSDGRCPVTGISGSNPHPFCP

Awk or sed commands are preferred, any help would be appreciated. Thanks!

alignment sequence awk sed

4 answers

As I'm a goat in awk, I let you a python solution

###Create a dictionnary containing your seq_merge.txt
merged_dict={}
###Open your seq table
with open("seq_merge.txt", 'r') as f:
    for line in f:
        ###Do a key/value dictionnary
        id_seq = line.rstrip().split("\t")[0]
        seq = line.rstrip().split("\t")[1]
        ###Check if the key exists in the dictionnary
        if id_seq not in merged_dict:
            merged_dict[id_seq] = seq
        else:
            merged_dict[id_seq] += seq
###Write in new file
with open("new_seq_merge.txt", "a") as new_seq_merge:
    for key, value in merged_dict.iteritems():
        new_seq_merge.write(">"+key+"\t"+value+"\n")

It is giving an error: Traceback (most recent call last): File "seq.py", line 8, in <module> seq = line.rstrip().split("\t")[1] IndexError: list index out of range

What is your delimiter in this line : B_phora_cucurbitarum -----------------------------------------------------MSHIKRD ? Tabulation ? 4 spaces ?

it is tabulation....

It's hard to investigate without your file.

Try to print line.split("\t") before id_seq = line.rstrip().split("\t")[0]

Does these lines really exist in your file ?

[Bin                                                                               ]

yes, they are. I found its the format problem, after converting to Unix format, it worked fine. Thanks a lot!

goat= greatest of all time...

probably, typing before lunch?

Nah, expression from my country to say that i'm very bad at awk writing

mkdir whatever
cp inputfile whatever
cd whatever
awk 'BEGIN{FS="\t";ORS=""}{print $2 >> $1}' inputfile
rm inputfile
for f in *; do awk -v N="$f" 'BEGIN{OFS="\t"}{print N,$0}' $f; done > newFile

I ignored the [Bin lines here. You can delete that file before the for loop

cat seq.txt| awk 'BEGIN{c=1}!/\[/{if(NF){n[c]=$1;s[c]=s[c] $2;c++}}/\[/{c=1}END{for( i in n){print n[i], s[i]}}'

Thanks but it's not working, it is just printing ---------------------------------------- in each line with some characters in between.

$ sed -n '/^$/d;/Bin/!p' test.txt| sed -e 's/\s\+/\t/g'  | sort -s -k 1,1 | datamash -g1 collapse 2 | sed 's/,//g'|  awk '{print ">"$1"\n"$2}'

>A_nfragosa_RCEF_1005
---------------------------------------------------MKYSILHLA---------MTENALSAEDLAKRG---LDKREVSYTGRITTTFDAAAQLVSNTGVHAFQA
>B_phora_cucurbitarum
-----------------------------------------------------MSHIKRDLSRISGGIGGFLSSIANNIYVFSWDFSLFLLNLVAFKRKVGKVTLEGNPGFGGKWPEYIP
>E_aceosorus_bombacis
RAQAPPGSHNDQPPLLDPLSGILSPLGLGGLTPRSDSLPEHLEMQRRHILERLNERDEDVRAQAPPGSHNDQPPL--------QLAVGAACLEHLEM------------LERLNERDEDV
>R_cillium_camemberti
--------------------------------------MRILTTGLLLWLLSLINLVSAF-------------------------MS------DIPVHQHSDGRCPVTGISGSNPHPFCP
>X_crodochium_bolleyi
-------------------------------------------MRLSNIAGQLAVGAACLNDQPPLLDPLSGILSPLGLGGLTP------------------MRL-SNIAGQLAVGAACL

Install datamash either from here or from distro repos (for debian based; sudo apt install datamash -y; for conda, conda install datamash -y).

Log in to answer this question.