This is a test version of Biostars. For the public version, visit https://www.biostars.org.
A quick idea how to connect two sets of multifasta files with a number of of NNNs in between sequences

Hi. I am looking for a quick idea how to connect two sets of multifasta files with a number of of NNNs in between. Would appreciate if anyone pointed me to existing script or other solution.

sort of:

file1
>1_set1
AAA

>2_set1
CCC
...

file2

> 1_set2
GGG

>2_set2
TTT

...

to get something like

>1_set1_set_2
AAANNNGGG

>2_set1_set2
CCCNNNTTT

Sorry for formatting, I am kinda new here... Thanks in advance, Xi

sequence assembly sequencing alignment

Thanks for the replies! I can use grep 'set' | sed 's/>\|_set*//g' | sort | comm -3 file1 file2 to make a sorted list of common sequences. Then use fastafetch -F to pull only relevant sequence pairs. I wrote little python script some time ago which gets rid of end of line characters in sequence part and writes two lines per record. Then should be fast and easy to test your solutions. Will let you know how it went soon.

Thanks for help!

2 answers

assuming both files have two lines (header+sequence) per record:

paste file1.fasta file2.fasta | \
awk -F '\t' '{if(NR%2==1) {printf(">%s_and_%s\n",substr($1,2),substr($2,2));} else  {printf("%sNNN%s\n",$1,$2);}}'

Slight modification for the awk part to match the requested headers in original post

awk -F '\t' '{if(NR%2==1) {printf(">%s%s\n",substr($1,2),substr($2,3));} else  {printf("%sNNN%s\n",$1,$2);}}'

Yes this worked a treat! Exactly what I was looking for. Thanks!

Yes this worked a treat! Exactly what I was looking for. Thanks!

How to delete replies/comments?

Also assuming that the sequences are in correct order..

paste File1 File2 | sed -e 's/\t/NNN/' -e 's/NNN>/_/' > union

Slight modification of the sed part to match the original header request.

sed -e 's/\t/NNN/' -e 's/NNN>/_/' -e 's/_[0-9]\+//'

This one produced the same output as awk solution. An it was noticeable faster than awk (my file has 53000 lines). Thanks!

Log in to answer this question.