{"id":546,"date":"2017-10-27T18:21:09","date_gmt":"2017-10-27T16:21:09","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/?p=546"},"modified":"2017-10-27T18:29:53","modified_gmt":"2017-10-27T16:29:53","slug":"alignment-phy2nex-few-liner","status":"publish","type":"post","link":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/2017\/10\/27\/alignment-phy2nex-few-liner\/","title":{"rendered":"Alignment Phy2Nex few-liner"},"content":{"rendered":"<p><strong>Alignment file format conversion for the efficient &#8211; Part II<\/strong><\/p>\n<p>Today, I needed to convert a series of alignments, which were stored in the <em>phylip<\/em> format, into the common <em>nexus<\/em> format. The output DNA alignment hereby needed to be of sequential format (i.e., non-interleaved).<\/p>\n<p>In February 2017, I had already written <a href=\"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/2017\/02\/08\/alignment-nex2phy-few-liner\/\" target=\"_blank\" rel=\"noopener\">a few-liner<\/a> to conduct the inverse conversion (<em>nexus<\/em> to <em>phylip<\/em>) and was, thus, surprised to find that the conversion from <em>phylip<\/em> to non-interleaved <em>nexus<\/em> did not work out of the box. Instead, a few more lines (and a little trick using <em>StringIO()<\/em>) were necessary to get this specific conversion to work.<\/p>\n<div style=\"background-color: #ffebdb\">\n<p style=\"padding-left: 30px\"><code>#!\/usr\/bin\/env python2.7<br \/>\n<code> <\/code><br \/>\nimport os<br \/>\nimport sys<br \/>\nfrom Bio import AlignIO<br \/>\nfrom Bio.Alphabet import IUPAC, Gapped<br \/>\nfrom Bio.Nexus import Nexus<br \/>\nfrom StringIO import StringIO<br \/>\n<code> <\/code><br \/>\ninFn = sys.argv[1]<br \/>\noutFn= os.path.splitext(inFn)[0]+\".nex\"<br \/>\n<code> <\/code><br \/>\ninp = open(inFn, 'rU')<br \/>\noutp = open(outFn, 'w')<br \/>\n<code> <\/code><br \/>\nalphabet = Gapped(IUPAC.ambiguous_dna)<br \/>\naln = AlignIO.parse(inp, 'phylip-relaxed', alphabet=alphabet)<br \/>\n<code> <\/code><br \/>\nout_handle = StringIO()<br \/>\nAlignIO.write(aln, out_handle, 'nexus')<br \/>\n<code> <\/code><br \/>\np = Nexus.Nexus()<br \/>\np.read(out_handle.getvalue())<br \/>\np.write_nexus_data(outp, interleave=False)<br \/>\n<code> <\/code><br \/>\noutp.close()<br \/>\ninp.close()<br \/>\n<\/code><\/p>\n<\/div>\n<p>And for those who wish to apply the above Python code (saved as &#8220;<em>phy2nex.py<\/em>&#8220;) to a collection of directories which contain a phylip-file each:<\/p>\n<div style=\"background-color: #ffebdb\">\n<p style=\"padding-left: 30px\"><code>for dir in *\/; do python2 phy2nex.py $dir*.phy; done<\/code><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Alignment file format conversion for the efficient &#8211; Part II Today, I needed to convert a series of alignments, which were stored in the phylip format, into the common nexus format. The output DNA alignment hereby needed to be of sequential format (i.e., non-interleaved). In February 2017, I had already written a few-liner to conduct [&hellip;]<\/p>\n","protected":false},"author":2306,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57598,57600],"tags":[],"class_list":["post-546","post","type-post","status-publish","format-standard","hentry","category-bioinformatics","category-one-liners"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/users\/2306"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/comments?post=546"}],"version-history":[{"count":6,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":552,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/posts\/546\/revisions\/552"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/gruenstaeudl\/wp-json\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}