<p>Try <a href="https://github.com/samtools/samtools/blob/develop/misc/md5fa.c" rel="nofollow noreferrer">md5fa</a> from samtools. It computes two MD5s with and without ordering, respectively. Alternatively, there is <a href="https://bioinf.shenwei.me/seqkit/usage/#sum" rel="nofollow noreferrer">seqkit sum</a>, which is perhaps more powerful.</p>

CC BY-SA 4.0
2022-05-30T20:03:47.857Z
2022-05-30T20:03:47.857Z
user172818
<p>There are a lot of ambiguities in the question, but I'll provide one approach by means of a fasta/fastq sorting program (see <a href="https://gitlab.com/gringer/bioinfscripts/-/blob/master/fastx-sort.pl" rel="nofollow noreferrer">here</a>), using process substitution to avoid intermediary files. This will only work for small-ish fasta files, which can fit into computer memory:</p>
<pre><code>diff -q &lt;(fastx-sort.pl a.fasta) &lt;(fastx-sort.pl b.fasta)
</code></pre>
<p>Note that this will compare the entire file. If there are any sequences in one file but not the other, then the result will be that the files are different.</p>
<p>The <code>-q</code> option for diff will just output whether or not the two files differ; you didn't specify in your question whether you wanted any more detail. This option can be removed to show differences, or replaced with <code>-u</code> to add context. Context and/or line changes are not necessarily going to be helpful for multi-line fasta sequences, because the compared files may have a different order from the input, and the context lines may not include the name of the sequence. This could be fixed by changing the files to single-line fasta format (e.g. by piping through <code>fasta_formatter</code> from the <a href="http://hannonlab.cshl.edu/fastx_toolkit/" rel="nofollow noreferrer">FASTX-Toolkit</a>):</p>
<pre><code>diff -u &lt;(fastx-sort.pl a.fasta | fasta_formatter) &lt;(fastx-sort.pl b.fasta | fasta_formatter)
</code></pre>
<p>Note that this approach will output the entire sequence for any different sequences, together with adjacent context (including the sequence name). There are ways round that as well... but it would be far better if you could make your question more specific so that we don't need to explore the universe when answering.</p>
<p>If you want to ignore case, convert everything to upper (or lower) case:</p>
<pre><code>diff -q &lt;(fastx-sort.pl a.fasta | perl -pe 'tr/a-z/A-Z/') &lt;(fastx-sort.pl b.fasta | perl -pe 'tr/a-z/A-Z/')
</code></pre>

CC BY-SA 4.0
2022-05-30T08:56:27.817Z
Excellent thanks! I was thinking of using seqkit, will try in a bit.
Does this output the diffing lines?
Cornelius Roemer
CC BY-SA 4.0
2022-05-30T04:43:08.367Z
2022-05-30T12:46:30.683Z
2022-05-30T12:46:30.683Z
gringer
<p>Is there a good CLI tool that can diff two multiline fasta files, ignoring order, and comparing sequences with identical names with each other pairwise.</p>
<p>Bonus points if lower/upper case is tolerated (as in, it's considered identical) and if also indels can be tolerated, maybe through a flag?</p>
<p>I could script this in Python myself, but I was wondering whether there was a custom tool for this purpose already out there. Maybe something more performant than a Biopython script.</p>
<p>If there's no custom tool, I'd also be happy with a short script that uses a number of tools, e.g. <code>seqkit</code> <code>samtools</code> <code>diff</code> etc.</p>

CC BY-SA 4.0
2022-05-27T18:28:24.913Z
Why are indels tolerated but not SNVs?
Chris_Rands
<fasta><samtools><seqkit>
Checking that two multiline FASTAs are identical (allowing for different order and lower/upper case)
# Checking that two multiline FASTAs are identical (allowing for different order and lower/upper case)

(Asked by: username_0 on 2022-05-27)

Is there a good CLI tool that can diff two multiline fasta files, ignoring order, and comparing sequences with identical names with each other pairwise.

Bonus points if lower/upper case is tolerated (as in, it's considered identical) and if also indels can be tolerated, maybe through a flag?

I could script this in Python myself, but I was wondering whether there was a custom tool for this purpose already out there. Maybe something more performant than a Biopython script.

If there's no custom tool, I'd also be happy with a short script that uses a number of tools, e.g. `seqkit` `samtools` `diff` etc.

**Tags:** fasta, samtools, seqkit
> **Comments:**
> **username_1**: Why are indels tolerated but not SNVs?  

---

## 2 answers

### Answer by username_2 on 2022-05-30. Score: 3

Try [md5fa](https://github.com/samtools/samtools/blob/develop/misc/md5fa.c) from samtools. It computes two MD5s with and without ordering, respectively. Alternatively, there is [seqkit sum](https://bioinf.shenwei.me/seqkit/usage/#sum), which is perhaps more powerful.


---

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

There are a lot of ambiguities in the question, but I'll provide one approach by means of a fasta/fastq sorting program (see here), using process substitution to avoid intermediary files. This will only work for small-ish fasta files, which can fit into computer memory:
diff -q <(fastx-sort.pl a.fasta) <(fastx-sort.pl b.fasta)

Note that this will compare the entire file. If there are any sequences in one file but not the other, then the result will be that the files are different.
The -q option for diff will just output whether or not the two files differ; you didn't specify in your question whether you wanted any more detail. This option can be removed to show differences, or replaced with -u to add context. Context and/or line changes are not necessarily going to be helpful for multi-line fasta sequences, because the compared files may have a different order from the input, and the context lines may not include the name of the sequence. This could be fixed by changing the files to single-line fasta format (e.g. by piping through fasta_formatter from the FASTX-Toolkit):
diff -u <(fastx-sort.pl a.fasta | fasta_formatter) <(fastx-sort.pl b.fasta | fasta_formatter)

Note that this approach will output the entire sequence for any different sequences, together with adjacent context (including the sequence name). There are ways round that as well... but it would be far better if you could make your question more specific so that we don't need to explore the universe when answering.
If you want to ignore case, convert everything to upper (or lower) case:
diff -q <(fastx-sort.pl a.fasta | perl -pe 'tr/a-z/A-Z/') <(fastx-sort.pl b.fasta | perl -pe 'tr/a-z/A-Z/')

> **Comments:**
> **username_0**: Excellent thanks! I was thinking of using seqkit, will try in a bit. Does this output the diffing lines?  

---

