35

I need to generate a random walk based on the DNA sequence of a virus, given its base pair sequence of 2k base pairs. The sequence looks like "ATGCGTCGTAACGT". The path should turn right for an A, left for a T, go upwards for a G and downwards for a C. How can I use either Matlab, Mathematica or SPSS for this purpose?

Gilles 'SO- stop being evil'
  • 92,660
  • 35
  • 189
  • 229
imbenzene
  • 364
  • 3
  • 13

6 Answers6

31

I did not previously know of Mark McClure's blog about Chaos Game representation of gene sequences, but it reminded me of an article by Jose Manuel Gutiérrez (The Mathematica Journal Vol 9 Issue 2), which also gives a chaos game algorithm for an IFS using (the four bases of) DNA sequences. A detailed description may be found here (the original article).

The method may be used to produce plots such as the following. Just for the hell of it, I've included (in the RHS panels) the plots generated with the corresponding complementary DNA strand (cDNA).

  • Mouse Mitochondrial DNA (LHS) and its complementary strand (cDNA) (RHS).

enter image description here

These plots were generated from GenBank Identifier gi|342520. The sequence contains 16295 bases.

(One of the examples used by Jose Manuel Gutiérrez. If anyone is interested, plots for the human equivalent may be generated from gi|1262342).

  • Human Beta Globin Region (LHS) and its cDNA (RHS)

enter image description here

Generated from gi|455025| (the example used my Mark McClure). The sequence contains 73308 bases

There are pretty interesting plots! The (sometimes) fractal nature of such plots is known, but the symmetry obvious in the LHS vs RHS (cDNA) versions was very surprising (at least to me).

The nice thing is that such plots for any DNA sequence may be very easily generated by directly importing the sequence (from, say, Genbank), and then using the power of Mma.
All you need it the accession number! ('Unknown' nucleotides such as "R" may need to be zapped) (I am using Mma v7).

The Original Implimenation (slightly modified) (by Jose Manuel Gutiérrez)

Important Update

On the advise of Mark McClure, I have changed Point/@Orbit[s, Union[s]] to Point@Orbit[s, Union[s]].

This speeds things up very considerably. See Mark's comment below.

Orbit[s_List, {a_, b_, c_, d_}] := 
  OrbitMap[s /. {a -> {0, 0}, b -> {0, 1}, c -> {1, 0}, 
     d -> {1, 1}}];
OrbitMap = 
  Compile[{{m, _Real, 2}}, FoldList[(#1 + #2)/2 &, {0, 0}, m]];
IFSPlot[s_List] := 
 Show[Graphics[{Hue[{2/3, 1, 1, .5}], AbsolutePointSize[2.5], 
    Point @ Orbit[s, Union[s]]}], AspectRatio -> Automatic, 
  PlotRange -> {{0, 1}, {0, 1}}, 
  GridLines -> {Range[0, 1, 1/2^3], Range[0, 1, 1/2^3]}]

This gives a blue plot. For green, change Hue[] to Hue[{1/3,1,1,.5}]

The following code now generates the first plot (for mouse mitochondrial DNA)

 IFSPlot[Flatten@
      Characters@
       Rest@Import[
         "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=\
    nucleotide&id=342520&rettype=fasta&retmode=text", "Data"]]

To get a cDNA plot I used the follow transformation rules (and also changed the Hue setting)

IFSPlot[    ....   "Data"] /. {"A" -> "T", "T" -> "A", "G" -> "C", 
   "C" -> "G"}]

Thanks to Sjoerd C. de Vries and telefunkenvf14 for help in directly importing sequences from the NCBI site.

Splitting things up a bit, for the sake of clarity.

Import a Sequence

mouseMitoFasta=Import["http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nucleotide&id=342520&rettype=fasta&retmode=text","Data"];

The method given for importing sequences in the original Mathematica J. article is dated.

A nice check

First@mouseMitoFasta

Output:

{>gi|342520|gb|J01420.1|MUSMTCG Mouse mitochondrion, complete genome}

Generation of the list of bases

mouseMitoBases=Flatten@Characters@Rest@mouseMitoFasta

Some more checks

{Length@mouseMitoBases, Union@mouseMitoBases,Tally@mouseMitoBases}

Output:

{16295,{A,C,G,T},{{G,2011},{T,4680},{A,5628},{C,3976}}}

The second set of plots was generated in a similar manner from gi|455025. Note that the sequence is long!

{73308,{A,C,G,T},{{G,14785},{A,22068},{T,22309},{C,14146}}}

One final example (containing 265922 bp), also showing fascinating 'fractal' symmetry. (These were generated with AbsolutePointSize[1] in IFSPlot).

The first line of the fasta file:

{>gi|328530803|gb|AFBL01000008.1| Actinomyces sp. oral taxon 170 str. F0386 A_spOraltaxon170F0386-1.0_Cont9.1, whole genome shotgun sequence}

enter image description here

The corresponding cDNA plot is again shown in blue on RHS

Finally, Mark's method also gives very beautiful plots (for example with gi|328530803), and may be downloaded as a notebook.

Community
  • 1
  • 1
tomd
  • 4,094
  • 2
  • 30
  • 37
  • 2
    Thanks for pointing out that article. It's particularly nice to learn how to auto-import from that site. I have two comments: First, the site that I refer to appears to be available to me. Are you sure you don't have a typo in the URL? You can avoid that potential problem by downloading the notebook version of the post from the link at the top of the page. Second, you can speed up Jose's code considerably by changing `Point/@Orbit[s, Union[s]]` to `Point@Orbit[s, Union[s]]`. This is a new and important syntax for Point that was introduced in V6, after that article appeared. – Mark McClure Apr 13 '11 at 16:57
  • @Mark. Firstly, your link _does_ work. Apologies for that. Secondly, your advise on `Point@` rather than `Point/@` works a treat and speeds things up very considerably. (The data in the link are from quite an old Genbank file. I could not find either a GI or accession.version number (see [here](http://www.ncbi.nlm.nih.gov/Sitemap/sequenceIDs.html). A more recent version may be obtain by changing 'fasta' to 'gp' in the import string given above. A few "n" entries (unknown?) have apparently been removed/updated, for example) – tomd Apr 15 '11 at 14:40
28

Not that I really understand the "graph" you want, but here is one literal interpretation.

None of the following code in necessarily in a final form. I want to know if this is right before I try to refine anything.

rls = {"A" -> {1, 0}, "T" -> {-1, 0}, "G" -> {0, 1}, "C" -> {0, -1}};
Prepend[Characters@"ATGCGTCGTAACGT" /. rls, {0, 0}];
Graphics[Arrow /@ Partition[Accumulate@%, 2, 1]]

enter image description here

Prepend[Characters@"TCGAGTCGTGCTCA" /. rls, {0, 0}];
Graphics[Arrow /@ Partition[Accumulate@%, 2, 1]]

enter image description here


3D Options

i = 0;
Prepend[Characters@"ATGCGTCGTAACGT" /. rls, {0, 0}];
Graphics[{Hue[i++/Length@%], Arrow@#} & /@ 
  Partition[Accumulate@%, 2, 1]]

enter image description here

i = 0;
Prepend[Characters@"ATGCGTCGTAACGT" /. 
    rls /. {x_, y_} :> {x, y, 0.3}, {0, 0, 0}];
Graphics3D[{Hue[i++/Length@%], Arrow@#} & /@ 
  Partition[Accumulate@%, 2, 1]]

enter image description here


Now that I know what you want, here is a packaged version of the first function:

genePlot[s_String] :=
 Module[{rls},
  rls =
   {"A" -> { 1, 0},
    "T" -> {-1, 0},
    "G" -> {0,  1},
    "C" -> {0, -1}};
  Graphics[Arrow /@ Partition[#, 2, 1]] & @
   Accumulate @ Prepend[Characters[s] /. rls, {0, 0}]
]

Use it like this:

genePlot["ATGCGTCGTAACGT"]
Community
  • 1
  • 1
Mr.Wizard
  • 23,689
  • 5
  • 41
  • 116
  • +1 ... Pretty strange random genetic walks ... Not your fault, for sure – Dr. belisarius Apr 10 '11 at 10:56
  • 1
    Hi Mr.Wizard! Thanx a lot for the response. I wanted exactly the first graphic that you have posted above. Can you please tell me the environment that that you suggest to code in 'R' or matlab? – imbenzene Apr 11 '11 at 16:46
  • 1
    @imbenzene, hello, and thank you for the accept. My code is for Mathematica, and that is what I have experience with. – Mr.Wizard Apr 11 '11 at 16:54
16

It sounds like you might be talking about CGR, or the so called Chaos Game Representation of a gene sequence described in the 1990 paper "Chaos game representation of gene structure" by Joel Jefferey. Here's an implementation in Mathematica:

cgrPic[s_String] := Module[
  {},
  chars = StringCases[s, "G"|"A"|"T"|"C"];
  f[x_, "A"] := x/2;
  f[x_, "T"] := x/2 + {1/2, 0};
  f[x_, "G"] := x/2 + {1/2, 1/2};
  f[x_, "C"] := x/2 + {0, 1/2};
  pts = FoldList[f, {0.5, 0.5}, chars];
  ListPlot[pts, AspectRatio -> Automatic]]

Here's how to apply it to a gene sequence taken from Mathematica's GenomeData command:

cgrPic[GenomeData["FAT4", "FullSequence"]]

enter image description here

Mark McClure
  • 4,017
  • 18
  • 32
  • There clearly is a fractal pattern in the figure shown in your blog. I wonder what that means... – Sjoerd C. de Vries Apr 10 '11 at 17:04
  • @Sjoerd, It means that there is a pattern in the amino acid sequence. Certain sub-sequences are more likely to appear than others. – Mark McClure Apr 10 '11 at 21:15
  • I tried pieces of the human genome and got little more than something that looked like a biased random walk. Your picture is quite different and especially the self-similarity is quite unexpected. – Sjoerd C. de Vries Apr 10 '11 at 22:00
6

Assuming that the sequence S has been mapped already*) to integer array then the actual computation of movements is straightforward based on rules R:

R =
   1  -1   0   0
   0   0   1  -1
S =
   1   2   3   4   3   2   4   3   2   1   1   4   3   2
T= cumsum(R(:, S), 2)
T =
   1   0   0   0   0  -1  -1  -1  -2  -1   0   0   0  -1
   0   0   1   0   1   1   0   1   1   1   1   0   1   1

*) You need to elaborate more on the actual sequence. Is it represented as single string, or perhaps as cell array, or something else?
Edit:
Assuming your sequence is represented as string, then you'll map it to integer sequence S like:

r= zeros(1, 84);
r(double("ATGC"))= [1 2 3 4];
S= r(double("ATGCGTCGTAACGT"))

And to plot it:

plot([0 T(1, :)], [0 T(2, :)], linespec)

where linespec is the desired line specification.

eat
  • 7,198
  • 1
  • 16
  • 27
  • @imbenzene: You are welcomed. BTW; I just realized that `Sjoerd C. de Vries` had edited your original post. So what does really count more for you, either 1) deterministic sequence (as indicated by your original post), or 2) random walk? If your application is more on random walks then `matlab` would also be able to provide `statistical sound` ways to do that. You may like to elaborate that on a separate question. Thanks – eat Apr 11 '11 at 17:32
6

You might also try something like this...

RandomDNAWalk[seq_, path_] := 
 RandomDNAWalk[StringDrop[seq, 1], 
  Join[path, getNextTurn[StringTake[seq, 1]]]];

RandomDNAWalk["", path_] := Accumulate[path];

getNextTurn["A"] := {{1, 0}};
getNextTurn["T"] := {{-1, 0}};
getNextTurn["G"] := {{0, 1}};
getNextTurn["C"] := {{0, -1}};

ListLinePlot[
 RandomDNAWalk[
  StringJoin[RandomChoice[{"A", "T", "C", "G"}, 2000]], {{0, 0}}]]
Andy Ross
  • 173
  • 7
0

This question seems to have been well answered already, but I thought I'd add that what you are describing has been previously published under the banner of DNA walks among a collection of numerical representation methods for DNA sequences, which are discussed in our preprint.

It turns out that DNA walks aren't very useful in practice, yet permit intuitive visualisation. I don't have it to hand, but I'd imagine my colleague would be more than happy to provide the Matlab code used to generate the below figure.

dna walk HIV

Bede Constantinides
  • 2,015
  • 2
  • 22
  • 25