My question follows that one: file(1) and magic(5) : describing other formats .

I want to describe a FASTA sequence ( http://en.wikipedia.org/wiki/FASTA_format)

It could be a DNA sequence (with only ATGC)


A 'magic' pattern would be


or a PROTEIN sequence ( ACDEFGHIKLMNPQRSTVWYBZX containing ATGC too)


A 'magic' pattern would be


But whenever I use those regular expressions, file tells me that it's a protein because it matches the 2nd regex. Is there a way to prioritize a result ? Is there a way to proritize , something like "Don't try any other pattern if that one matches ? ".

  • 2
    Why are you limiting your format in such a way? Both U (Sec) and O (Pyl) are correct and valid amino acid codes and you can also find * for STOP as well as the various IUPAC codes such as Y for pyrimidines etc in nucleotide sequences as well as simple - for gaps or X or N for masked or unknown residues. I'm pretty sure that most software will use some fairly complex heuristics to choose between DNA (and you seem to be ignoring RNA here) and protein. I very much doubt you can do it with a simple regex.
    – terdon
    Sep 6, 2014 at 14:25
  • Also, I've never played with this so I don't know if it will work, but you might be able to anchor the pattern. Something like >.*\n[ATCGXN-]*\n for DNA for example (ignoring the other IUPAC codes).
    – terdon
    Sep 6, 2014 at 14:31
  • I wouldn't bother ennumerating all the amino acids. Just use "a-zA-Z\*\-". The important thing is to exclude spaces and numbers, which might appear in e-mail messages which often look like this >From: fred.bloggs@example.edu Subject: Much ado about nothing
    – vk5tu
    Feb 8, 2015 at 6:24

1 Answer 1


You can set priorities using a "strength" value. From magic(5):

An optional strength can be supplied on a separate line which refers to the current magic description using the following format:

    !:strength OP VALUE

The operand OP can be: +, -, *, or / and VALUE is a constant between 0 and 255. This constant is applied using the specified operand to the currently computed default magic strength.

To lower the priority of the PROTEIN description, append this line:

!:strength - N

...where N is big enough to take it below the score of the DNA description.

The "currently computed default magic strength" of a test isn't immediately obvious, but you can use the --list flag to show them all. Alternatively, read the source -- the function responsible is apprentice_magic_strength. It's calculated from the first test of the entry, so if you want to give one type a precedence over another, having identical first lines is helpful. (That way, N only needs be 1.)

One other problem: Your regexps aren't strict enough. * can match zero characters, so the pattern is found at the start of every line - protein, DNA or other. To tighten it up, confirm that the whole line consists only of the permitted characters: \n[ATGC]+$, or \n[ATGC]{num,}$ (where num is the shortest pattern you expect to see)

0       string  =>header
>&0      regex   \n[ATGC]+$     DNA

0       string  =>header
!:strength - 1
  • So baseline strength starts at 20, and various operations raise or lower it roughly based on their size and type.
    – Pysis
    May 2, 2019 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.