<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Cognitive Consonance</title>
	
	<link>http://tiago.org/cc</link>
	<description>Software engineering in a computational biology environment</description>
	<lastBuildDate>Mon, 06 Sep 2010 14:09:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CognitiveConsonance" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="cognitiveconsonance" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>ogaraK: A simulator of malaria population genetics in Groovy</title>
		<link>http://tiago.org/cc/2010/09/06/ogarak-a-simulator-of-malaria-population-genetics-in-groovy/</link>
		<comments>http://tiago.org/cc/2010/09/06/ogarak-a-simulator-of-malaria-population-genetics-in-groovy/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 14:09:49 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Bioinformatics]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[malaria]]></category>
		<category><![CDATA[population genetics]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=228</guid>
		<description><![CDATA[I would like to announce ogaraK, a simulator of malaria population genetics. ogaraK is a Java Web Start application developed in Groovy and Java which allows to simulate parasite population genetics. It can be used, for instance, to compare the effects of different drug deployment policies. A set of Python scripts are made available to [...]]]></description>
			<content:encoded><![CDATA[<p><img align="center" src="http://popgen.eu/soft/ogaraK/title.png" alt="ogaraK" /></p>
<p>I would like to announce <a href="http://popgen.eu/soft/ogaraK">ogaraK</a>,  a simulator of malaria population genetics.</p>
<p>ogaraK is a Java Web Start application developed in Groovy and Java which allows to simulate parasite population genetics. It can be used, for instance, to compare the effects of different drug deployment policies.</p>
<p>A set of Python scripts are made available to analyze the results (mainly frequencies of resistance loci over time).</p>
<p>ogaraK is free software (GPL v3).</p>
<p>ogaraK can also be used to simulate some of common theories about sex: Epistasis, Red-Queen and spatial heterogeneity (but not based on size, as the underlying model has no concept of population numbers).</p>
<p>I would like to ask the reader for some basic help: If you can, could you please test the application (A single click to run, if you have Java 1.5+)? I know most people will not understand the results or the parameters, but just a simple run would help (and reporting back if something goes wrong!). The objective is to make the application available to epidemiologists, and due to their lack of IT knowledge, a robust application is needed. Any comments would really be appreciated (I make no financial profit from this free application). If you know Java Web Start, then if you could activate the console and return any error detected (even from a blind execution), that would be most appreciated.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/5qYWcuo7Lrs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2010/09/06/ogarak-a-simulator-of-malaria-population-genetics-in-groovy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>In praise of Groovy</title>
		<link>http://tiago.org/cc/2010/08/27/in-praise-of-groovy/</link>
		<comments>http://tiago.org/cc/2010/08/27/in-praise-of-groovy/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 12:36:12 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=211</guid>
		<description><![CDATA[Preamble: The problem of writing a defense of a certain thing X is that, most people interpret that as an attack to potential alternatives. This is not how this post should be read. There is no One Single Solution. My defense of Groovy is based on a set of assumptions that do not hold true [...]]]></description>
			<content:encoded><![CDATA[<p>Preamble: The problem of writing a defense of a certain thing X is that, most people interpret that as an attack to potential alternatives. This is not how this post should be read. There is no One Single Solution. My defense of Groovy is based on a set of assumptions that do not hold true for many people. In fact, they do not even hold true for myself. Different people and different development problems entail different solutions.</p>
<p>My main assumption in defense of Groovy is that you are, a Java person. Java is your day to day programming language and you are comfortable with that. Though you are comfortable with that you want to try something different: maybe you want to try a scripting language, maybe you do not like too much boilerplate code.</p>
<p>Why Groovy? Because it gives you a lot of goodies with essentially no learning curve. An illustrative example: I&#8217;ve spent the whole morning thinking that I was editing a Java source file, but I was indeed working on a Groovy file. You see, Groovy not being a superset of Java ends up being almost that: Code in Java is, in most cases already code in Groovy. So, if you know Java, you can write Groovy. You will not write the best idiomatic Groovy, you will not gain any of Groovy&#8217;s goodies (and you will pay a performance penalty, BTW). But this is an amazing head-start if you want to go in the direction of higher-level languages (I would argue that it is even smoother than C from C++ as the paradigm does not change from Java to Groovy, it is OO to OO). If you go the Jython way then you have to learn a new language. If you go Scala or Clojure then you have to learn a whole new paradigm (and deal with the impedance of imperative semantics in typical Java libs against standard functional semantics).</p>
<p>For little to no cost you have now many goodies associated with scripting languages (dynamic, low boiler-plate coding, DSLs, better meta-programming, &#8230;). In some cases Groovy even out-competes supposedly more elegant languages (are Scala meta-programming facilities still as bad as in the past?).</p>
<p>Another interesting advantage of Groovy is that, if you want to revert back to Java then it is much easier. Why would you want to do that? Well, for performance reasons. In a Groovy application that I have, a small part of the code is extremely intensive, so I had to rewrite it in Java. This revealed to be a trivial exercise (similar syntax, similar semantics).</p>
<p>Again, let me stress out this: your requirements and your personal path are fundamental in any decision you take. There is no true language (OK, Prolog&#8230;. <img src='http://tiago.org/cc/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ). Different people, different approaches. All I am saying is: if your background is strongly grounded on Java and you feel comfortable with that, then Groovy is probably the way to go.</p>
<p>Disclaimer: While I have a couple of applications made in Groovy, most of my scripting efforts in the JVM world involve Jython (another fine language implementation, appropriate in a different set of circumstances) and I also believe, that from a declarative and highly expressive language point of view, what is being done with Clojure certainly deserves mention.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/WZNvxbVchGE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2010/08/27/in-praise-of-groovy/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Is Java/JVM still viable?</title>
		<link>http://tiago.org/cc/2010/08/17/is-javajvm-still-viable/</link>
		<comments>http://tiago.org/cc/2010/08/17/is-javajvm-still-viable/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 05:10:59 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=215</guid>
		<description><![CDATA[First, let me tell you a little story: A few years ago I worked for an university and we decided to replace a closed, proprietary backend infrastructure with an open one. The strange thing is that we replaced IBM with&#8230; IBM. You see, a long time ago IBM had a self-centered, closed mentality but since [...]]]></description>
			<content:encoded><![CDATA[<p>First, let me tell you a little story: A few years ago I worked for an university and we decided to replace a closed, proprietary backend infrastructure with an open one. The strange thing is that we replaced IBM with&#8230; IBM. You see, a long time ago IBM had a self-centered, closed mentality but since Gerstner&#8217;s time as IBM CEO things changed a lot. So, the old unpalatable IBM was replaced with the new, desirable IBM. A similar argument can be made for Sun&#8230; you probably still remember the closed Sun, the closed Java. Interestingly when we replaced IBM with IBM we also replaced the database backend: from Oracle to IBM.</p>
<p>Oracle.</p>
<p>Let me continue my little story: Fast-forward a few years and I accepted a job at the same university as data centre manager. The 3 biggest universities in the city had bought the same accounting/payroll system. This system had a Oracle backend: database, application server and financial extensions. It so happened that each component had completely different licensing terms: one was on concurrent connections another on concurrent users, yet another on the total number of users. The notion of user also varied: The financial application had thousands of users, but it connected with a single user to the application server. The usage of multiple CPUs was also subjected to licensing. I hate to think on the countless hours spent by the data centre managers just parsing all this. It still gives me a headache.</p>
<p>But it does not stop here: even as a manager I was still very technically inclined (and had previous experience as a database manager): I would subscribe to the view that Oracle databases are a too-much complex database system that seem to be highly profitable to Oracle and the contractors that are payed to maintain such a complex system. Between Oracle DB and IBM DB/2 I would take DB/2 any time (at least 5 years ago where my professional path changed). Of course, for most applications PostgreSQL is more than enough&#8230;</p>
<p>The point is, as you probably have noticed by now: I am not comfortable with Oracle&#8217;s corporate culture. I am not comfortable with Oracle taking the lead on Java. In fact I can think of no worse corporation to lead Java.</p>
<p>I would also like to draw your attention to size and power bias. My point is this: people tend to dislike Microsoft, but <i>part</i> of that dislike does not come from corporate culture but from the influence and power that Microsoft has (still) in computing. I dread to think of the consequences of people like Larry Ellison or Steve Jobs having the same influence as Bill Gates. I am not defending Bill, just suggesting that it could be much worse. We do not feel the pain of Oracle (or Apple for that matter) because they do not have the massive OS/Office market share that MS has.</p>
<p>I always had a love relationship with Java (though I have a background in declarative languages). It was amazing to see the appearance of new languages on top of the JVM (I use Groovy, Clojure and Jython). The idea of a shared VM (and shared libraries) which a set of cooperating and competing languages on top is wonderful&#8230;</p>
<p>But I am starting to doubt that the JVM world will stay an open community where the best ideas can be incepted and flourish.</p>
<p>If IBM had bought Sun&#8230;</p>
<p>I do not trust you, Larry!</p>
<p><small><br />
Recommending readings (from James Gosling&#8217;s blog):<br />
<a href="http://nighthacks.com/roller/jag/entry/quite_the_firestorm">Quite the firestorm</a><br />
<a href="http://nighthacks.com/roller/jag/entry/the_shit_finally_hits_the">The shit finally its the fan</a><br />
<a href="http://nighthacks.com/roller/jag/entry/cynical_chuckles">Cynical chuckes</a><br />
</small></p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/y9NaXIFIzNE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2010/08/17/is-javajvm-still-viable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool Links</title>
		<link>http://tiago.org/cc/2010/08/17/cool-links/</link>
		<comments>http://tiago.org/cc/2010/08/17/cool-links/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 03:37:17 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=144</guid>
		<description><![CDATA[Nice reads mostly older: [jvm] YouDebug &#8211; A debug with a scriptable UI (not a GUI). Allowing for automated debugging. The main justification for this is e.g. on costumer sites where you cannot fire a graphical debugger. But an automated debugging framework can be the ground for some cool debugging tools. Furthermore it is scriptable [...]]]></description>
			<content:encoded><![CDATA[<p>Nice reads mostly older:</p>
<ul>
<li>[jvm] <a href="http://youdebug.kenai.com/">YouDebug</a> &#8211; A debug with a scriptable UI (not a GUI). Allowing for automated debugging. The main justification for this is e.g. on costumer sites where you cannot fire a graphical debugger. But an automated debugging framework can be the ground for some cool debugging tools. Furthermore it is scriptable in Groovy!</li>
<li>[general]<a href="http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey">Are we there yet?</a> &#8211; &#8220;Rich Hickey advocated for the reexamination of basic principles like state, identity, value, time, types, genericity, complexity, as they are used by OOP today, to be able to create the new constructs and languages to deal with the massive parallelism and concurrency of the future&#8221;. This is a 1 hour video (with synchronized slides). While I disagree with the basic premise that concurrency is fundamental in the future, this presentation is one of the most invaluable videos about the design of programming languages.
	</li>
<li>[lisp] <a href="http://www.google.com/calendar/embed?src=pm55j8kg30dnm54ib2if9fuocc%40group.calendar.google.com&#038;ctz=Europe/London">A Lisp User-group meetings calendar</a> (Google based)</li>
<li>[clojure] <a href="http://muckandbrass.com/web/display/~cemerick/2009/11/03/Be+mindful+of+Clojure%27s+binding">Be mindful of Clojure&#8217;s binding</a> &#8211; Pitfalls of lazy sequences and thread bindings. The lazy sequence stuff seems more of an example of global variables are bad thing, as the example is dependent on an external symbol evaluation, but clearly one has to be mindful of lazy versus non-lazy evaluation.</li>
</ul>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/lGMlPHDsqfQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2010/08/17/cool-links/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lisp and Clojure: prefix notation and parentheses</title>
		<link>http://tiago.org/cc/2009/12/03/lisp-and-clojure-prefix-notation-and-parentheses/</link>
		<comments>http://tiago.org/cc/2009/12/03/lisp-and-clojure-prefix-notation-and-parentheses/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 11:32:47 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[prolog]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=205</guid>
		<description><![CDATA[I would like to add just a few more arguments regarding the issue of notation. There is a certain austerity tradition coming from the CS community (at least from the BEST part of it). Most people see syntactic sugar as a waste of time and energy. Furthermore everything is just trees and a linear representation [...]]]></description>
			<content:encoded><![CDATA[<p>I would like to add just a few more arguments regarding the issue of notation.</p>
<p>There is a certain austerity tradition coming from the CS community (at least from the BEST part of it). Most people see syntactic sugar as a waste of time and energy. Furthermore everything is just trees and a linear representation with the root at the head is just fine to represent trees. Lisp embodies this tradition to the full. And for the most part is a great tradition.</p>
<p>This is all fine, and I would understand the defensiveness considered that we live in a world polluted with overly complex languages which just bring problems and no advantages to compensate.</p>
<p>But some level of syntactic sugar is important for humans. And while meta-programming is nice, I suppose most programming languages are still used by human programmers <img src='http://tiago.org/cc/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  . </p>
<p>It would suggest to have a look at the literature on human cognition, namely the strategies used to read text: it is messy. But I prefer to go in a different direction. I just want to expose how some subtle representation issues might have a massive impact: Have you ever tried to do basic arithmetic with roman numerals? Have a look <a href="http://mathforum.org/dr.math/faq/faq.roman.html">here</a>, namely &#8220;How can I use Roman numerals to do arithmetic problems?&#8221;. While this is a different problem, it serves to emphasize that representation matters in the most strangest ways.</p>
<p>Even if it doesn&#8217;t, humans are a conservative species, they are not ready to change everything at the same time. Having a to jump to imperative+heteroiconic+infix to functional+homoiconic+prefix is too much. Lisp derived languages loose potential to attract more people. And in this case is is really worth it? Is it really THAT IMPORTANT to be prefixed? Is that the main conceptual selling point of Lisp?</p>
<p><b>But most importantly there are solutions that are not dirty:</b>.</p>
<p>First nobody is forcing infix notation, one can have both at the same time (check Prolog  :- op &#8211; you can do BOTH +(1 2) and 1+2).</p>
<p>Second hard-coding operators is not necessary at all: again check the Prolog suggestion. You can have all infix operators that you want (including none)</p>
<p>It is easy to conceptualize a superset of Lisp with infix notation and ZERO INITIAL infix operators.</p>
<p>If I can shout something is this: Check Prolog&#8217;s :- op ! Simple, elegant. And fully coherent with the Lisp philosophy at its core.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/iwgNlfHioQQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/12/03/lisp-and-clojure-prefix-notation-and-parentheses/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Clojure: “Being a Lisp Is a Handicap”</title>
		<link>http://tiago.org/cc/2009/12/02/clojure-being-a-lisp-is-a-handicap/</link>
		<comments>http://tiago.org/cc/2009/12/02/clojure-being-a-lisp-is-a-handicap/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 15:34:44 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[prolog]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=200</guid>
		<description><![CDATA[Tim Bray presented his 11 Theses on Clojure. Thesis number 2 is &#8220;Being a Lisp Is a Handicap&#8221;. This argument has actually 3 parts: the Lisp-parenthesis syntax, which he finds bad. Homoiconicity (he finds good) and macros (again good). I would like to discuss a bit the first (syntax) and the last (macros). The macro [...]]]></description>
			<content:encoded><![CDATA[<p>Tim Bray presented his <a href="http://www.tbray.org/ongoing/When/200x/2009/12/01/Clojure-Theses">11 Theses on Clojure</a>. Thesis number 2 is &#8220;Being a Lisp Is a Handicap&#8221;. This argument has actually 3 parts: the Lisp-parenthesis syntax, which he finds bad. Homoiconicity (he finds good) and macros (again good).</p>
<p>I would like to discuss a bit the first (syntax) and the last (macros). The macro part discussed somewhere in the future. For now I would like to concentrate on syntax.</p>
<p>Lisp syntax is strange for most humans, we simply do not write (+ (* 2 3) (/ 4 2)). Lisp proposers are probably aware that this will not be very popular, as people are used to something completely different. It is not only different but is also very uneconomical to both read and write: 2*3 + 4/2 is arguably easier to read. One can even make some intuitive declarative things. Notice that I&#8217;ve written 2*3 (no space around *) but 2*3 + 4*2 (spaces around +). This allows to give queues to readers about the precedence of operators. You can say that these issues are just simple and ridiculous syntactic sugar. Well, it seems that <i>Homo sapiens</i> has evolved to appreciate it. And I don&#8217;t really see a reason not to accommodate this requirement.</p>
<p>Yes, I read you already: infix syntax forces hard-coding of operators and precedences (e.g. + precedes *) . Makes parsing much more complex (there goes the beautifully simple Lisp parser out of the window) and in fact forcing a certain structure with hard-coded operators.</p>
<p>Well&#8230; no.</p>
<p>Take a look at Prolog <a href="http://www.cse.unsw.edu.au/~billw/cs9414/notes/prolog/op.html">op built-in</a>. :- op allows you to define operator precedence and association rules. So the parser is really not much complex: it starts with a simple, uber-flexible, blank slate which you can fill in.</p>
<p>So, for (- 4 3 2) you can say &#8211; is an infix operator with first argument with precedence over the second (i.e. (4 &#8211; 3) &#8211; 2) and, voila, you can write 4 &#8211; 3 &#8211; 2. No need for hard-coded operators, all dynamically defined. You could, maybe for sadistic reasons, change the argument precedence so 4 &#8211; 3 &#8211; 2 would become 4 &#8211; (3 &#8211; 2). The flexibility is there. </p>
<p>You can also state that + and &#8211; have higher precedence than  * and /. So that 2*3 + 2 is interpreted as (+ (* 2 3) 2). Again, you can change the precedence rules.</p>
<p>Ah&#8230; and you can have operators that are atoms (alphanumeric symbols). In Prolog you can say &#8220;X is 3 +2&#8243;. &#8220;is&#8221; is an operator. You can redefine operator rules, define new operators.</p>
<p>Operators are nothing more than a set of syntactic sugar rules to convert a more intuitive representation to another. But they really make things much more pleasant and humane.</p>
<p>I would imagine that adding this to Clojure would be DEAD EASY, by the way. </p>
<p>Regarding the issues about macros, I will leave it for another post.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/a81loz5yvgk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/12/02/clojure-being-a-lisp-is-a-handicap/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Brain to symbol impedance</title>
		<link>http://tiago.org/cc/2009/11/19/brain-to-symbol-impedance/</link>
		<comments>http://tiago.org/cc/2009/11/19/brain-to-symbol-impedance/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 18:31:59 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=157</guid>
		<description><![CDATA[What is the importance of the names that are given to things when programming? There seem to be quite some different approaches to this issue. Some people think that calling data items/variables a, b1, c is OK. Less people are seen defending the idea that functions should be called f1, f2, f2. Some other people [...]]]></description>
			<content:encoded><![CDATA[<p>What is the importance of the names that are given to things when programming? There seem to be quite some different approaches to this issue. Some people think that calling data items/variables a, b1, c is OK. Less people are seen defending the idea that functions should be called f1, f2, f2. Some other people think variables should have names connected with their meaning, so, if you have a data item whose content is related to a person object, maybe that variable should be called something with &#8220;person&#8221; on its name, like currentPerson, or whatever.</p>
<p>So far, nothing new. I for one, am in the group of the explicit naming of everything.</p>
<p>Sometimes the name has some impedance between the average expectation and the functionality exposed. That can be a source of lots of grief. Lets have a look at a concrete example to make things clear.</p>
<p>Clojure has a function named contains? . How do you expect such a function to operate? Let me give a few examples (and if you know how it really operates in reality, try to forget for now what you know).</p>
<p>What you think would be the result of:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? <span style="color: #ff0000;">&quot;bla&quot;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? <span style="color: #ff0000;">&quot;bla&quot;</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>contains? '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Does [3 5] contains 3?<br />
Does [3 5] contains 1?<br />
Does &#8220;bla&#8221; contains 1?<br />
Does &#8220;bla&#8221; contains &#8220;a&#8221;?<br />
Does (1 2) contains 1?</p>
<p>Actually (contains? [3 5] 3) is false. Why? Lets have a look at the documentation:</p>
<pre>
clojure.core/contains?
([coll key])
Returns true if key is present in the given collection, otherwise
returns false.  Note that for numerically indexed collections like
vectors and Java arrays, this tests if the numeric key is within the
range of indexes. [...]
</pre>
<p>So contains? is looking at the KEYS of a collection. The vector [3 5] has two keys: 0 and 1 (the indexes with occupation of the said vector), that is why (contains? &#8220;bla&#8221; 1) is true (the string &#8220;bla&#8221; has 3 keys 0, 1 and 2.</p>
<p>By the way, the list (1 2) is not a collection, so that false is caused by a type error.</p>
<p>Note that contains? actually has some utility <img src='http://tiago.org/cc/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  : it is used to check if a map has a certain key.</p>
<p>There is an slight impedance between what most people would expect from &#8220;containing something&#8221; and contains? It so happens that slight impedances are much worse than big ones, because big ones are so damn obvious that they are easy to spot and normally end up being corrected (imagine a function drawLine, that draws a circle: it is a dead obvious problem that one will easily notice and the developer most probably correct). Slight impedance problems are more obnoxious:</p>
<ul>
<li>They will confuse newbies (like myself), whose expectations will be aligned with the general knowledge of a certain name or action.
  </li>
<li>They will increase the cognitive load of experts which will need to be aware of the dissonance between the meaning of a certain word in the programming language and the meaning in the natural language.
  </li>
<li>It will make reading and understanding a program more difficult.
  </li>
<li>They will be a source of bugs, as developers, even experienced one, will sometimes forget the local meaning of a word and unconsciously apply the general meaning.
</li>
</ul>
<p>By the way, my personal solution for contains? Well, quite simply rename it hasKey? and only apply it to maps. Note that this post is not about contains? in particular, but about this &#8220;impedance&#8221; problem in general.</p>
<p>I would imagine that some readers might think this as irrelevant and nitpicking. In my subjective and personal point of view, this issue is a potential source of hard to find bugs and a waste of mental energy. In fact this blog is called Cognitive _Consonance_ for some reason&#8230;</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/CacKTgyn8KI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/19/brain-to-symbol-impedance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading newick/nexus phylogenetic trees with BioJava</title>
		<link>http://tiago.org/cc/2009/11/17/reading-newicknexus-phylogenetic-trees-with-biojava/</link>
		<comments>http://tiago.org/cc/2009/11/17/reading-newicknexus-phylogenetic-trees-with-biojava/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 18:02:11 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[biojava]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[phylogenetics]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=183</guid>
		<description><![CDATA[BioJava has a parser for the nexus file format. While Nexus supports quite a lot of information (like DNA sequences), the most complex part to process is the phylogenetic tree descriptions based on the Newick format. Below you can find a small tutorial on how to process these. BioJava&#8217;s parser relies on JGraphT to create [...]]]></description>
			<content:encoded><![CDATA[<p>BioJava has a parser for the <a href="http://hydrodictyon.eeb.uconn.edu/eebedia/index.php/Phylogenetics:_NEXUS_Format">nexus file format</a>. While Nexus supports quite a lot of information (like DNA sequences), the most complex part to process is the phylogenetic tree descriptions based on the <a href="http://en.wikipedia.org/wiki/Newick_format">Newick format</a>. Below you can find a small tutorial on how to process these.</p>
<p>BioJava&#8217;s parser relies on <a href="http://jgrapht.sourceforge.net/">JGraphT</a> to create a representation of the &#8220;tree&#8221;. The tree is actually really more an acyclic graph than a tree, though some trees are rooted (and therefore trees in the proper sense). Manipulating the JGraphT weighted graph is the complicated part, not really the BioJava interface. Note that JGraphT objects can be easily rendered using the <a href="http://www.jgraph.com/jgraph.html">JGraph</a> library (yeah, it is confusing: there is one lib with graph algorithms called JGraphT and another, for vizualisation, called JGraph).</p>
<p>In this small tutorial, we will only try to write a textual representation of a tree.</p>
<p>Imagine this simple nexus file:</p>
<pre>
#NEXUS

Begin TREES;
	tree test1 = (1,2);
	tree test4 = (1:0.1,(2:0.2,3:0.3):0.4);
End;
</pre>
<p>We just want to draw this:</p>
<pre>
coder@move-on:~/development/biobug/test$ java Test test1.nex test1
Will process file test1.nex tree test1
p0
  1: 1.0
  2: 1.0
coder@move-on:~/development/biobug/test$ java Test test1.nex test4
Will process file test1.nex tree test4
p0
  1: 0.1
  p1: 0.4
    2: 0.2
    3: 0.3
</pre>
<p>So, tree 1 is composed of nodes (leaves) 1 and 2 and the inner node which was named p1. Tree 2 has distances.</p>
<p>By the way, we will also want to know which trees are in the file.</p>
<p>Lets start!.</p>
<p>So, we start by loading and parsing the file:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojavax.bio.phylo.io.nexus.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span>
        <span style="color: #666666; font-style: italic;">//file is a String with the name of the file to be processed</span>
        NexusFileBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NexusFileBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        NexusFileFormat.<span style="color: #006633;">parseFile</span><span style="color: #009900;">&#40;</span>builder, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        NexusFile nexus <span style="color: #339933;">=</span> builder.<span style="color: #006633;">getNexusFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nexus files have several blocks (Taxa, Data, Tree, Set). We are interested in getting the Tree block, lets do a function for that:</p>
<pre language="java">
    TreesBlock getTreeNode(NexusFile nexus) {
        Iterator it = nexus.blockIterator();
        NexusBlock block;
        while(it.hasNext()) {
                block = (NexusBlock)it.next();
                if (block.getBlockName().equals("TREES")) {
                        return (TreesBlock)block;
                }
            }
            return null;
    }
</pre>
<p>We get the nexus block iterator and go through it until we find a block whose name is TREES, and return that block.</p>
<p>No that we have the TREES block, lets print the names of all trees:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000066; font-weight: bold;">void</span> printTrees<span style="color: #009900;">&#40;</span>NexusFile nexus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            TreesBlock node <span style="color: #339933;">=</span> getTreeNode<span style="color: #009900;">&#40;</span>nexus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Map</span> trees <span style="color: #339933;">=</span> node.<span style="color: #006633;">getTrees</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Set</span> keys <span style="color: #339933;">=</span> trees.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Trees:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> obj <span style="color: #339933;">:</span> keys<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>We get a map, where the key is the name of the tree and the value would be the tree as, essentially, a String based (but with some annotated info) representation (not  a graph). Now, given a certain name, lets get the graph:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.biojava.bio.seq.io.ParseException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jgrapht.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jgrapht.graph.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#91;</span>...<span style="color: #009900;">&#93;</span>
&nbsp;
    WeightedGraph<span style="color: #339933;">&lt;</span>string , DefaultWeightedEdge<span style="color: #339933;">&gt;</span> getTree<span style="color: #009900;">&#40;</span>NexusFile nexus, <span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ParseException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> topNode<span style="color: #339933;">;</span>
        TreesBlock node <span style="color: #339933;">=</span> getTreeNode<span style="color: #009900;">&#40;</span>nexus<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        WeightedGraph<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;&lt;</span>string , DefaultWeightedEdge<span style="color: #339933;">&gt;</span> graph <span style="color: #339933;">=</span> node.<span style="color: #006633;">getTreeAsWeightedJGraphT</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        topNode <span style="color: #339933;">=</span> node.<span style="color: #006633;">getTopNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The top node is: &quot;</span> <span style="color: #339933;">+</span> topNode<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> graph<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Note that getTreeAsWeightedJGraphT will do some parsing, so ParsingException can be raised. Note also that the top node name can be retrieved (in the case of tree test1, that will be named p1). Some considerations: You can change the rules to create internal nodes; if there are clashes of names inner nodes will be renamed (not leaves!).</p>
<p>Regarding the top node, we call it top node and not root node. While from a data structure perspective the tree has a root, from a phylogenetic perspective the tree might be rooted or not (in which case being root has no meaning, and it is really just a simple weighted acyclic graph). How to know if the tree is rooted? Remember the function to get all trees (getTrees)? The value of the map has a method called getRootType. So, to know if is rooted, you need to use that function. Not the best design&#8230; but at least works.</p>
<p>Ok, now we just need to print a tree&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> dump<span style="color: #009900;">&#40;</span>WeightedGraph<span style="color: #339933;">&lt;</span>string , DefaultWeightedEdge<span style="color: #339933;">&gt;</span> graph,
            <span style="color: #003399;">String</span> parent, <span style="color: #003399;">String</span> node, <span style="color: #003399;">String</span> depth<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Set<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;&lt;</span>string<span style="color: #339933;">&gt;</span> verts <span style="color: #339933;">=</span> graph.<span style="color: #006633;">vertexSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> vertex <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> candidate <span style="color: #339933;">:</span> verts<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>candidate.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                vertex <span style="color: #339933;">=</span> candidate<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span>depth <span style="color: #339933;">+</span> vertex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>parent <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">+</span> graph.<span style="color: #006633;">getEdgeWeight</span><span style="color: #009900;">&#40;</span>graph.<span style="color: #006633;">getEdge</span><span style="color: #009900;">&#40;</span>parent, vertex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>DefaultWeightedEdge e<span style="color: #339933;">:</span> graph.<span style="color: #006633;">edgesOf</span><span style="color: #009900;">&#40;</span>vertex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>graph.<span style="color: #006633;">getEdgeSource</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>node<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                dump<span style="color: #009900;">&#40;</span>graph, vertex, graph.<span style="color: #006633;">getEdgeTarget</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;  &quot;</span><span style="color: #339933;">+</span> depth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> 
<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Ok, this is the complicated part. Note the following:</p>
<ul>
<li>The complexity has to do with processing graphs</li>
<li>dump is a recursive function</li>
<li>Node is synonym with Vertex</li>
<li>Notice the <strong>important bit</strong>, if you know that there is a node called &#8220;bla&#8221;, it is not enough to do graph.containsVertex(&#8220;bla&#8221;). The answer will probably be false. Remember that one thing is reference (which we have here, i.e. ==) and not content equality (.equals). See below, a remainder</li>
<li>Finally we go through all edges referencing the current vertex and choose the ones that start on the current one. Again, if the tree is unrooted, the notion of direction does not apply, but it is still good to do a &#8220;tree&#8221; traversal</li>
</ul>
<p>We end here.</p>
<p>Regarding the &#8220;equal&#8221; issue remember that:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span> <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Returns false, true. By this order. This is important when traversing the graph. If you know that the reference is equal (and it is when we getEdgeTarget) than one could use it. If you don&#8217;t know (like you pass a String that you have constructed yourself or got from some other place), then one needs to go through the vertex/node list and do a .equals to get the correct vertex.</p>
<p>A small example with all the above, <a href="http://tiago.org/cc/wp-content/uploads/2009/11/Test.java">is here</a>, ready to use.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/kLEOBIXNQJg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/17/reading-newicknexus-phylogenetic-trees-with-biojava/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A phylogenetic tree viewer in clojure</title>
		<link>http://tiago.org/cc/2009/11/16/a-phylogenetic-tree-viewer-in-clojure/</link>
		<comments>http://tiago.org/cc/2009/11/16/a-phylogenetic-tree-viewer-in-clojure/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 19:44:11 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[phylogenetics]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=160</guid>
		<description><![CDATA[Here is my first clojure application: A phylogenetic tree viewer (PhyloXML format). The obligatory screenshot: Preamble: This is newbie code: Handle with care! My main objective is not to make a tree viewer but a tree comparer. So this is no more than a learning step. You can test it yourself as it is a [...]]]></description>
			<content:encoded><![CDATA[<p>Here is my first clojure application: A phylogenetic tree viewer (PhyloXML format). The obligatory screenshot:</p>
<div id="attachment_172" class="wp-caption alignnone" style="width: 461px"><img src="http://tiago.org/cc/wp-content/uploads/2009/11/Screenshot-Simple.png" alt="Simple Phylogenetic viewer" title="Simple" width="451" height="480" class="size-full wp-image-172" /><p class="wp-caption-text">Simple Phylogenetic viewer</p></div>
<p>Preamble:</p>
<ol>
<li>This is newbie code: Handle with care! My main objective is not to make a tree viewer but a tree comparer. So this is no more than a learning step.</li>
<li>You can test it yourself as it is a Java WebStart application, just click <a href="http://popgen.eu/soft/tV/launch.jnlp">here</a>. You don&#8217;t need to have a phylogenetic tree file yourself. I supply an example inside.
         </li>
<li>This makes use of JGraph and Archaeopteryx (the PhyloXML parser)
</li>
</ol>
<p>I do maintain this code on github. I have <a href="http://github.com/tiagoantao/treeHugger">one project for the viewer</a> and <a href="http://github.com/tiagoantao/cljUtils">another for general utilities</a>. All the code is still very crude, but you might be interesting in stealing some of the <a href="http://github.com/tiagoantao/cljUtils/blob/master/src/org/tiago/swing.clj">swing code</a>, either as a crude example of how to interact with swing or taking my <a href="http://tiago.org/cc/2009/10/27/swing-menus-in-clojure/">micro-DSL for menus</a>. If you want to interact with JGraph, <a href="http://github.com/tiagoantao/cljUtils/blob/master/src/org/tiago/graph_vis.clj">this</a> might be a starting point. I don&#8217;t want, in any way, suggest that this code is any good. </p>
<p>Some lessons that I&#8217;ve learned and that I would like to share:</p>
<ol>
<li>Some of the clojure.contrib code is a bit green. I tried to use the graph library, but it is very small and specific. I ended up starting doing my own. Mine is even smaller and specific, no claims of generality.</li>
<li>I don&#8217;t appreciate some of the core functions of Clojure (I&#8217;ve written on this before and will write more in the near future). The great thing about Clojure is that you can import only what you want from the core and extend it yourself. I intend to do just that for my personal use. This is a PLUS point for Clojure: the flexibility that is made available to change many of the decisions of the language implementor (in the great tradition of declarative and homoiconic languages)
  </li>
<li>While I can change the core for my uses, I think defnk should really be core for everybody! I fact I wander if defn should not become defnk&#8230;</li>
<li>I am pretty sure that when *warn-on-reflection* is activated and action taken to correct the warnings, lots of code will increase in performance. With the more important side effect of annotating the code with type info.</li>
<li>I have quite a lot of recursive code that doesn&#8217;t use recur. Something to learn and master&#8230;</li>
<li>JGraph layout algorithms are not fantastic. I&#8217;ve tried with much bigger trees and the result was far from perfect (I also noticed performance problems in my own code).</li>
</ol>
<p>The biggest hurdle that I&#8217;ve found was the construction of user interfaces and how verbose Clojure Java interop can become. Of course one can create functions (and that was done) to create buttons, frames, menus, etc. But the creation of Java container structures (think frame contains menubar which contains menu with menus inside and so on) would benefit from a dialect where, when a certain (container) object was created it&#8217;s (Java) namespace would become easily available.</p>
<p>Imagine constructing a Structure like this:</p>
<pre>
MenuBar[
    Menu(File) [
        Menu(New)
        Menu(Close)
        Separator
        Menu
    ]
    Menu(Edit) [
        Menu(Cut)
        Menu(Paste)
    ]
]
</pre>
<p>it would be nice to be able to write something like:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new JMenuBar<span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;File&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;New&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Close&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>addSeparator<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Edit&quot;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Cut&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>add <span style="color: #66cc66;">&#40;</span>new JMenu <span style="color: #ff0000;">&quot;Paste&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>&#8220;add&#8221; and &#8220;addSeparator&#8221; are Java methods. All this would be dynamic against the Java object hierarchy (not a hand-written library!). Note that there is no doto special form (or variants) and, most importantly, note that, given a list (a b c d), if a is a Java object b c d are evaluated as methods of a. If b is (i (y x s)), x and s would be evaluated as methods of y, if they failed then as methods of a, if this fails interpreted as normal Clojure. Something like this (rough sketch).<br />
This would be useful, e.g., to construct Swing hierarchies by hand in a expedient way (not suggesting anything more, especially not to do big programs with outside scope).<br />
I am going to try to write some code that does this in the next few days.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/Ks2VOeaWiYc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/16/a-phylogenetic-tree-viewer-in-clojure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The 99 Prolog problems in Clojure</title>
		<link>http://tiago.org/cc/2009/11/11/the-99-prolog-problems-in-clojure/</link>
		<comments>http://tiago.org/cc/2009/11/11/the-99-prolog-problems-in-clojure/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 12:33:45 +0000</pubDate>
		<dc:creator>tiago</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[prolog]]></category>

		<guid isPermaLink="false">http://tiago.org/cc/?p=167</guid>
		<description><![CDATA[Here can be found an interesting effort to implement the 99 Prolog problems in Clojure. It is not clear to me that the exercise is conducted in the same way as the original one [Update: the author actually says this in the preamble]. Let me explain: The original Prolog problems are solved without the help [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://m.3wa.com/?p=426">Here</a> can be found an interesting effort to implement the <a href="https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/">99 Prolog problems</a> in Clojure.</p>
<p>It is not clear to me that the exercise is conducted in the same way as the original one [Update: the author actually says this in the preamble]. Let me explain:</p>
<p>The original Prolog problems are solved without the help of the (existing) Prolog libraries, just using the basic language mechanisms. They are a good at illustrating the underlying declarative power of Prolog.</p>
<p>For instance, problem 1, finding the last element of a list is solved with this in Prolog:</p>

<div class="wp_syntax"><div class="code"><pre class="prolog" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">% P01 (*): Find the last element of a list</span>
&nbsp;
<span style="color: #666666; font-style: italic;">% my_last(X,L) :- X is the last element of the list L</span>
<span style="color: #666666; font-style: italic;">%    (element,list) (?,?)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">% Note: last(?Elem, ?List) is predefined</span>
&nbsp;
my<span style="color: #008080;">_last</span><span style="color: #009900;">&#40;</span><span style="color: #008080;">X</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #008080;">X</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
my<span style="color: #008080;">_last</span><span style="color: #009900;">&#40;</span><span style="color: #008080;">X</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #008080;">_</span><span style="color: #339933;">|</span><span style="color: #008080;">L</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:-</span> my<span style="color: #008080;">_last</span><span style="color: #009900;">&#40;</span><span style="color: #008080;">X</span><span style="color: #339933;">,</span><span style="color: #008080;">L</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span></pre></div></div>

<p>Notice the in-code comment that &#8220;last is predefined&#8221;. In fact, using the Prolog library this could be done with a one liner:</p>

<div class="wp_syntax"><div class="code"><pre class="prolog" style="font-family:monospace;"><span style="color: #339933;">?-</span> last<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #800080;">1</span><span style="color: #339933;">,</span><span style="color: #800080;">2</span><span style="color: #339933;">,</span><span style="color: #800080;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #008080;">E</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>
<span style="color: #008080;">E</span> <span style="color: #339933;">=</span> <span style="color: #800080;">3.</span></pre></div></div>

<p>The offered solution in Clojure is also a one liner:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">last</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">8</span></pre></div></div>

<p>Given a sufficiently large and clever library (and Clojure has a very nice library) all problems on the list could be solved with a one-liner.</p>
<p>In my opinion, an apples-to-apples comparison with the original solutions would not use the core library.<br />
It would probably be like this for the same problem:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn mylast <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>mynext <span style="color: #66cc66;">&#40;</span>next <span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span>? mynext<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>first <span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>mylast mynext<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Yep, next is in the core library also, but being a call to clojure.lang.RT, I think it is fair game to use it.</p>
<p>Ok, better yet, with recur, as it is on core (this is essentially a copy of the core version):</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn mylast <span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>next s<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>recur <span style="color: #66cc66;">&#40;</span>next s<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>first s<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The Prolog exercise exposes the declarativeness and expressive power of Prolog. The Clojure example exposes mostly the cleverness of the core library.</p>
<p>Both are interesting points of view (I am not criticizing the Clojure solution), but they cannot be used for comparison purposes.</p>
<img src="http://feeds.feedburner.com/~r/CognitiveConsonance/~4/VtxyZ06DXMs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://tiago.org/cc/2009/11/11/the-99-prolog-problems-in-clojure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
