16 September 2010

Using MongoDB with Apache Tomcat, searching for Pubmed articles

The current post continues the previous one titled "
MongoDB and NCBI pubmed: Inserting, searching and updating. My notebook.
".

Here I've used the Java API for MongoDB/BSON in Apache Tomcat, the java servlet container to search and display some pubmed papers stored in mongo via a web interface. At the end the result looks like this:


The servlet

The servlet uses the JAVA API for mongo.

  • An instance of Mongo is created and it connects to the mongodb server
  • We obtain an object DB from the server for the database 'pubmed'
  • we get an object DBCollection from the database
  • If the user provided a valid pmid, the new query is created:
    BasicDBObject query=new BasicDBObject("_id",Long.parseLong(pmid))
    and it is used to search the collection
    BObject article=collection.findOne(query);
  • If the article was found, it was injected in the request and forwarded to the JSP named 'article.jsp'

File: src/WEB-INF/src/fr/inserm/umr915/biomongo/BioMongoServlet.java
package fr.inserm.umr915.biomongo;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.ServerAddress;

@SuppressWarnings("serial")
public class BioMongoServlet extends HttpServlet
{
/** mongodb host */
private String mongoHost=ServerAddress.defaultHost();
/** mongodb port */
private int mongoPort=ServerAddress.defaultPort();

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
String param=config.getInitParameter("mongo.port");
if(param!=null) this.mongoPort=Integer.parseInt(param);
param=config.getInitParameter("mongo.host");
if(param!=null) this.mongoHost=param;
}

@Override
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
Mongo mongo=null;
try {
//connect to mongo
mongo = new Mongo(this.mongoHost,this.mongoPort);
//get the database pubmed
DB database=mongo.getDB("pubmed");
if(database==null) throw new ServletException("canot get database");
//get the collection 'articles'
DBCollection col = database.getCollection("articles");
if(col==null) throw new ServletException("canot get collection");
//get the query parameter 'pmid'
String pmid=req.getParameter("pmid");
String jspPage="/WEB-INF/jsp/index.jsp";
//if pmid exist and looks like a number
if(pmid!=null && pmid.matches("[0-9]+"))
{
//find
DBObject article=col.findOne(new BasicDBObject("_id",Long.parseLong(pmid)));
if(article==null)
{
req.setAttribute("message", "Cannot find pmid:"+pmid);
}
else
{
jspPage="/WEB-INF/jsp/article.jsp";
req.setAttribute("article",article);
}
}
req.getRequestDispatcher(jspPage).forward(req, res);
}
catch(ServletException e)
{
throw e;
}
catch(IOException e)
{
throw e;
}
catch(Exception e)
{
throw new ServletException(e);
}
finally
{
//cleanup mongo
if(mongo!=null)
{
mongo.close();
}
mongo=null;
}
}
}

article.jsp

article.jsp displays the article. As this object is an instance of BasicDBObject (that is to say a Map and/or a List) it was very easy to use it with the JSP technology and the Java Standard Tag Library (JSTL).

File: ./src/WEB-INF/jsp/article.jsp
<%@page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="mongo" tagdir="/WEB-INF/tags" %>
<% out.clearBuffer(); %><html><head>
<title>PMID:<c:out value="${param['pmid']}" escapeXml="true"/></title>
</head>
<body>
<h1>PMID:<c:out value="${param['pmid']}" escapeXml="true"/></h1>
<h2><c:out value="${article.title}" escapeXml="true"/></h2>
<dl>
<dt>Date</dt><dd><mongo:dbobject object="${article.created}"/></dd>
<dt>Authors</dt><dd><ul><c:forEach var="author" items="${article.authors}">
<li>
<mongo:dbobject object="${author}"/>
</li>
</c:forEach></ul></dd>
<dt>Journal</dt><dd><mongo:dbobject object="${article.journal}"/></dd>
<dt>Mesh</dt><dd><ul><c:forEach var="term" items="${article.mesh}">
<li>
<c:out value="${term}" escapeXml="true"/>
</li>
</c:forEach></ul></dd>
</dl>


</body>
</html>

dbobject.tag

The previous JSP page calls a custom tag file <mongo:dbobject> which make a 'pretty display' of a BSON object by serializing it with com.mongodb.util.JSON.serialize(Object o)

File: ./src/WEB-INF/tags/dbobject.tag
<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@tag import="com.mongodb.util.JSON"%>
<%@tag import="com.mongodb.DBObject"%>
<%@attribute name="object" required="true" rtexprvalue="true" type="java.lang.Object"%>
<c:set var="bson"><%= JSON.serialize(this.object) %></c:set>

<div style="background-color:lightgray;"><c:out escapeXml="true" value="${bson}"/></div>


Other Files


index.jsp

a simple form for searching an article.

File: ./src/WEB-INF/jsp/index.jsp
<%@page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<% out.clearBuffer(); %><html><head>
<title>Search PMID</title>
</head>
<body>
<form method="GET" action="${pageContext.request.contextPath}/biomongo" >
<div style=" position:absolute;width:400px;height:100px;left:50%;top:50%;margin-left:-100px;margin-top:-50px;">
<c:if test="${not empty message}">
<div style="text-align:center; color:red;"><c:out value="${message}" escapeXml="true"/></div>
</c:if>
<input name="pmid"/><input type="submit"/>
</div>
</form></body>
</html>

web.xml

the deployement descriptor.

File: ./src/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<description>Biomongo Servlet</description>
<display-name>Biomongo</display-name>


<servlet>
<display-name>Biomongo Servlet</display-name>
<servlet-name>biomongo</servlet-name>
<servlet-class>fr.inserm.umr915.biomongo.BioMongoServlet</servlet-class>

<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>biomongo</servlet-name>
<url-pattern>/biomongo</url-pattern>
</servlet-mapping>


<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
</taglib>


<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>



<taglib>
<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/tld/sql.tld</taglib-location>
</taglib>



<taglib>
<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/tld/x.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>http://java.sun.com/jstl/functions</taglib-uri>
<taglib-location>/WEB-INF/tld/fn.tld</taglib-location>
</taglib>


</jsp-config>

</web-app>

An ANT file for building the project


File: build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project default="biomongo">
<property file="build.properties"/>
<property name="root.dir" value="."/>

<property file="${rootdir}/build.properties"/>


<path id="mongo.path">
<pathelement location="${mongodb.lib}"/>
</path>

<path id="servlet.path">
<pathelement location="${tomcat.servlet.api}"/>
<pathelement location="${tomcat.jsp.api}"/>
</path>


<target name="biomongo">
<property name="base.dir" value="${root.dir}/biomongo"/>
<mkdir dir="${base.dir}/src/WEB-INF/lib"/>
<mkdir dir="${base.dir}/src/WEB-INF/classes"/>

<copy todir="${base.dir}/src/WEB-INF/lib" includeEmptyDirs="false">
<fileset file="${mongodb.lib}"/>
<fileset dir="${taglib.dir}/lib" includes="*.jar"/>
</copy>

<copy todir="${base.dir}/src/WEB-INF/tld" includeEmptyDirs="false">
<fileset dir="${taglib.dir}/tld" includes="*.tld"/>
</copy>


<javac srcdir="${base.dir}/src/WEB-INF/src"
destdir="${base.dir}/src/WEB-INF/classes"
debug="true"
source="1.6"
target="1.6">
<classpath>
<path refid="mongo.path"/>
<path refid="servlet.path"/>
</classpath>
<sourcepath>
<pathelement location="${base.dir}/src/WEB-INF/src"/>
</sourcepath>
<include name="**/BioMongoServlet.java"/>
</javac>

<jar destfile="${tomcat.dir}/webapps/biomongo.war"
basedir="${base.dir}/src">
</jar>

</target>

</project>

Result





That's it.

Pierre

15 September 2010

MongoDB and NCBI pubmed: Inserting, searching and updating. My notebook.

After Neil and Jan, it is now my turn to play with MongoDB.
In this post, I've inserted some NCBI/PUBMED records into Mongo and tested various queries for selecting and updating the entries.

The initial set was a list of ~300 records where Charles Darwin was cited as a "[PersonalNameSubject]": http://www.ncbi.nlm.nih.gov/sites/entrez?db=pubmed&cmd=search&term=%22Darwin+C%22[ps]. This set was saved as XML and transformed to an input for Mongo using the following XSLT stylesheet:(code available here: http://openwetware.org/wiki/Image:Pubmed2mongo.xsl)

<?xml version="1.0" encoding="UTF-8"?>



<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0">

<!--

This stylesheet transforms one or more Pubmed
Article in xml format into JSON for mongodb

Author: Pierre Lindenbaum PhD plindenbaum@yahoo.fr

-->

<xsl:output method="text" encoding="UTF-8"/>


<xsl:template match="/">
<xsl:text>
db.articles.drop();
</xsl:text>
<xsl:apply-templates/>

<xsl:text>
db.articles.ensureIndex({pmid:1}, {unique: true});
db.articles.ensureIndex({created:1});
db.articles.ensureIndex({authors:1});
db.articles.ensureIndex({mesh:1});
db.articles.ensureIndex({journal:1});


</xsl:text>

</xsl:template>

<xsl:template match="PubmedArticleSet">
<xsl:apply-templates select="PubmedArticle"/>
</xsl:template>



<xsl:template match="PubmedArticle">
<xsl:text>
article={
</xsl:text>
<xsl:value-of select="concat('_id:',MedlineCitation/PMID,',')"/>
<xsl:value-of select="concat('pmid:',MedlineCitation/PMID)"/>
<xsl:apply-templates select="MedlineCitation/DateCreated"/>
<xsl:apply-templates select="MedlineCitation/Article/ArticleTitle"/>
<xsl:apply-templates select="MedlineCitation/Article/Journal/JournalIssue/Issue"/>
<xsl:apply-templates select="MedlineCitation/Article/Journal/JournalIssue/Volume"/>
<xsl:apply-templates select="MedlineCitation/Article/Pagination/MedlinePgn"/>
<xsl:apply-templates select="MedlineCitation/Article/Journal"/>
<xsl:apply-templates select="PubmedData/ArticleIdList/ArticleId[@IdType='doi']"/>
<xsl:apply-templates select="PubmedData/ArticleIdList/ArticleId[@IdType='pmc']"/>
<xsl:apply-templates select="MedlineCitation/Article/Language"/>
<xsl:apply-templates select="MedlineCitation/Article/AuthorList"/>
<xsl:apply-templates select="MedlineCitation/MeshHeadingList"/>
<xsl:text>};
article=db.articles.save(article);

</xsl:text>

<xsl:text>

</xsl:text>
</xsl:template>




<xsl:template match="ArticleId[@IdType='doi']">
<xsl:text>,doi:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>

<xsl:template match="ArticleId[@IdType='pmc']">
<xsl:text>,pmc:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>

<xsl:template match="AuthorList">
<xsl:text>,authors:[</xsl:text>
<xsl:for-each select="Author">
<xsl:if test="position()!=1">,</xsl:if>
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:template>




<xsl:template match="MeshHeadingList">
<xsl:text>,mesh:[</xsl:text>
<xsl:for-each select="MeshHeading/DescriptorName">
<xsl:if test="position()!=1">,</xsl:if>
<xsl:apply-templates select="." mode="text"/>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:template>


<xsl:template match="Author">
<xsl:text>{firstName:"</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="s" select="ForeName"/>
</xsl:call-template>
<xsl:text>",lastName:"</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="s" select="LastName"/>
</xsl:call-template>
<xsl:text>"}</xsl:text>
</xsl:template>




<xsl:template match="DateCreated">
<xsl:text>,created:{</xsl:text>
<xsl:value-of select="concat('year:',number(Year),',')"/>
<xsl:value-of select="concat('month:',number(Month),',')"/>
<xsl:value-of select="concat('day:',number(Day))"/>
<xsl:text>}</xsl:text>
</xsl:template>


<xsl:template match="ArticleTitle">
<xsl:text>,title:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>


<xsl:template match="Issue">
<xsl:text>,issue:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>

<xsl:template match="Volume">
<xsl:text>,volume:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>

<xsl:template match="MedlinePgn">
<xsl:text>,pgn:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>



<xsl:template match="Journal">
<xsl:text>,journal:{title:</xsl:text>
<xsl:apply-templates select="Title" mode="text"/>
<xsl:apply-templates select="ISOAbbreviation"/>
<xsl:apply-templates select="ISSN[@IssnType='Print']"/>
<xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="ISOAbbreviation">
<xsl:text>,abbr:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>

<xsl:template match="ISSN">
<xsl:text>,issn:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>


<xsl:template match="Language">
<xsl:text>,lang:</xsl:text>
<xsl:apply-templates select="." mode="text"/>
</xsl:template>

<xsl:template match="*" mode="text">
<xsl:text>"</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="s" select="."/>
</xsl:call-template>
<xsl:text>"</xsl:text>
</xsl:template>

<xsl:template name="escape">
<xsl:param name="s"/>
<xsl:choose>
<xsl:when test="contains($s,'&quot;')">
<xsl:value-of select="substring-before($s,'&quot;')"/>
<xsl:text>\&quot;</xsl:text>
<xsl:call-template name="escape">
<xsl:with-param name="s" select="substring-after($s,'&quot;')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>




</xsl:stylesheet>


Transform pubmed/xml to mongo:
xsltproc pubmed2mongo.xsl ~/pubmed_result.txt > input.js

> more input.js

db.articles.drop();

article={_id:20665232,pmid:20665232,created:{year:2010,month:8,day:10},title:"Charles Darwin's beagle voyage, fossil vertebrate succession, and \"the gradual birth amp; death of species\".",issue:"2",volume:"43",pgn:"363-99",journal:{title:"Journal of the history of biology",abbr:"J Hist Biol",issn:"0022-5010"},doi:"10.1007/s10739-009-9189-9",lang:"eng",authors:[{firstName:"Paul D",lastName:"Brinkman"}],mesh:["Animals","Fossils","History, 19th Century","Natural History","Phylogeny","Vertebrates"]};
article=db.articles.save(article);




article={_id:20626121,pmid:20626121,created:{year:2010,month:7,day:14},title:"[The biomedical legacy of Charles Darwin]",issue:"2",volume:"146",pgn:"87-9",journal:{title:"Gaceta médica de México",abbr:"Gac Med Mex",issn:"0016-3813"},lang:"spa",authors:[{firstName:"Emilio",lastName:"García-Procel"}],mesh:["Biology","Evolution","History, 19th Century","History, 20th Century","Medicine"]};
article=db.articles.save(article);




article={_id:20503821,pmid:20503821,created:{year:2010,month:5,day:27},title:"Darwin and the popularization of evolution.",issue:"1",volume:"64",pgn:"5-24",journal:{title:"Notes and records of the Royal Society of London",abbr:"Notes Rec R Soc Lond",issn:"0035-9149"},lang:"eng",authors:[{firstName:"Bernard",lastName:"Lightman"}],mesh:["Biology","Evolution","Genetic Fitness","History, 19th Century","History, 20th Century","Humans","Male","Philosophy","Religion","Science","Selection, Genetic","United States"]};
article=db.articles.save(article);

and load this file into mongo in the database 'pubmed'.
mongo pubmed input.js


and add a few indexes for this database:
>db.articles.ensureIndex({pmid:1}, {unique: true});
db.articles.ensureIndex({created:1});
db.articles.ensureIndex({authors:1});
db.articles.ensureIndex({mesh:1});
db.articles.ensureIndex({journal:1});


print 3 records

> db.articles.find().limit(3).forEach(printjson);
{
"_id" : 20665232,
"pmid" : 20665232,
"created" : {
"year" : 2010,
"month" : 8,
"day" : 10
},
"title" : "Charles Darwin's beagle voyage, fossil vertebrate succession, and \"the gradual birth & death of species\".",
"issue" : "2",
"volume" : "43",
"pgn" : "363-99",
"journal" : {
"title" : "Journal of the history of biology",
"abbr" : "J Hist Biol",
"issn" : "0022-5010"
},
"doi" : "10.1007/s10739-009-9189-9",
"lang" : "eng",
"authors" : [
{
"firstName" : "Paul D",
"lastName" : "Brinkman"
}
],
"mesh" : [
"Animals",
"Fossils",
"History, 19th Century",
"Natural History",
"Phylogeny",
"Vertebrates"
]
}
{
"_id" : 20626121,
"pmid" : 20626121,
"created" : {
"year" : 2010,
"month" : 7,
"day" : 14
},
"title" : "[The biomedical legacy of Charles Darwin]",
"issue" : "2",
"volume" : "146",
"pgn" : "87-9",
"journal" : {
"title" : "Gaceta médica de México",
"abbr" : "Gac Med Mex",
"issn" : "0016-3813"
},
"lang" : "spa",
"authors" : [
{
"firstName" : "Emilio",
"lastName" : "García-Procel"
}
],
"mesh" : [
"Biology",
"Evolution",
"History, 19th Century",
"History, 20th Century",
"Medicine"
]
}
{
"_id" : 20503821,
"pmid" : 20503821,
"created" : {
"year" : 2010,
"month" : 5,
"day" : 27
},
"title" : "Darwin and the popularization of evolution.",
"issue" : "1",
"volume" : "64",
"pgn" : "5-24",
"journal" : {
"title" : "Notes and records of the Royal Society of London",
"abbr" : "Notes Rec R Soc Lond",
"issn" : "0035-9149"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "Bernard",
"lastName" : "Lightman"
}
],
"mesh" : [
"Biology",
"Evolution",
"Genetic Fitness",
"History, 19th Century",
"History, 20th Century",
"Humans",
"Male",
"Philosophy",
"Religion",
"Science",
"Selection, Genetic",
"United States"
]
}

skip 2 and print 1 record

> db.articles.find().skip(2).limit(1).forEach(printjson);
{
"_id" : 20503821,
"pmid" : 20503821,
"created" : {
"year" : 2010,
"month" : 5,
"day" : 27
},
"title" : "Darwin and the popularization of evolution.",
"issue" : "1",
"volume" : "64",
"pgn" : "5-24",
"journal" : {
"title" : "Notes and records of the Royal Society of London",
"abbr" : "Notes Rec R Soc Lond",
"issn" : "0035-9149"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "Bernard",
"lastName" : "Lightman"
}
],
"mesh" : [
"Biology",
"Evolution",
"Genetic Fitness",
"History, 19th Century",
"History, 20th Century",
"Humans",
"Male",
"Philosophy",
"Religion",
"Science",
"Selection, Genetic",
"United States"
]
}

How many records in the database, regardless of the limit ?

> db.articles.find().limit(20).count();
327

How many records will be printed ?

> db.articles.find().limit(20).size();
20

Find one record with PMID=-1

> db.articles.findOne({pmid:-1});
null

Find PMID 20180452

> db.articles.findOne({pmid:20180452});
{
"_id" : 20180452,
"pmid" : 20180452,
"created" : {
"year" : 2010,
"month" : 2,
"day" : 25
},
"title" : "[Darwin's hidden feeling for emotions of the species]",
"issue" : "50-51",
"volume" : "106",
"pgn" : "3443-6",
"journal" : {
"title" : "Läkartidningen",
"abbr" : "Lakartidningen",
"issn" : "0023-7205"
},
"lang" : "swe",
"authors" : [
{
"firstName" : "Gösta",
"lastName" : "Alfvén"
}
],
"mesh" : [
"Animals",
"Emotions",
"England",
"Evolution",
"Famous Persons",
"History, 19th Century",
"Humans",
"Species Specificity"
]
}

Searching in Arrays: find records having a mesh equals to 'Lactose Intolerance'

> db.articles.find({mesh:'Lactose Intolerance'}).forEach(printjson);
{
"_id" : 17575947,
"pmid" : 17575947,
"created" : {
"year" : 2007,
"month" : 6,
"day" : 19
},
"title" : "Darwin's illness: a final diagnosis.",
"issue" : "1",
"volume" : "61",
"pgn" : "23-9",
"journal" : {
"title" : "Notes and records of the Royal Society of London",
"abbr" : "Notes Rec R Soc Lond",
"issn" : "0035-9149"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "Fernando",
"lastName" : "Orrego"
},
{
"firstName" : "Carlos",
"lastName" : "Quintana"
}
],
"mesh" : [
"Biology",
"Crohn Disease",
"England",
"History, 19th Century",
"Humans",
"Lactose Intolerance",
"Male"
]
}
{
"_id" : 15811889,
"pmid" : 15811889,
"created" : {
"year" : 2005,
"month" : 4,
"day" : 6
},
"title" : "Darwin's illness revealed.",
"issue" : "954",
"volume" : "81",
"pgn" : "248-51",
"journal" : {
"title" : "Postgraduate medical journal",
"abbr" : "Postgrad Med J",
"issn" : "0032-5473"
},
"doi" : "10.1136/pgmj.2004.025569",
"pmc" : "PMC1743237",
"lang" : "eng",
"authors" : [
{
"firstName" : "Anthony K",
"lastName" : "Campbell"
},
{
"firstName" : "Stephanie B",
"lastName" : "Matthews"
}
],
"mesh" : [
"Famous Persons",
"Great Britain",
"History, 19th Century",
"Lactose Intolerance"
]
}

Explain, find with an index (PMID)

> db.articles.find({pmid:20180452}).explain();
{
"cursor" : "BtreeCursor pmid_1",
"nscanned" : 1,
"nscannedObjects" : 1,
"n" : 1,
"millis" : 0,
"indexBounds" : {
"pmid" : [
[
20180452,
20180452
]
]
}
}

Explain, find without an index (volume)

> db.articles.find({volume:'81'}).explain();
{
"cursor" : "BasicCursor",
"nscanned" : 327,
"nscannedObjects" : 327,
"n" : 2,
"millis" : 0,
"indexBounds" : {

}
}

'AND operator' search for records having mesh and 'Evolution' AND 'Religion' AND 'History, 19th Century'

>db.articles.find({mesh:{$all:["Evolution","History, 19th Century","Religion"]}}).limit(2).forEach(printjson);

{
"_id" : 20503821,
"pmid" : 20503821,
"created" : {
"year" : 2010,
"month" : 5,
"day" : 27
},
"title" : "Darwin and the popularization of evolution.",
"issue" : "1",
"volume" : "64",
"pgn" : "5-24",
"journal" : {
"title" : "Notes and records of the Royal Society of London",
"abbr" : "Notes Rec R Soc Lond",
"issn" : "0035-9149"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "Bernard",
"lastName" : "Lightman"
}
],
"mesh" : [
"Biology",
"Evolution",
"Genetic Fitness",
"History, 19th Century",
"History, 20th Century",
"Humans",
"Male",
"Philosophy",
"Religion",
"Science",
"Selection, Genetic",
"United States"
]
}
{
"_id" : 20145235,
"pmid" : 20145235,
"created" : {
"year" : 2010,
"month" : 2,
"day" : 10
},
"title" : "Darwin's compassionate view of human nature.",
"issue" : "6",
"volume" : "303",
"pgn" : "557-8",
"journal" : {
"title" : "JAMA : the journal of the American Medical Association",
"abbr" : "JAMA"
},
"doi" : "10.1001/jama.2010.101",
"lang" : "eng",
"authors" : [
{
"firstName" : "Paul",
"lastName" : "Ekman"
}
],
"mesh" : [
"Altruism",
"Animals",
"Behavior, Animal",
"Empathy",
"Evolution",
"Famous Persons",
"History, 19th Century",
"Humans",
"Religion",
"Social Values"
]
}

Search all, only return the name and the pmid, limit 5

> db.articles.find({},{"title":1,"pmid":1}).limit(5).forEach(printjson);
{
"_id" : 20665232,
"pmid" : 20665232,
"title" : "Charles Darwin's beagle voyage, fossil vertebrate succession, and \"the gradual birth & death of species\"."
}
{
"_id" : 20626121,
"pmid" : 20626121,
"title" : "[The biomedical legacy of Charles Darwin]"
}
{
"_id" : 20503821,
"pmid" : 20503821,
"title" : "Darwin and the popularization of evolution."
}
{
"_id" : 20481191,
"pmid" : 20481191,
"title" : "Between the Beagle and the barnacle: Darwin's microscopy, 1837-1854."
}
{
"_id" : 20338536,
"pmid" : 20338536,
"title" : "Darwin as a student of behavior."
}

Search all, only returns the name and the date, sort by year, limit 5

> db.articles.find({},{"title":1,"created":1}).limit(5).sort({"created.year":1}).forEach(printjson);

{
"_id" : 20255988,
"created" : {
"year" : 1947,
"month" : 12,
"day" : 1
},
"title" : "Charles Darwin's life at Downe."
}
{
"_id" : 14840951,
"created" : {
"year" : 1951,
"month" : 12,
"day" : 1
},
"title" : "Some letters from Charles Darwin to Jeffries Wyman."
}
{
"_id" : 13110175,
"created" : {
"year" : 1954,
"month" : 12,
"day" : 1
},
"title" : "The life of the shawl."
}
{
"_id" : 13457284,
"created" : {
"year" : 1957,
"month" : 12,
"day" : 1
},
"title" : "[Charles Darwin; 1809-1882.]"
}
{
"_id" : 13377935,
"created" : {
"year" : 1957,
"month" : 12,
"day" : 1
},
"title" : "[Darwin and Freud; on Sigmund Freud's centenary.]"
}

Search all, omit some fields, limit 2

> db.articles.find({},{title:0,pmid:0,authors:0,created:0,mesh:0,journal:0}).limit(2).forEach(printjson);
{
"_id" : 20665232,
"issue" : "2",
"volume" : "43",
"pgn" : "363-99",
"doi" : "10.1007/s10739-009-9189-9",
"lang" : "eng"
}
{
"_id" : 20626121,
"issue" : "2",
"volume" : "146",
"pgn" : "87-9",
"lang" : "spa"
}

Sub documents: Search articles published in the 'Lancet',limit 2

> db.articles.find({"journal.title":"Lancet"}).limit(2).forEach(printjson);
{
"_id" : 19205083,
"pmid" : 19205083,
"created" : {
"year" : 2009,
"month" : 2,
"day" : 9
},
"title" : "What Darwin learned in medical school.",
"issue" : "9662",
"volume" : "373",
"pgn" : "454-5",
"journal" : {
"title" : "Lancet",
"abbr" : "Lancet"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "Niles",
"lastName" : "Eldredge"
}
],
"mesh" : [
"Education, Medical",
"Educational Status",
"Evolution",
"History, 19th Century",
"Natural History",
"Schools, Medical",
"Scotland"
]
}
{
"_id" : 11597663,
"pmid" : 11597663,
"created" : {
"year" : 2001,
"month" : 10,
"day" : 12
},
"title" : "Darwin the philosopher?",
"issue" : "9288",
"volume" : "358",
"pgn" : "1118",
"journal" : {
"title" : "Lancet",
"abbr" : "Lancet",
"issn" : "0140-6736"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "J",
"lastName" : "Radcliffe Richards"
}
],
"mesh" : [
"History, 19th Century",
"Philosophy"
]
}

Only print the first author of each article

> db.articles.find({},{"authors":{$slice:1}}).limit(3).forEach(printjson);
{
"_id" : 20665232,
"pmid" : 20665232,
"created" : {
"year" : 2010,
"month" : 8,
"day" : 10
},
"title" : "Charles Darwin's beagle voyage, fossil vertebrate succession, and \"the gradual birth & death of species\".",
"issue" : "2",
"volume" : "43",
"pgn" : "363-99",
"journal" : {
"title" : "Journal of the history of biology",
"abbr" : "J Hist Biol",
"issn" : "0022-5010"
},
"doi" : "10.1007/s10739-009-9189-9",
"lang" : "eng",
"authors" : [
{
"firstName" : "Paul D",
"lastName" : "Brinkman"
}
],
"mesh" : [
"Animals",
"Fossils",
"History, 19th Century",
"Natural History",
"Phylogeny",
"Vertebrates"
]
}
{
"_id" : 20626121,
"pmid" : 20626121,
"created" : {
"year" : 2010,
"month" : 7,
"day" : 14
},
"title" : "[The biomedical legacy of Charles Darwin]",
"issue" : "2",
"volume" : "146",
"pgn" : "87-9",
"journal" : {
"title" : "Gaceta médica de México",
"abbr" : "Gac Med Mex",
"issn" : "0016-3813"
},
"lang" : "spa",
"authors" : [
{
"firstName" : "Emilio",
"lastName" : "García-Procel"
}
],
"mesh" : [
"Biology",
"Evolution",
"History, 19th Century",
"History, 20th Century",
"Medicine"
]
}
{
"_id" : 20503821,
"pmid" : 20503821,
"created" : {
"year" : 2010,
"month" : 5,
"day" : 27
},
"title" : "Darwin and the popularization of evolution.",
"issue" : "1",
"volume" : "64",
"pgn" : "5-24",
"journal" : {
"title" : "Notes and records of the Royal Society of London",
"abbr" : "Notes Rec R Soc Lond",
"issn" : "0035-9149"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "Bernard",
"lastName" : "Lightman"
}
],
"mesh" : [
"Biology",
"Evolution",
"Genetic Fitness",
"History, 19th Century",
"History, 20th Century",
"Humans",
"Male",
"Philosophy",
"Religion",
"Science",
"Selection, Genetic",
"United States"
]
}

Only print the last author of each article

> db.articles.find({},{"authors":{$slice:-1}}).limit(3).forEach(printjson);
{
"_id" : 20665232,
"pmid" : 20665232,
"created" : {
"year" : 2010,
"month" : 8,
"day" : 10
},
"title" : "Charles Darwin's beagle voyage, fossil vertebrate succession, and \"the gradual birth & death of species\".",
"issue" : "2",
"volume" : "43",
"pgn" : "363-99",
"journal" : {
"title" : "Journal of the history of biology",
"abbr" : "J Hist Biol",
"issn" : "0022-5010"
},
"doi" : "10.1007/s10739-009-9189-9",
"lang" : "eng",
"authors" : [
{
"firstName" : "Paul D",
"lastName" : "Brinkman"
}
],
"mesh" : [
"Animals",
"Fossils",
"History, 19th Century",
"Natural History",
"Phylogeny",
"Vertebrates"
]
}
{
"_id" : 20626121,
"pmid" : 20626121,
"created" : {
"year" : 2010,
"month" : 7,
"day" : 14
},
"title" : "[The biomedical legacy of Charles Darwin]",
"issue" : "2",
"volume" : "146",
"pgn" : "87-9",
"journal" : {
"title" : "Gaceta médica de México",
"abbr" : "Gac Med Mex",
"issn" : "0016-3813"
},
"lang" : "spa",
"authors" : [
{
"firstName" : "Emilio",
"lastName" : "García-Procel"
}
],
"mesh" : [
"Biology",
"Evolution",
"History, 19th Century",
"History, 20th Century",
"Medicine"
]
}
{
"_id" : 20503821,
"pmid" : 20503821,
"created" : {
"year" : 2010,
"month" : 5,
"day" : 27
},
"title" : "Darwin and the popularization of evolution.",
"issue" : "1",
"volume" : "64",
"pgn" : "5-24",
"journal" : {
"title" : "Notes and records of the Royal Society of London",
"abbr" : "Notes Rec R Soc Lond",
"issn" : "0035-9149"
},
"lang" : "eng",
"authors" : [
{
"firstName" : "Bernard",
"lastName" : "Lightman"
}
],
"mesh" : [
"Biology",
"Evolution",
"Genetic Fitness",
"History, 19th Century",
"History, 20th Century",
"Humans",
"Male",
"Philosophy",
"Religion",
"Science",
"Selection, Genetic",
"United States"
]
}

comparators: Print the title and the date for the articles published between February and March 2009.

> db.articles.find({"created.year":2009,"created.month":{$lt:4,$gt:1} },{title:1,created:1}).limit(2).forEach(printjson);

{
"_id" : 19283711,
"created" : {
"year" : 2009,
"month" : 3,
"day" : 17
},
"title" : "The day of immunology 2009."
}
{
"_id" : 19258529,
"created" : {
"year" : 2009,
"month" : 3,
"day" : 4
},
"title" : "MMBR to highlight microbial evolution, diversity, and ecology in 2009."
}

Print the title and the meshs for the articles NOT having ('Evolution' and 'Animals') in the mesh list.

> db.articles.find({mesh:{$nin:["Evolution","Animals"]} },{title:1,mesh:1}).limit(2).forEach(printjson);

{
"_id" : 20338529,
"title" : "The Darwin of pangenesis.",
"mesh" : [
"Cell Biology",
"History, 19th Century",
"Natural History",
"Physiology",
"Reproduction",
"Selection, Genetic"
]
}
{
"_id" : 20338527,
"title" : "Cross- and self-fertilization of plants.",
"mesh" : [
"Bibliography as Topic",
"Botany",
"Correspondence as Topic",
"Crosses, Genetic",
"Fertilization",
"Flowers",
"History, 19th Century",
"Orchidaceae",
"Plant Physiological Phenomena",
"Pollination"
]
}

Print the title and the authors for the articles having two authors, limit 2.

>db.articles.find({authors:{$size:2}} ,{title:1,authors:1}).limit(2).forEach(printjson);

{
"_id" : 20338526,
"title" : "Minute observations and theoretical framework of Darwin's studies on climbing plants.",
"authors" : [
{
"firstName" : "Jean-Marc",
"lastName" : "Drouin"
},
{
"firstName" : "Thierry",
"lastName" : "Deroin"
}
]
}
{
"_id" : 20338522,
"title" : "A non-Darwinian Darwin: An introduction.",
"authors" : [
{
"firstName" : "Jean",
"lastName" : "Gayon"
},
{
"firstName" : "Michel",
"lastName" : "Veuille"
}
]
}

Search for articles having a pmc-id.

> db.articles.find({pmc:{$exists:true}} ,{title:1,pmc:1}).limit(2).forEach(printjson);
{
"_id" : 19884139,
"title" : "Darwin's contributions to our understanding of emotional expressions.",
"pmc" : "PMC2781895"
}
{
"_id" : 19258529,
"title" : "MMBR to highlight microbial evolution, diversity, and ecology in 2009.",
"pmc" : "PMC2650884"
}

Search for articles lacking a pmc-id.

>db.articles.find({pmc:{$exists:false}} ,{title:1,pmc:1}).limit(2).forEach(printjson);
{
"_id" : 20665232,
"title" : "Charles Darwin's beagle voyage, fossil vertebrate succession, and \"the gradual birth & death of species\"."
}
{ "_id" : 20626121, "title" : "[The biomedical legacy of Charles Darwin]" }

Number of article where the title is not a string


> db.articles.find({title:{$not:{$type:2}}}).count();

0

OR operator:Articles published in 'Nature' or 'Lancet',limit 3

> db.articles.find({$or:[{"journal.title":"Lancet"},{"journal.title":"Nature"}]},{title:1,"journal.title":1}).limit(3).forEach(printjson);

{
"_id" : 19242459,
"title" : "Q&A: Getting under Darwin's skin. Interview by Adam Rutherford.",
"journal" : {
"title" : "Nature"
}
}
{
"_id" : 19205083,
"title" : "What Darwin learned in medical school.",
"journal" : {
"title" : "Lancet"
}
}
{
"_id" : 19020602,
"title" : "Birthdays to remember.",
"journal" : {
"title" : "Nature"
}
}

AND operator:Number of articles published in 'Nature' AND 'Lancet',limit 2

> db.articles.find({$and:[{"journal.title":"Lancet"},{"journal.title":"Nature"}]}).count();
0

REGEX operator:Search articles having a word starting with 'darwin'

> db.articles.find({title:/darwin[a-z]+/i },{title:1}).limit(5).forEach(printjson);

{
"_id" : 20338530,
"title" : "Sexual selection: Another Darwinian process."
}
{ "_id" : 20338522, "title" : "A non-Darwinian Darwin: An introduction." }
{
"_id" : 19784612,
"title" : "The predictability of evolution: glimpses into a post-Darwinian world."
}
{
"_id" : 19213802,
"title" : "Darwinian evolution in the light of genomics."
}
{
"_id" : 19203139,
"title" : "Darwinian theory, functionalism, and the first American psychological revolution."
}

Using javascript $where: articles starting with 'DARWIN'

> db.articles.find({$where:"this.title.substr(0,6)==\"DARWIN\""},{title:1}).limit(5).forEach(printjson);

{
"_id" : 14341734,
"title" : "DARWIN AS THE SOURCE OF FREUD'S NEO-LAMARCKIANISM."
}
{ "_id" : 14275525, "title" : "DARWIN'S ILLNESS." }
{
"_id" : 14248443,
"title" : "DARWIN'S HEALTH IN RELATION TO HIS VOYAGE TO SOUTH AMERICA."
}
{ "_id" : 14217140, "title" : "DARWIN'S ILLNESS." }

Array element by position: First author is Darwin

> db.articles.find({"authors.0.lastName":"Darwin"},{authors:1,title:1}).forEach(printjson);

{
"_id" : 11640659,
"title" : "[Not Available]",
"authors" : [
{
"firstName" : "C",
"lastName" : "Darwin"
}
]
}

Min/Max operator with indexed properties. Print PMID between [797714,382974[


>db.articles.find({},{pmid:1}).max({pmid:797714}).min({pmid:382974}).forEach(printjson);


{ "_id" : 382974, "pmid" : 382974 }
{ "_id" : 385078, "pmid" : 385078 }
{ "_id" : 387661, "pmid" : 387661 }
{ "_id" : 392346, "pmid" : 392346 }
{ "_id" : 395189, "pmid" : 395189 }
{ "_id" : 797709, "pmid" : 797709 }
{ "_id" : 797713, "pmid" : 797713 }

Map reduce: build a database containing all the distinct authors' names

> db.eval( function()
{
db.articles.find().forEach(function(o)
{
for(i in o.authors)
{
var s= o.authors[i].lastName;
db.names.save({name:s, _id:s});
}
}
);});
> db.names.find().limit(10).forEach(printjson);



{ "_id" : "Hodge", "name" : "Hodge" }
{ "_id" : "Deutsch", "name" : "Deutsch" }
{ "_id" : "Bellini", "name" : "Bellini" }
{ "_id" : "Torgerson", "name" : "Torgerson" }
{ "_id" : "Brinkman", "name" : "Brinkman" }
{ "_id" : "Alfvén", "name" : "Alfvén" }
{ "_id" : "Derry", "name" : "Derry" }
{ "_id" : "Fara", "name" : "Fara" }
{ "_id" : "Eldredge", "name" : "Eldredge" }
{ "_id" : "Buss", "name" : "Buss" }

Distinct operator: get all the distinct publication year

> db.articles.distinct("created.year")

[
1947,
1951,
1954,
1957,
1959,
1960,
1963,
1965,
1967,
1969,
1970,
1971,
1972,
1973,
1974,
1975,
1977,
1978,
1979,
1980,
1981,
1982,
1983,
1984,
1985,
1986,
1987,
1989,
1990,
1991,
1992,
1993,
1994,
1995,
1996,
1997,
1998,
1999,
2000,
2001,
2002,
2003,
2004,
2005,
2006,
2007,
2008,
2009,
2010
]

Distinct operator: get all the distinct publication years for the Journal 'Nature'

> db.articles.distinct("created.year",{"journal.title":"Nature"})

[ 1969, 1982, 1983, 2001, 2004, 2005, 2007, 2008, 2009 ]

GROUP operator: the number of articles per journal having mesh='Evolution' and having a number of articles greater than 2

> db.articles.group(
{
key:{},
cond:{mesh:"Evolution"},
initial:{journal:{},total:0},
reduce: function(object, aggregate)
{
var count=aggregate.journal[object.journal.title];
if(!count)
{
count=0;
}
count++;
aggregate.journal[object.journal.title]=count;
aggregate.total++;
},
finalize:function(aggregate)
{
for(j in aggregate.journal)
{
if( aggregate.journal[j]<3)
{
delete aggregate.journal[j];
}
}
}
})



[
{
"journal" : {
"Comptes rendus biologies" : 5,
"Läkartidningen" : 6,
"Isis; an international review devoted to the history of science and its cultural influences" : 6,
"Tidsskrift for den Norske lægeforening : tidsskrift for praktisk medicin, ny række" : 3,
"Endeavour" : 3,
"Journal of the history of biology" : 9,
"Die Naturwissenschaften" : 4,
"The American psychologist" : 7,
"Scientific American" : 3,
"Current biology : CB" : 5,
"Science (New York, N.Y.)" : 3,
"History and philosophy of the life sciences" : 8,
"Nature" : 10,
"Studies in history and philosophy of biological and biomedical sciences" : 5,
"Journal of the history of the behavioral sciences" : 5,
"Singapore medical journal" : 4,
"Rivista di biologia" : 4,
"Annals of science" : 3,
"Journal of the history of medicine and allied sciences" : 4
},
"total" : 207
}
]

Remove some fields for the journal published in Nature

> db.articles.update({"journal.title":"Nature"},{$unset:{title:1,authors:1,created:1,mesh:1}},false,true)
> db.articles.find({"journal.title":"Nature"}).limit(2).forEach(printjson);



{
"_id" : 19242459,
"doi" : "10.1038/4571087b",
"issue" : "7233",
"journal" : {
"title" : "Nature",
"abbr" : "Nature"
},
"lang" : "eng",
"pgn" : "1087",
"pmid" : 19242459,
"volume" : "457"
}
{
"_id" : 19020602,
"doi" : "10.1038/456324a",
"issue" : "7220",
"journal" : {
"title" : "Nature",
"abbr" : "Nature"
},
"lang" : "eng",
"pgn" : "324-5",
"pmid" : 19020602,
"volume" : "456"
}

Add another author for PMID:382974

> db.articles.update({"pmid":382974},{$push:{authors:{firstName:"John",lastName:"Doe"}}},false,true)
> db.articles.find({"pmid":382974},{authors:1}).forEach(printjson)


{
"_id" : 382974,
"authors" : [
{
"firstName" : "N",
"lastName" : "Freire-Maia"
},
{
"firstName" : "John",
"lastName" : "Doe"
}
]
}

Remove first author for PMID:382974

> db.articles.update({pmid:382974},{$pop:{authors:-1}},false,true)
> db.articles.find({pmid:382974},{authors:1}).forEach(printjson);


{
"_id" : 382974,
"authors" : [
{
"firstName" : "John",
"lastName" : "Doe"
}
]
}

Insert comment for PMID:382974

> db.articles.update({pmid:382974},{$set:{comment:"this is my comment"}})
> db.articles.find({pmid:382974},{comment:1}).forEach(printjson);


{ "_id" : 382974, "comment" : "this is my comment" }

add 2000000 to the year of publication for PMID:382974

> db.articles.update({pmid:382974},{$inc:{"created.year":2000000}},false)
> db.articles.find({pmid:382974},{created:1}).forEach(printjson);


{
"_id" : 382974,
"created" : {
"day" : 26,
"month" : 10,
"year" : 2001979
}
}


That's it.

Pierre

04 September 2010

Dumping NCBI Gene as XML: my notebook.

Download the NCBI C toolbox

The version I used was ftp://ftp.ncbi.nih.gov/toolbox/ncbi_tools++/CURRENT/NCBI_C_Toolkit/ncbi_c--Jun_15_2010.tar.gz, the uncompress the file.
Change the name to 'ncbi':
mv ncbi_c--Jun_15_2010.tar.gz ncbi
and compile the toolkit:
ncbi/make/makedis.csh
(...)
*********************************************************
*The new binaries are located in ./ncbi/build/ directory*
*********************************************************

Download NCBI gene as ASN.1 for Homo Sapiens


Call gene2xml to transform NCBI gene as ASN.1 to XML


ncbi/build/gene2xml -b -i Homo_sapiens.ags
<?xml version="1.0"?>
<!DOCTYPE Entrezgene-Set PUBLIC "-//NCBI//NCBI Entrezgene/EN" "NCBI_Entrezgene.dtd">
<Entrezgene-Set>

<Entrezgene>
<Entrezgene_track-info>
<Gene-track>
<Gene-track_geneid>1</Gene-track_geneid>
<Gene-track_status value="live">0</Gene-track_status>

<Gene-track_create-date>
<Date>


That's it

Pierre

02 September 2010

Playing with the ming API , a C library for SWF/Flash.My notebook.

Ming is a C library generating some simple Flash/SWF movies. In this post I will describe how I used the Ming API by try to converting to SWF the SVG that was generated in a previous post
Genetic Algorithm with Darwin's Face: Dynamic SVG


The original SVG file

The original SVG file looks like this:
<?xml version="1.0"?>
<svg:svg xmlns:svg="http://www.w3.org/2000/svg" version="1.1" width="160" height="222" style="stroke: none;" viewBox="0 0 160 222">
<svg:g id="face">
<svg:polygon points="110,193 11,320 -67,164" style="fill: rgb(129, 82, 122); opacity: 0.68;"/>
<svg:polygon points="109,30 224,181 -4,-1" style="fill: rgb(213, 113, 17); opacity: 0.52;"/>
<svg:polygon points="128,105 101,0 209,-75" style="fill: rgb(200, 91, 58); opacity: 0.04;"/>
<svg:polygon points="115,183 169,232 193,-29" style="fill: rgb(185, 133, 125); opacity: 0.9;"/>
<svg:polygon points="68,186 47,39 57,30" style="fill: rgb(249, 68, 34); opacity: 0.62;"/>
<svg:polygon points="80,185 108,123 158,131" style="fill: rgb(62, 181, 169); opacity: 0.56;"/>
<svg:polygon points="110,61 211,-86 115,217" style="fill: rgb(10, 46, 216); opacity: 0.86;"/>
<svg:polygon points="115,183 169,232 192,-27" style="fill: rgb(188, 127, 122); opacity: 0.9;"/>
<svg:polygon points="78,197 205,135 85,178" style="fill: rgb(53, 145, 210); opacity: 0.6;"/>
<svg:polygon points="108,64 211,-81 111,220" style="fill: rgb(10, 46, 216); opacity: 0.46;"/>
<svg:polygon points="110,59 210,-88 116,215" style="fill: rgb(10, 46, 216); opacity: 0.86;"/>
<svg:polygon points="53,138 60,45 46,60" style="fill: rgb(13, 98, 29); opacity: 0.8;"/>
(...)


A generic C program using the ming API would look like this:
/* initialize ming */
Ming_init();
/* create a movie */
movie=newSWFMovie();
/* set number of frames */
SWFMovie_setNumberOfFrames(movie, NUM_FRAME);
/* loop over the frames */
for(i=0;i<NUM_FRAME;++i)
{
/* create rectange */
rect = newSWFShape();
SWFShape_movePenTo(rect,x,y);
SWFShape_drawLineTo(rect,x+width,y);
SWFShape_drawLineTo(rect,x+width,y+height);
SWFShape_drawLineTo(rect,x,y+height);
SWFShape_drawLineTo(rect,x,y);
/* add figure in the movie */
SWFMovie_add(swf, rect);
(...)
/* add frame */
SWFMovie_nextFrame(movie);
}
/* save movie to file */
WFMovie_save(movie,"file.swf");
/* dispose movie */
destroySWFMovie(movie);
I created a simple C code transforming my SVG to SWF using the ming API. The file contains 5 frames where I slightly moved some shapes. The code was posted on github at: http://github.com/lindenb/ccsandbox/blob/master/src/svg2swf.c.
Compile & run:
export LD_LIBRARY_PATH=${ming.lib.dir}
gcc -L ${ming.lib.dir} -I ${ming.include.dir} `xml2-config --cflags ` svg2swf.c -lming `xml2-config --libs `
./a.out -o darwin.swf darw.svg




That's it
Pierre

11 August 2010

Mwncbi, a mediawiki extension loading asynchronously some records from the NCBI

I've just created new extension for mediawiki. This extension creates a new handler for three new tags :<ncbigene/> , <ncbisnp> and <ncbipubmed>.

Each of those tags download asynchronously a XML record from the NCBI (Gene, NCBI Pubmed or dbSNP) using NCBI-EFetch. The XML is then transformed to HTML on the client side using a XSLT transformation and inserted in the mediawiki page. (As the XSLT processor is specific from Firefox I'm afraid this extension won't run for the other browsers ). As I'm using XSLT, the stylesheets are easily modifiable and hence, the HTML rendering is truly customizable.

The source code is available on github: http://github.com/lindenb/mw4bio/

The installation was described on Mediawiki.org: http://www.mediawiki.org/wiki/Extension:Mwncbi.

Screenshots


Gene



Pubmed



DBSNP





That's it

Pierre

06 August 2010

A MediaWiki extension displaying the UCSC Genome Browser

Today I wrote an extension for mediawiki displaying an HTML <iframe/> to the UCSC Genome Browser. This extension will help my colleagues to annotate some candidate genes threw our local wiki.

This extension handles a new tag <ucsciframe> composed of three required parameters: 'chrom', 'start' and 'end'.

For example
<ucsciframe chrom="chr2" start="98987" end="9879899"/>
The source code for this extension is available at:and its documentation is available on www.mediawiki.org.

That's it !

Pierre

04 August 2010

A Tiny Genome Browser (XHTML/Javascript/json/svg)

I'm currently working on a small set of NGS data and all I need is a simple genome browser to display my mutations while being able to select some SNPs according to some criteria (is it a known rs## , what is the prediction with SIFT or/and Polyphen ?, etc... ). As it is a relative small amount of data, it can be embedded in a webpage without needing a web server.

First, I need a description of the genes as JSON: I described yesterday how I generated this file using mysql, XSLT and the data from the UCSC:

var knownGenes=[{"name":"uc001aaa.3","chrom":"chr1","strand":"+","txStart":11873,"txEnd":14409,"cdsStart":11873,"cdsEnd":11873,"exonCount":3,"exonStarts":[11873,12612,13220],"exonEnds":[12227,12721,14409],"proteinID":"","alignID":"uc001aaa.3"}
,{"name":"uc010nxq.1","chrom":"chr1","strand":"+","txStart":11873,"txEnd":14409,"cdsStart":12189,"cdsEnd":13639,"exonCount":3,"exonStarts":[11873,12594,13402],"exonEnds":[12227,12721,14409],"proteinID":"B7ZGX9","alignID":"uc010nxq.1"}
,{"name":"uc010nxr.1","chrom":"chr1","strand":"+","txStart":11873,"txEnd":14409,"cdsStart":11873,"cdsEnd":11873,"exonCount":3,"exonStarts":[11873,12645,13220],"exonEnds":[12227,12697,14409],"proteinID":"","alignID":"uc010nxr.1"},
(...)];


Second, I need a JSON description of my data:
/** these are ==RANDOM== data */
var variations=[
{"chrom":"chr1","start":12874,"end":12875,"ref":"A","alt":"G","struct":false,"depth":8,"sift":"TOLERATED","pph2div":"benign","pph2var":"benign","rs":null,"hq":true,"id":0},
{"chrom":"chr1","start":792398,"end":792399,"ref":"A","alt":"G","struct":false,"depth":89,"sift":"TOLERATED","pph2div":"probably damaging","pph2var":"benign","rs":null,"hq":true,"id":1},
{"chrom":"chr1","start":959761,"end":959762,"ref":"A","alt":"G","struct":false,"depth":76,"sift":"DAMAGING","pph2div":"probably damaging","pph2var":"benign","rs":null,"hq":true,"id":2}
,(...)];

Third, I wrote a small genome browser using javascript , displaying the data as SVG and using the two previous files. Here is a screenshot:


I posted a random sample of the files on http://gist.github.com/508676, feel free to play with it.

That's it
Pierre

03 August 2010

Curvilnear perspective

I've always been fascinated by Albert Flocon's book about Curvilinear Perspective.

I started the article in the french wikipedia and I've searched for a long time how to generate those figures. The solution was rather simple simple: For a point with the 3D coordinates:
(x0,y0,z0)
. Then, in a curvilinear perspective
dist=sqrt(x1^2 + y1^2 + z1^2)

x=x1/dist
y=y1/dist
I can now generate my own drawings! :-)

Using a curvilinear perspective...
>
... or using a classical vanishing point.

That's it,

Pierre

Creating a TAR file in C++

I just wrote a C++ class creating a Tar file. The source code is available at:http://github.com/.../tarball.h. Creating such archive is useful when a web server should return a set of files or when a tool generates a whole bunch of files.

Example

~> g++ -Wall -I ${PATH_TO_SRC}/cclindenb/src/core
tarfile.cpp
~> ./a.out
~> tar tvf archive.tar
-rw-r--r-- pierre/users 14 2010-08-03 20:50 myfiles/item1.txt
-rw-r--r-- pierre/users 14 2010-08-03 20:50 myfiles/item2.txt
-rw-r--r-- pierre/users 692 2010-08-03 20:50 myfiles/code.cpp

tar xvf archive.tar --to-stdout myfiles/item1.txt 2> /dev/null
Hello World 1


That's it,
Pierre

Transforming mysql results to JSON using XSLT

The option '-X' of mysql produces a XML output:

mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A \
-e 'select * from knownGene where chrom="chr1" limit 2' -X -D hg19

<?xml version="1.0"?>
<resultset statement="select * from knownGene where chrom=&quot;chr1&quot; limit 2
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="name">uc001aaa.3</field>
<field name="chrom">chr1</field>
<field name="strand">+</field>
<field name="txStart">11873</field>
<field name="txEnd">14409</field>
<field name="cdsStart">11873</field>
<field name="cdsEnd">11873</field>
<field name="exonCount">3</field>
<field name="exonStarts">11873,12612,13220,</field>
<field name="exonEnds">12227,12721,14409,</field>
<field name="proteinID"></field>
<field name="alignID">uc001aaa.3</field>
</row>

<row>
<field name="name">uc010nxq.1</field>
<field name="chrom">chr1</field>
<field name="strand">+</field>
<field name="txStart">11873</field>
<field name="txEnd">14409</field>
<field name="cdsStart">12189</field>
<field name="cdsEnd">13639</field>
<field name="exonCount">3</field>
<field name="exonStarts">11873,12594,13402,</field>
<field name="exonEnds">12227,12721,14409,</field>
<field name="proteinID">B7ZGX9</field>
<field name="alignID">uc010nxq.1</field>
</row>
</resultset>
I wrote a simple xslt stylesheet transforming this XML to JSON so I can easily use those sql results in a dynamic HTML page. The stylesheet is available at:

Example

:
mysql --user=genome --host=genome-mysql.cse.ucsc.edu -A \
-e 'select * from knownGene where chrom="chr1" limit 2' > query.xml
xsltproc sql2json.xsl query.xml
[
{
"name":"uc001aaa.3",
"chrom":"chr1",
"strand":"+",
"txStart":11873,
"txEnd":14409,
"cdsStart":11873,
"cdsEnd":11873,
"exonCount":3,
"exonStarts":"11873,12612,13220,",
"exonEnds":"12227,12721,14409,",
"proteinID":"",
"alignID":"uc001aaa.3"
},
{
"name":"uc010nxq.1",
"chrom":"chr1",
"strand":"+",
"txStart":11873,
"txEnd":14409,
"cdsStart":12189,
"cdsEnd":13639,
"exonCount":3,
"exonStarts":"11873,12594,13402,",
"exonEnds":"12227,12721,14409,",
"proteinID":"B7ZGX9",
"alignID":"uc010nxq.1"
}]
The stylesheet uses two optional parameters: var=name defines a javascript variable named 'var' and ucsc=true interprets some fields from the UCSC database: for example 'exonStarts' will be treated as an array of integers.
xsltproc --stringparam var genes --stringparam ucsc true sql2json.xsl query.xml
var genes=[
{
"name":"uc001aaa.3",
"chrom":"chr1",
"strand":"+",
"txStart":11873,
"txEnd":14409,
"cdsStart":11873,
"cdsEnd":11873,
"exonCount":3,
"exonStarts":[11873,12612,13220],
"exonEnds":[12227,12721,14409],
"proteinID":"",
"alignID":"uc001aaa.3"
},
{
"name":"uc010nxq.1",
"chrom":"chr1",
"strand":"+",
"txStart":11873,
"txEnd":14409,
"cdsStart":12189,
"cdsEnd":13639,
"exonCount":3,
"exonStarts":[11873,12594,13402],
"exonEnds":[12227,12721,14409],
"proteinID":"B7ZGX9",
"alignID":"uc010nxq.1"
}
];


That's it,
Pierre