<p>The <code>concat</code> command from <a href="https://bioinf.shenwei.me/seqkit/" rel="nofollow noreferrer">seqkit</a> could concatenate sequences with same ID from multiple files.</p>
<pre><code>seqkit concat [file1] [file2] ...
</code></pre>

CC BY-SA 4.0
2022-12-05T11:54:19.270Z
2022-12-05T11:54:19.270Z
Forrest Vigor
<p>I think it can be done using python dictionary. You can split the file using the header and then make the fasta id as your key and the sequence as your value. Then using simple loop and conditions you can concatenate them.</p>

CC BY-SA 4.0
2021-10-11T04:31:22.177Z
Okay. I will try
Saruul B
CC BY-SA 4.0
2021-11-05T17:47:03.340Z
Hi and welcome to the site! Please try and post complete answers. This isn't really answering, it is only giving a general direction in which an answer could be found.
terdon
CC BY-SA 4.0
2021-10-06T16:26:46.877Z
2021-10-06T16:26:46.877Z
Lazy PhD
<p>Assuming your files are small enough to allow you to fit one of them into memory, you can do this:</p>
<pre><code>$ awk '{ 
        if(/^&gt;/){ name=$0 }
        else{ 
            if(NR==FNR){seq[name]=$0 } 
            else{
                print name; 
                printf &quot;%s%s\n&quot;, seq[name],$0
            }
        }
      }' file1.fa file2.fa 
&gt;1721_1 gene name
TGA  
&gt;10_14 gene name
GTTGAT
</code></pre>
<p>If you cannot fit the files in memory, you can try this:</p>
<pre><code>$ join -o1.1,1.2,2.2 -t $'\t' &lt;(FastaToTbl file1.fa ) &lt;(FastaToTbl file2.fa) | sed 's/\t//2' | TblToFasta 
&gt;1721_1 gene name 
ATGTGA
&gt;10_14 gene name 
GTTGAT
</code></pre>
<p>Note that this approach requires the <code>FastaToTbl</code> and <code>TblToFasta</code> scripts I have posted here: <a href="https://bioinformatics.stackexchange.com/a/2651/298">https://bioinformatics.stackexchange.com/a/2651/298</a>.</p>
<p>Or, if brevity is more your thing, the same approach but more compact:</p>

CC BY-SA 4.0
2021-11-05T17:59:23.960Z
2021-11-05T17:59:23.960Z
terdon
<p>I have seven fasta files, one per gene, with more than 400 fasta entries per file, like this:
Input:</p>
<p>Gene1.fasta</p>
<pre><code>&gt;1721_1 gene name  
ATG   
&gt;10_14 gene name  
GTT
</code></pre>
<p>Gene2.fasta</p>
<pre><code>&gt;1721_1 gene name  
TGA  
&gt;10_14 gene name   
GAT
</code></pre>
<p>Output:</p>
<pre><code>&gt;1721_1  
ATGTGA  
&gt;10_14   
GTTGAT
</code></pre>
<p>What I want to do is create one fasta (continuously) file containing seven genes per sample. As you can see, the sample name is not the same before the _ in the fasta headers.</p>
<p>Is there any way to do this with awk or other command-line tools? Thank you very much.</p>

CC BY-SA 4.0
2021-10-04T03:46:52.940Z
`cat Gene*.fasta > all_genes.fasta` ?
Timur Shtatland
CC BY-SA 4.0
2021-10-04T11:25:52.773Z
@TimurShtatland Thank you. But it should contain all genes sequences of each sample. Each sample should be separate files.
Saruul B
CC BY-SA 4.0
2021-10-05T11:57:32.947Z
I would suggest using `samtools faidx` to pull the sample's gene out of each file, and then removing the header.  It will ignore anything after whitespace so gene name after space should not be problem. You could iterate across sample names in a bash loop. Of course, it requires that every sample have every gene.
Maximilian Press
CC BY-SA 4.0
2021-10-11T04:31:42.107Z
okay. I will try
Saruul B
<fasta><linux><awk>
Concatenate multiple fasta files into one file for MLST
# Concatenate multiple fasta files into one file for MLST

(Asked by: username_0 on 2021-10-04)

I have seven fasta files, one per gene, with more than 400 fasta entries per file, like this:
Input:
Gene1.fasta
>1721_1 gene name  
ATG   
>10_14 gene name  
GTT

Gene2.fasta
>1721_1 gene name  
TGA  
>10_14 gene name   
GAT

Output:
>1721_1  
ATGTGA  
>10_14   
GTTGAT

What I want to do is create one fasta (continuously) file containing seven genes per sample. As you can see, the sample name is not the same before the _ in the fasta headers.
Is there any way to do this with awk or other command-line tools? Thank you very much.

**Tags:** fasta, linux, awk
> **Comments:**
> **username_1**: `cat Gene*.fasta > all_genes.fasta` ?  
> **username_0**: @TimurShtatland Thank you. But it should contain all genes sequences of each sample. Each sample should be separate files.  
> **username_2**: I would suggest using `samtools faidx` to pull the sample's gene out of each file, and then removing the header.  It will ignore anything after whitespace so gene name after space should not be problem. You could iterate across sample names in a bash loop. Of course, it requires that every sample have every gene.  
> **username_0**: okay. I will try  

---

## 3 answers

### Answer by username_3 on 2022-12-05. Score: 2

The concat command from seqkit could concatenate sequences with same ID from multiple files.
seqkit concat [file1] [file2] ...


---

### Answer by username_4 on 2021-10-06. Score: 0

I think it can be done using python dictionary. You can split the file using the header and then make the fasta id as your key and the sequence as your value. Then using simple loop and conditions you can concatenate them.

> **Comments:**
> **username_0**: Okay. I will try  
> **username_5**: Hi and welcome to the site! Please try and post complete answers. This isn't really answering, it is only giving a general direction in which an answer could be found.  

---

### Answer by username_5 on 2021-11-05. Score: 0

Assuming your files are small enough to allow you to fit one of them into memory, you can do this:
$ awk '{ 
        if(/^>/){ name=$0 }
        else{ 
            if(NR==FNR){seq[name]=$0 } 
            else{
                print name; 
                printf "%s%s\n", seq[name],$0
            }
        }
      }' file1.fa file2.fa 
>1721_1 gene name
TGA  
>10_14 gene name
GTTGAT

If you cannot fit the files in memory, you can try this:
$ join -o1.1,1.2,2.2 -t $'\t' <(FastaToTbl file1.fa ) <(FastaToTbl file2.fa) | sed 's/\t//2' | TblToFasta 
>1721_1 gene name 
ATGTGA
>10_14 gene name 
GTTGAT

Note that this approach requires the FastaToTbl and TblToFasta scripts I have posted here: https://bioinformatics.stackexchange.com/a/2651/298.
Or, if brevity is more your thing, the same approach but more compact:


---

