# Creating plasmid diagrams in TeX

I need to create a diagram of plasmid in TeX, which looks like that:

The images above are slightly different but show pretty much the same thing so it would be great if I could create any of them with TeX. Of course it would be easy to just insert an image but I want to challenge myself if there's nothing like that available.

Is there any package I could use to create such diagram or should I create my own package?

-
This look feasible for TikZ but I’m afraid there isn’t a package or library that supports this. There is a big range on what this package should be able to implement. Have you thought about the interface/input syntax? Of course, I suggest a key-value input. Especially the second image includes a lot of information. –  Qrrbrbirlbel Jun 12 '13 at 20:00
I know what I want to insert but I haven't came up with the syntax yet. Key-value seems like a right choice but the ideal solution would be if I could choose a specific start and end point on the circle, it's color and label - would that be possible? Is TikZ easily extendable? –  REACHUS Jun 12 '13 at 20:26
If I had to do this, honestly I'd probably write a python script to convert whatever the data source is (googling suggests there's some standard formats as well as a package biopython for manipulating this kind of data) into tikz code (itself possibly involving various helper macros), rather than completely building an interface in latex. Here are a couple examples from texample.net that may provide helpful inspiration/techniques: 1, 2. –  kgr Jun 12 '13 at 20:43
Are there programs that create SVG diagrams for this? You could then convert SVG to PGF/TikZ with InkScape. –  Martin Schröder Jun 14 '13 at 8:22
@MartinSchröder I don't now but thanks for the tip. –  REACHUS Jun 14 '13 at 10:32

I agree with kgr generating the actual data should be done by a script. To have a certain readability (in case you need to manually tweak something afterwards, like e.g. highlight something with a special color) you should definitively go with a key-value interface as Qrrbrbirlbel suggested. I'd go with pgfkeys, which is already a part of TikZ. Here's a starting point with random data:

## Code

\documentclass[tikz,border=2mm]{standalone}

\tikzset{
plasmidopt/.is family,
plasmidopt,
height/.initial=0.3,
color/.initial=blue!50,
start angle/.initial=0,
stop angle/.initial=6,
scale/.initial=1,
}

\newcommand{\plasmidkey}[1] % access a specific key by name
{   \pgfkeysvalueof{/tikz/plasmidopt/#1}}

\newcommand{\plasmid}[1]{
\tikzset{plasmidopt,#1} % Process Keys passed to command
\fill[\plasmidkey{color}] (\plasmidkey{start angle}:\plasmidkey{radius}) arc (\plasmidkey{start angle}:\plasmidkey{stop angle}:\plasmidkey{radius}) -- (\plasmidkey{stop angle}:\plasmidkey{radius}+\plasmidkey{height}) arc (\plasmidkey{stop angle}:\plasmidkey{start angle}:\plasmidkey{radius}+\plasmidkey{height}) -- cycle;
}

\begin{document}

\begin{tikzpicture}
\foreach \x in {0,5,...,359.9} % random data
{   \pgfmathsetmacro{\mycolorvalue}{random(10,90)}
\pgfmathsetmacro{\myheight}{0.1+rand}
\pgfmathsetmacro{\myend}{\x+4*rnd+1}
}
\draw circle (3);
\end{tikzpicture}

\end{document}


## Output

Edit 1: What do you see when you're standing on the bottom of a manhole in New York at night, looking up?

## Code

\begin{tikzpicture}
\fill[inner color=black, outer color=black!70!blue] (0,0) circle (4.5);
\foreach \star in {1,...,256}
\fill[inner color=yellow,outer color=yellow!20!black] ({random(0,359)}:{3*rnd}) circle (\starradius);
}
\foreach \x in {0,5,...,359.9} % random data
{ \pgfmathsetmacro{\mycolorvalue}{random(10,90)}
\pgfmathsetmacro{\myheight}{-rnd}
\pgfmathsetmacro{\myend}{\x+4*rnd+1}
}
\fill(-4.5,-4.5) rectangle (4.5,4.5) (0,0) circle (3);
\end{tikzpicture}


## Output

-
That looks really impressive with such a small amount of code. I've loaded it up in writeLaTeX if anyone wants to try adding to the example: writelatex.com/227022gsflbv –  John Hammersley Jun 12 '13 at 23:02
Hehe, wow, that's just beautiful! –  Jake Jun 13 '13 at 20:53
Freckles!!! :) So nice! –  percusse Jun 14 '13 at 7:03
@percusse: If I get you right, the sky is just the inside of a giant egg? –  Tom Bombadil Jun 14 '13 at 7:46

This is just a little add-on to Tom Bombadil's very impressive answer that shows how to feed the macro data from a table (which can also be a CSV file) using PGFPlotstable.

You can provide your data like this (values taken from Addgene):

\pgfplotstableread{
Name                    Start   End Color   Height
T7_terminator       129     1   20      0.1
T7_Terminal_primer  69      87  20      0.1
lacI                    219     1858    20      0.1
T7_transl_en_RBS        322     306 90      0.2
lacO                    367     340 90      0.2
T7_promoter         385     367 90      0.2
tet                 421     684 90      0.1
pBRrevBam_primer        492     473 90      0.1
tet                     1917        2192    50      0.3
ROP                 2667        2858    30      0.3
pGEX_3_primer       2874        2852    40      0.3
pBR322_origin       3892        3273    10      0.5
KanR2               3998        4813    10      0.6
}\datatable


to produce

\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\tikzset{
plasmidopt/.is family,
plasmidopt,
height/.initial=0.3,
color/.initial=blue!50,
start angle/.initial=0,
stop angle/.initial=6,
scale/.initial=1,
}

\newcommand{\plasmidkey}[1] % access a specific key by name
{   \pgfkeysvalueof{/tikz/plasmidopt/#1}}

\newcommand{\plasmid}[1]{
\tikzset{plasmidopt,#1} % Process Keys passed to command
\fill[\plasmidkey{color}] (\plasmidkey{start angle}:\plasmidkey{radius}) arc (\plasmidkey{start angle}:\plasmidkey{stop angle}:\plasmidkey{radius}) -- (\plasmidkey{stop angle}:\plasmidkey{radius}+\plasmidkey{height}) arc (\plasmidkey{stop angle}:\plasmidkey{start angle}:\plasmidkey{radius}+\plasmidkey{height}) -- cycle;
}

\begin{document}

Name                    Start   End Color   Height
T7_terminator       129     1   20      0.1
T7_Terminal_primer  69      87  20      0.1
lacI                    219     1858    20      0.1
T7_transl_en_RBS        322     306 90      0.2
lacO                    367     340 90      0.2
T7_promoter         385     367 90      0.2
tet                 421     684 90      0.1
pBRrevBam_primer        492     473 90      0.1
tet                     1917        2192    50      0.3
ROP                 2667        2858    50      0.3
pGEX_3_primer       2874        2852    50      0.3
pBR322_origin       3892        3273    50      0.5
KanR2               3998        4813    50      0.6
}\datatable

\def\mymaxvalue{5372}

\begin{tikzpicture}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro\rows{\pgfplotsretval-1}

\foreach \row in {0,...,\rows}{
\pgfplotstablegetelem{\row}{Start}\of\datatable
\pgfmathsetmacro\mystart{-\pgfplotsretval/\mymaxvalue*360+90}
\pgfplotstablegetelem{\row}{End}\of\datatable
\pgfmathsetmacro\myend{-\pgfplotsretval/\mymaxvalue*360+90}
\pgfplotstablegetelem{\row}{Height}\of\datatable
\edef\myheight{\pgfplotsretval}
\pgfplotstablegetelem{\row}{Color}\of\datatable
\edef\mycolorvalue{\pgfplotsretval}