# How to concatenate two fasta sequences by py

I have three backbone and i want to concatenate 70 sequence into these backbone. such like: fasta file 1:

Backbone1: ACTGCGTATGTCAGCATATAGACCGCCT
Backbone2: CGTAGCTAGCTAGCTCGTGTCGTGATGCTACTC
Backbone3: GTCATGCTACGTGCTACGTGCTCGTCGCGC


fasta file 2:

sequence_1:CGCTAGTGTAGCTGCTACTCCG


output sequence:

Backbone1+sequence_1: ACTGCGTATGTCAGCATATAGACCGCCTCGCTAGTGTAGCTGCTACTCCG
Backbone2+sequence_1: CGTAGCTAGCTAGCTCGTGTCGTGATGCTACTCCGCTAGTGTAGCTGCTACTCCG
Backbone3+sequence_1: GTCATGCTACGTGCTACGTGCTCGTCGCGCCGCTAGTGTAGCTGCTACTCCGs

• Use Biopython's seqio
– M__
Nov 18 '20 at 11:27
• Can you give me some tips for writing codes? Nov 18 '20 at 12:19

First of all, those are not fasta sequences. Fasta is a specific format, and requires a > followed by a sequence identifier on one line and the one or more lines of sequence.

Assuming you really have two files in the format you show, you can do:

$$awk -F: '{ gsub(/ /,""); if(NR==FNR){bbone[2]; next} for(b in bbone){print b$$2}}' file1 file2
GTCATGCTACGTGCTACGTGCTCGTCGCGCCGCTAGTGTAGCTGCTACTCCG
ACTGCGTATGTCAGCATATAGACCGCCTCGCTAGTGTAGCTGCTACTCCG
CGTAGCTAGCTAGCTCGTGTCGTGATGCTACTCCGCTAGTGTAGCTGCTACTCCG


The gsub(/ /,""); just removes the extra space you have in file1 after each :. If you also want to have the sequence names as you show, use:

$$awk -F: '{ gsub(/ /,""); if(NR==FNR){bbone[2]="Backbone"NR; next} for(b in bbone){print bbone[b]"+"1":", b$$2}}' file1 file2
Backbone3+sequence_1: GTCATGCTACGTGCTACGTGCTCGTCGCGCCGCTAGTGTAGCTGCTACTCCG
Backbone1+sequence_1: ACTGCGTATGTCAGCATATAGACCGCCTCGCTAGTGTAGCTGCTACTCCG
Backbone2+sequence_1: CGTAGCTAGCTAGCTCGTGTCGTGATGCTACTCCGCTAGTGTAGCTGCTACTCCG


If you really need to do this in python for some reason, please edit your question and show us your python script so we have something to work on and expand. I really don't want to write a whole python program for something so trivial.