Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have created a macro which I used to format DNA sequences, as they should be in the monospaced font and uppercased:

\newcommand{\DNA}[1]{\texttt{\uppercase{#1}}}

Now I have a column with some different DNA sequences on different rows, lets say:

1 & \DNA{GCATCCAATGCC}\\
2 & \DNA{GGGACCAATGCG}\\
3 & \DNA{TTATCCAATCTC}\\

I would like to color each character (base) in the DNA sequence with a different color, as it easier for the reader to see certain patterns (in this case the reoccuring CCAAT sequence, a putative Hap complex binding domain). (for example as done here)

that means, all A -> Red T -> Green C -> Blue G -> Black

Is there a way to do this, or a verbatim package in which this can be achieved ? Thanks alot for your help!

----------

Liu's example posted works well, but not in my tabular/floatrow table Tobi's example works well in all environments tested..

Thanks again for your great help all!

share|improve this question
2  
omg guys... I just went to the groceries, and after returning I see three splendid answers to my question... I feel very humble now. –  Timtico Apr 25 '11 at 13:27
    
Is there also a way to put a line under a specified nucleotide? –  user5644 May 20 '11 at 10:58
    
@Ros: You could change a literate line like {B}{\textcolor{blue}{B}}{1} to {B}{\underline{B}}{1} in my above example. –  Tobi May 20 '11 at 13:44
    
Thanks, toby! But I don't want to underline all B's. E.g. if I want to emphasize a start-codon in, let's say, the sequence ACTACATGCA, I only want to underline ATG, and not the other nucleotides. –  user5644 May 20 '11 at 13:49
    
You might want to have a look at the texshade package (Typesetting nucleotide and peptide alignments) and maybe also textopo (for shaded membrane protein topology plots) by the same author. –  cgnieder Mar 22 '12 at 17:51
add comment

5 Answers

up vote 18 down vote accepted

Like this?

[EDIT: I changed the example so it doesn’t touch other listings.]

\documentclass{article}
\usepackage{listings,xcolor}
\lstdefinestyle{dna}{%
    literate={A}{\textcolor{green}{A}}{1}
        {B}{\textcolor{blue}{B}}{1}
        {C}{\textcolor{red}{C}}{1}
        {a}{\textcolor{green}{A}}{1}
        {b}{\textcolor{blue}{B}}{1}
        {c}{\textcolor{red}{C}}{1},
    basicstyle=\ttfamily,
}
\newcommand{\DNA}[1]{%
    \lstinline[style=dna]{#1}%
}
\begin{document}
\lstinline{ABCabc}\quad\DNA{bbCa}
\section{Somthing about \DNA{abcabcababc}}
\end{document}

You can use it also in commands like \section See the listingsmanual for more info.

share|improve this answer
    
+1 for listings. Thanks to the more robust implementation, it works better in macro arguments. –  Leo Liu Apr 25 '11 at 14:00
add comment

(Revised version following Martin's suggestion)

\documentclass{article}
\usepackage{xcolor}
\begingroup
\catcode`A\active
\catcode`T\active
\catcode`C\active
\catcode`G\active
\gdef A{\textcolor{red}{\string A}}
\gdef T{\textcolor{green}{\string T}}
\gdef C{\textcolor{blue}{\string C}}
\gdef G{\textcolor{black}{\string G}}
\endgroup

\DeclareRobustCommand\DNA[1]{\begingroup
  \scantokens{%
    \catcode`A\active
    \catcode`C\active
    \catcode`G\active
    \catcode`T\active
    \ttfamily#1\endinput}%
  \endgroup}

\begin{document}

\section{\DNA{ACGT}}

\DNA{ACGTACGT}, \fbox{\DNA{ACGTACGT}}

\end{document}

A more professional choice is to use texshade package, especially for biology. I almost forgot it. It is quite complex for me, and there're some predefined styles. You'd better read the manual first.

share|improve this answer
    
I just looked at some examples in the manual of texshade, and indeed it's a very nice package. However, in this case I have already preprocessed information. In which I already truncated part of the sequence I want to show with a perlscript. I'm combining that information with some other tabular info –  Timtico Apr 25 '11 at 13:40
    
Your example doesn't work in the xelatex / fontspec environment. I will need to take a look into that. –  Timtico Apr 25 '11 at 13:41
    
@Timtico: The code I listed works fine with fontspec on my computer. Maybe you can provide more information about it. (egreg's code is actually the same.) BTW, I knew texshade from LaTeX Graphics Companion, but I've never used it in practice. –  Leo Liu Apr 25 '11 at 13:53
    
Yep, I didn't want to make it too complex. You can easily add some code for lowercase. –  Leo Liu Apr 25 '11 at 14:08
1  
Instead of \symbol{`A} you could also use \string A. If you use \scantokens the \DNA macro could also be used in macro arguments. –  Martin Scharrer Apr 25 '11 at 14:33
show 2 more comments
\begingroup\lccode`~=`A
\lowercase{\endgroup\def~}{\textcolor{red}{A}}
\begingroup\lccode`~=`T
\lowercase{\endgroup\def~}{\textcolor{green}{T}}

\begingroup\lccode`~=`C
\lowercase{\endgroup\def~}{\textcolor{blue}{C}}

\begingroup\lccode`~=`G
\lowercase{\endgroup\def~}{\textcolor{black}{G}}

\let\NX\noexpand
\def\DNAmathcodes{%
  \catcode`A=\active \catcode`T=\active
  \catcode`C=\active \catcode`G=\active
  \catcode`a=\active \catcode`t=\active
  \catcode`c=\active \catcode`g=\active
}

\DeclareRobustCommand{\DNA}{\bgroup\DNAmathcodes\doDNA}
\newcommand\doDNA[1]{\texttt{\uppercase{\scantokens{#1\NX}}}\egroup}


\begin{tabular}{ll}
1 & \DNA{GCATCCAATGCC}\\
2 & \DNA{GGGACCAATGCG}\\
3 & \DNA{TTATCCAATCTC}
\end{tabular}

Note: without \noexpand, \scantokens would add a space; but \noexpand contains an "a"! Also \DNA must be robustified, if we need it in a section title.

share|improve this answer
    
You could use \scantokens to allow \DNA to work in macro arguments. If you use \lowercase to define the letter macros you don't need to use \char`\A, simply A etc. should do it. –  Martin Scharrer Apr 25 '11 at 14:35
    
The \char\A` remained from a try with \mathcode. Edited. Thanks. –  egreg Apr 25 '11 at 19:41
add comment

For completeness, here is a solution using LuaTeX. I use LPEG to parse the input and then apply the appropriate style to it. The solution is in ConTeXt, but it should be easy to translate it to LaTeX.

\unexpanded\def\DNA#1%
    {\ctxcommand{dnaparse("#1")}}

\startluacode
  thirddata = thirddata or {}
  thirddata.dna = thirddata.dna or {}
  local dna = thirddata.dna

  dna.attributes ={ 
      A = { color = "red",   style = "mono" },
      T = { color = "green", style = "mono" },
      C = { color = "blue",  style = "mono" },
      G = { color = "black", style = "mono" },
      default = { color = "black", style = "mono"},
    }

  local function format(char)
    local hash = dna.attributes[char] or dna.attributes[default]
    context.style(hash, char)
  end

  local char = lpeg.S("ATCG") / format
  dna.parser = lpeg.Cs(char^0)


  function commands.dnaparse(str)
    dna.parser:match(str)
  end
\stopluacode

\starttext

  \DNA{GCATCCAATGCC}
  \DNA{GGGACCAATGCG}
  \DNA{TTATCCAATCTC}

\stoptext

The advantage of using Lua is that you don't have to mess around with catcodes.

share|improve this answer
add comment

New solution, with a parser.

Using core TeX:

\documentclass{article}
\usepackage{color}
\def\dnaA{\textcolor{red}{A}}
\def\dnaT{\textcolor{green}{T}}
\def\dnaC{\textcolor{blue}{C}}
\def\dnaG{\textcolor{black}{G}}
\def\DNA#1{%
  \edef\testchar{#1}%
  \if;\testchar
    \relax
  \else
    \csname dna#1\endcsname
    \expandafter\DNA
  \fi}
\begin{document}
\DNA ACGTAGCTAACCG;
\end{document}

Using pgf's parser module:

\documentclass{article}
\usepackage{pgf}
\usepgfmodule{parser}
\pgfparserdef{dna}{initial}{\meaning A}{\textcolor{red}{A}}
\pgfparserdef{dna}{initial}{\meaning T}{\textcolor{green}{T}}
\pgfparserdef{dna}{initial}{\meaning C}{\textcolor{blue}{C}}
\pgfparserdef{dna}{initial}{\meaning G}{\textcolor{black}{G}}
\pgfparserdef{dna}{initial}{\meaning ;}{\pgfparserswitch{final}}
\newcommand\DNA{\pgfparserparse{dna}}
\begin{document}
\DNA ACGTAGCTAACCG;
\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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