04 January 2009

An extension for MediaWiki: displaying a DNA sequence

This post is about a new extension for MediaWiki (the wiki engine of wikipedia written in PHP). This was the first extension I wrote: this extension add a new custom tag <dnaseq> and it simply displays a DNA sequence. Here is a screenshot of this extension installed in my local mediawiki.


and the source code for this extension is available here:


First we tell MediaWiki about this new extension in ${MWROOT}/LocalSettings.php
require_once("$IP/extensions/dnaseq/dnaseq.php");

Then we install this new feature which is a new TAG named <dnaseq>. Each time the mediawiki will find a <dnaseq> , the function myRenderDnaSequence will be called.
$wgHooks['ParserFirstCallInit'][] = 'myDnaSequence';
(...)
function myDnaSequence()
{
global $wgParser;
$wgParser->setHook( 'dnaseq', 'myRenderDnaSequence' );
return true;
}

myRenderDnaSequence is the function returning the formatted DNA sequence:
function myRenderDnaSequence( $input, $args, $parser )
{
if($input==null) return "";
$len= strlen($input);
$n=0;
$html="<div style='padding: 10px; font-size:10px; border-width: thin; border: 1px black solid; white-space: pre;background-color: white;font-family: courier, monospace;line-height:13px; font-size:12px;'>";
for($i=0;$i< $len;$i++)
{
$c = $input[$i];
if(ctype_space($c) || ctype_digit($c)) continue;
if($n % 60 == 0)
{
if($n!=0) $html.="<br/>";
$html.= sprintf("%06d ",($n+1));
}
else if($n % 10 ==0)
{
$html.=" ";
}
$n++;
switch(strtolower($c))
{
case "a":
$html.="<span style='color:green;'>".$c."</span>";
break;
case "c":
$html.="<span style='color:blue;'>".$c."</span>";
break;
case "g":
$html.="<span style='color:black;'>".$c."</span>";
break;
case "t":
case "u":
$html.="<span style='color:red'>".$c."</span>";
break;
default:
$html.="<span style='text-decoration:blink;color:gray'>".$c."</span>";
break;
}
if($n % 60 == 0)
{
$html.= sprintf(" %06d",($n));
}
}
$html .= "</div>";
return $html;
}


That's it.
Pierre

2 comments:

Jason Stajich said...

Cool Pierre - while I'm not a fan of putting too much of annotated sequence in a wiki, it is a nice plugin to have.

G said...

hello, I read an your old post about xmp on pdf at:
http://plindenbaum.blogspot.com/2007/05/is-there-any-xmp-in-scientific-pdf-no.html

so, can you tell me how did you extract xmp through the adobe's sdk?
(I'm using the java part)