<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Code-itch &#187; python-tips</title>
	<atom:link href="http://www.code-itch.com/blog/tag/python-tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.code-itch.com/blog</link>
	<description>A non-coders attempts at writing useful code</description>
	<lastBuildDate>Sat, 20 Mar 2010 20:13:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Command line handling in Python with optparse</title>
		<link>http://www.code-itch.com/blog/2009/04/command-line-handling-in-python-with-optparse/</link>
		<comments>http://www.code-itch.com/blog/2009/04/command-line-handling-in-python-with-optparse/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 10:49:28 +0000</pubDate>
		<dc:creator>harijay</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[python-tips]]></category>

		<guid isPermaLink="false">http://www.code-itch.com/blog/?p=114</guid>
		<description><![CDATA[Command lines interfaces to programs are very empowering. I started using computers with the  Linux command line and  I have never strayed too far from programs that are predominantly command line based. Whether it was rasmol , povray , phenix or ffmpeg I always found that the command line gave the program a more  transparent [...]]]></description>
			<content:encoded><![CDATA[<p>Command lines interfaces to programs are very empowering. I started using computers with the  Linux command line and  I have never strayed too far from programs that are predominantly command line based. Whether it was rasmol ,<a href="http://www.povray.org/documentation/view/3.6.1/216/"> povray</a> , <a href="http://phenix-online.org/documentation/refinement.htm#anch46">phenix</a> or <a href="http://ffmpeg.mplayerhq.hu/ffmpeg-doc.html">ffmpeg</a> I always found that the command line gave the program a more  transparent interface . By that I mean it was easier ( at least for me ) to figure out how to do decipher a  manual page  than to go looking for a particular functionality in a GUI window.</p>
<p>No in the case of python ,  I have been writing scripts that take in user input from the command line for a while now. In these scripts , I would generally accept only one input and that would be the first in the sys.argv list. If I had more than one input I would iterate over the input list and try and figure out what the inputs were . Even worse in most cases I would  hard code the order  of inputs into my code ( terrible practise). Fortunately  for me , my discovery of the optparse module has changed all that.</p>
<p>The optparse module is  an object oriented ( dont let that scare you) and super-intuitive way to add command line options to any python scriptSo say you want to add an input file command line switch with the -i attribute , All you have to do is</p>
<p>from optparse import OptionParser</p>
<p>optparse_object =OptionParser()</p>
<p>optparser_object.add_option(&#8220;-i&#8221;,&#8221;&#8211;infile&#8221;, dest=&#8221;infile&#8221;,help=&#8221;input file for script&#8221; , metavar=&#8221;[infile.txt]&#8220;)</p>
<p>Once you do this you can easily have the module parse the sys.argv list and make sense of it .So you would add the following line</p>
<p>options_object, spillover_options = optparser_object.parse_args()</p>
<p>Then options_object.infile  will have the value of the input option . This is specified by the dest section in the add_option argument list) . The nice thing with the module is that all possibilities can be mapped to the same options_object.infile destination . So for eg I have mapped &#8220;-i&#8221; and &#8220;&#8211;infile&#8221; to the same destination .Even better is the option to add a help string with the help=&#8221;help text&#8221; argument . This help is then printed out if the user provides an option that the script cannot handle or if the code specifically calls  the optparser_object.print_help() function.</p>
<p>For a concrete example on how to use the opt_parse module consult the <a href="http://docs.python.org/library/optparse.html">docs</a> or <a href="http://github.com/harijay/xtaltools/blob/369a0d14710edecffd55b5707778b82690d50f26/mtztophs.py">my example code on github </a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.code-itch.com/blog/2009/04/command-line-handling-in-python-with-optparse/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python command line tricks for reverse primer design</title>
		<link>http://www.code-itch.com/blog/2008/10/quickie-dickie-python-command-line-for-reverse-primer-design/</link>
		<comments>http://www.code-itch.com/blog/2008/10/quickie-dickie-python-command-line-for-reverse-primer-design/#comments</comments>
		<pubDate>Tue, 07 Oct 2008 16:28:45 +0000</pubDate>
		<dc:creator>harijay</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python string reversal]]></category>
		<category><![CDATA[python-tips]]></category>

		<guid isPermaLink="false">http://www.code-itch.com/blog/?p=50</guid>
		<description><![CDATA[If I were to list one of the most attractive things about python , the interactive command line would rank among the top few. Well today I was forced with a rather simple task. I had to order a primer for a sub-cloning experiment that was the reverse primer corresponding to the last twenty bases of a [...]]]></description>
			<content:encoded><![CDATA[<p>If I were to list one of the most attractive things about python , the interactive command line would rank among the top few.</p>
<p>Well today I was forced with a rather simple task. I had to order a primer for a sub-cloning experiment that was the reverse primer corresponding to the last twenty bases of a given protein coding gene sequence.</p>
<p>Here is how python made the task super easy</p>
<p>1) First I opened the pubmed page with my sequence and copied the forward strand to my interactive python session</p>
<p>2) Then I wrote a couple of  lines to generate the reverse complement at the same time reversing the order of the string</p>
<address><span style="color: #339966;">&gt;&gt;&gt; x="GGATGAAGTCCAGATA"<br />
&gt;&gt;&gt; c={"A":"T","T":"A","G":"C","C":"G"}<br />
&gt;&gt;&gt; p=""<br />
&gt;&gt;&gt; for i in x :<br />
...  p = c[i] + p<br />
...<br />
&gt;&gt;&gt; p<br />
'TATCTGGACTTCATCC'</span></address>
<p>So in the above example the</p>
<address><span style="color: #008000;">c = </span><span style="color: #008000;">{"A":"T","T":"A","G":"C","C":"G"}</span></address>
<p>Is the reverse complement dictionary. The for loop replaces each nucleotide with its complement and simultaneously reverses the order , because every nucleotide is added to the begining ( i.e prepended) . So the last nucleotide ends up first.</p>
<p>The other way to do the same thing would be to use python string reversal as demonstrated in the code below</p>
<address><span style="color: #008000;">&gt;&gt;&gt; x="GGATGAAGTCCAGATA"<br />
&gt;&gt;&gt; c={"A":"T","T":"A","G":"C","C":"G"}<br />
&gt;&gt;&gt; for i in x :<br />
...  p = p + c[i]<br />
...<br />
&gt;&gt;&gt; p<br />
'CCTACTTCAGGTCTAT'<br />
&gt;&gt;&gt; reverse_5prime_3prime = p[::-1]<br />
&gt;&gt;&gt; reverse_5prime_3prime<br />
'TATCTGGACTTCATCC'</span></address>
<p>The cool part here is the funky and admittedly unreadable string-reversal syntax,</p>
<div class="igBar"><span id="lpython-2"><a href="#" onclick="javascript:showPlainTxt('python-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-2">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">reverse_5prime_3prime = p<span style="color: black;">&#91;</span>::-<span style="color: #ff4500;color:#800000;">1</span><span style="color: black;">&#93;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>So  In python string reversal is powerful. The p[::-1] basically says , give me the string from start to finish in backwards order (-1) . This elegant <a href="http://www.python.org/doc/2.3.5/whatsnew/section-slices.html">string reversal in python</a> is explained well <a href="http://www.answermysearches.com/index.php/super-easy-way-to-reverse-a-string-in-python/188/">in this post </a> and gives us the primer sequence in the correct 5-prime to 3-prime order as required by the oligo-synthesizing order forms.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.code-itch.com/blog/2008/10/quickie-dickie-python-command-line-for-reverse-primer-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
