From OrganicDesign Wiki
<?php
# [[Category:Private]][[Category:Private]]
# Extension:Fasta
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author: [http://www.organicdesign.co.nz/Sven User:Sven]
/*
* Local variables:
* tab-width: 2
* c-basic-offset: 2
* indent-tabs-mode: nil
* End:
*/
if (!defined('MEDIAWIKI')) die('Not a valid entry point!');
define('FASTA_VERSION','1.0.0, 2007-05-07');
$wgFastaMagic = "tfaFormat";
$wgFastaTag = "tfa";
$wgExtensionFunctions[] = 'wfSetupFasta';
$wgHooks['LanguageGetMagic'][] = 'wfFastaLanguageGetMagic';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'Fasta',
'author' => '[http://www.organicdesign.co.nz/Sven User:Sven]',
'description' => 'FASTA file rendering extension with regular expression search capability',
'url' => 'http://www.organicdesign.co.nz/Extension:Fasta',
'version' => FASTA_VERSION
);
class Fasta {
# Properties
private $dnaColour = array(
'G' => 'black',
'A' => 'green',
'C' => 'blue',
'T' => 'red'
);
private $aaColour = array(
'A' => "C8C8C8", # grey
'R' => "145AFF", # blue
'N' => "00DCDC", # Turquoise
'D' => "E60A0A", # red
'C' => "E6E600", # yellow
'Q' => "00DCDC", # turquoise
'E' => "E60A0A", # red
'G' => "EBEBEB", # light grey
'H' => "8282D2", # purple
'I' => "0F820F", # green
'L' => "0F820F", # green
'K' => "145AFF", # blue
'M' => "E6E600", # yellow
'F' => "3232AA", # purple
'P' => "DC9682", # pink
'S' => "DC9682", # orange
'T' => "DC9682", # orange
'Y' => "3232AA", # purple
'V' => "0F820F", # green
);
private $url = array('gi' => 'http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?val=');
# Constructor
function Fasta() {
global $wgHooks,$wgParser,$wgFastaMagic,$wgFastaTag,$wgJavaScriptFunctions;
# Add the parser-function
$wgParser->setFunctionHook($wgFastaMagic,array($this,'magicTfaFormat'));
# Add the tagHook
$wgParser->setHook($wgFastaTag,array($this,'tagTfa'));
}
function type($bases)
{
return preg_match("/^[\\n\\d\\sacgtn*-]+$/i", $bases) ? "dna"
: (preg_match("/^[\\n\\d\\sacgun*-]+$/i", $bases) ? "rna" : "protein" );
}
# DEBUGGING
# Logging to an article from within the MW environment
function logAdd($article,$msg) {
$ts = $GLOBALS['wgLang']->timeanddate(wfTimestampNow(),true);
$la = new Article(Title::newFromText($article));
$la->quickEdit($la->getContent()."\n*$ts: $msg");
}
# Logging to a file
function logFile($file, $msg) {
$fh = fopen($file,'a');
$ts = $GLOBALS['wgLang']->timeanddate(wfTimestampNow(),true);
if(is_array($msg)) $msg = print_r($msg, true);
fwrite($fh, "\n$ts: $msg");
}
# Expand the tfaFormat-magic
function magicTfaFormat(&$parser) {
# Populate $argv with both named and numeric parameters
$argv = array();
foreach (func_get_args() as $arg) if (!is_object($arg)) {
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
}
# Build text of expanded result
$args = '';
foreach ($argv as $k => $v) $args .= "*'''$k:''' ''$v''\n";
$text = "=== magicTfaFormat(): ===\n$args";
# Return result with available parser flags
return $text;
}
# Extract first row of a FASTA file
function removeHeader(&$text) {
$lines = explode("\n", $text, 2);
list($header, $text) = $lines;
return $header;
}
# Convert the <tfa> tags to HTML
function tagTfa($text,$argv,&$parser) {
$file = "/tmp/Fasta.log";
# If a \n is found before fasta header remove it
$text = preg_replace('/^\n/', '', $text);
# Remove header line '>gb|...| description\n'
$header = $this->removeHeader($text);
# Determine base type
$baseType = $this->type($text);
$this->logFile($file, "$baseType");
if( array_key_exists('regex', $argv) ){
# Replace regex match with ^ and $ as marker for beginning and end
$text = preg_replace("/($argv[regex])/s", "^$1$", $text);
}
$this->logFile($file, $text);
# Replace regex section with url
$header = preg_replace_callback('/(>gi\|)(\d+)(\|.+)/',array($this,'urlRegexpCallback'),$header);
# Reparse the header through $wgParser
$header = $parser->parse($header,$parser->mTitle,$parser->mOptions,false,false)->getText();
# Replace bases with font tags
if($baseType == "protein") {
$text = preg_replace_callback('/[ARNDCQEGHILKMFPSTYV]/i',array($this,'aaRegexpCallback'),$text);
} else {
$text = preg_replace_callback('/[GACT]/i',array($this,'dnaRegexpCallback'),$text);
}
if( array_key_exists('regex', $argv) ){
$file = "/tmp/Fasta.log";
# Replace ^ with span opening tag
$text = preg_replace('/\^/', '<span class="inline">', $text);
# Replace $ with span closure tag
$text = preg_replace('/\$/', '</span>', $text);
}
$args = '';
foreach ($argv as $k => $v) $args .= "<li><b>$k:</b> <i>$v</i></li>\n";
return "<h3>tagTfa():</h3>\n<ul>$args<li>Content:</b> $header<br />$text
</ul>\n";
}
# Called by the regexp in tagTfa()
function dnaRegexpCallback($match) {
$chr = strtoupper($match[0]);
return '<font color="' . $this->dnaColour[$chr] . '">' . $chr . '</font>';
}
# Called by the regexp in tagTfa()
function aaRegexpCallback($match) {
$chr = strtoupper($match[0]);
return '<font color="' . $this->aaColour[$chr] . '">' . $chr . '</font>';
}
# Called by the regexp in tagTfa()
function urlRegexpCallback($match) {
return $match[1] . "[" . $this->url['gi'] . $match[2] . " " . $match[2] . "]" . $match[3];
}
# Needed in some versions to prevent Special:Version from breaking
function __toString() { return 'Fasta'; }
}
# Called from $wgExtensionFunctions array when initialising extensions
function wfSetupFasta() {
global $wgFasta;
$wgFasta = new Fasta();
}
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
function wfFastaLanguageGetMagic(&$magicWords,$langCode = 0) {
global $wgFastaMagic;
$magicWords[$wgFastaMagic] = array(0,$wgFastaMagic);
return true;
}
?>