Alignment Phy2Nex few-liner
Alignment file format conversion for the efficient – 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 the inverse conversion (nexus to phylip) and was, thus, surprised to find that the conversion from phylip to non-interleaved nexus did not work out of the box. Instead, a few more lines (and a little trick using StringIO()) were necessary to get this specific conversion to work.
#!/usr/bin/env python2.7
import os
import sys
from Bio import AlignIO
from Bio.Alphabet import IUPAC, Gapped
from Bio.Nexus import Nexus
from StringIO import StringIO
inFn = sys.argv[1]
outFn= os.path.splitext(inFn)[0]+".nex"
inp = open(inFn, 'rU')
outp = open(outFn, 'w')
alphabet = Gapped(IUPAC.ambiguous_dna)
aln = AlignIO.parse(inp, 'phylip-relaxed', alphabet=alphabet)
out_handle = StringIO()
AlignIO.write(aln, out_handle, 'nexus')
p = Nexus.Nexus()
p.read(out_handle.getvalue())
p.write_nexus_data(outp, interleave=False)
outp.close()
inp.close()
And for those who wish to apply the above Python code (saved as “phy2nex.py“) to a collection of directories which contain a phylip-file each:
for dir in */; do python2 phy2nex.py $dir*.phy; done
Standardizing tRNA anticodon abbreviations in genome files
Better U than T (or the other way around)
Incongruence exists among the annotations of tRNAs in genomic files: Most authors use “U” (for uracil) in the three-letter anticodon abbreviations of tRNA anntotations, but some use “T” (for thymine). While I consider the usage of both letters as justified, I believe that this usage should be consistent within a single genome file.
To assist with such a standardization, I have written a small Python script (directly executable from the bash shell) that replaces all “U”s of an anticodon abbreviation with “T”s (or vice versa, if so desired). Simply change the file name from “tmp” to the actual filename in the below script.
python - <<EOF
import re
with open("tmp", "r+") as f:
lines = f.read().splitlines()
outL = []
kw = "\ttrn" # Depending on file format, keyword can also be "=trn" (e.g., gff-files)
for l in lines:
if kw in l:
pos = [m.end()+2 for m in re.finditer(kw, l)]
for p in pos:
l = l[:p] + l[p:p+3].replace("U","T") + l[p+3:] # Replacement from U to T; switch letters if desired
outL.append(l)
else:
outL.append(l)
f.seek(0)
f.write('\n'.join(outL))
f.truncate()
EOF
Note:
P.S. Is indenting in HTML supposed to be that clumsy, with using different <p> </p>
definitions for every indent level?
P.P.S. Indenting works nicely when using the tag <pre> </pre>
, as the display mode seems to switch into verbatim. A big thank you to Chris B. for pointing this out to me.
Teaching award 2017
Awarded annually to a lecturer in biology, chemistry and pharmacy
I recently received the teaching award for biology at the FU Berlin for 2017. Many thanks to those students who recommended me. I appreciate your praise!
New paper – Conserved plastid genome structure in early-diverging angiosperms
Not so different after all.
Together with two other scientists, I just published a new paper on the plastid genome structure of early-diverging angiosperms. We demonstrate that the plastid genomes of early-diverging angiosperms are much more conserved than previously considered.
Teaching in spring
In the lab with teachers-to-be and high-school students
One of the master-level courses I teach at the Freie Universität Berlin during spring semester, the NatLab Evolution, is geared towards a comprehensive teaching education for upcoming high-school teachers. During this course, we invite high-school students from across Berlin to spend a day in the lab and work with the Master’s students on biological experiments. Given the general responses, both sides seem to enjoy this arrangement a lot!
Ordering charsets within NEXUS files
Character sets for the orderly.
Defining character sets (charset) in NEXUS files can be an efficient way to annotated specific regions of a DNA or protein alignment. However, many software packages able to write NEXUS files (e.g., BioPython) do not save charsets in an ordered fashion, if multiple ones are present (i.e., that charset at alignment positions 100-250 were listed before charset at alignment positions 350-550). Instead, a typical NEXUS file written by Biopython looks something like this:
#NEXUS
BEGIN DATA;
DIMENSIONS NTAX=3 NCHAR=12;
FORMAT DATATYPE=DNA GAP=- MISSING=?;
MATRIX
seq1 ATGACAATATAA
seq2 ATGACTGTATAA
seq3 ATGATTGTCTAA;
END;
BEGIN SETS;
CharSet foo = 5-7;
CharSet bar = 3-8;
CharSet baz = 2-6;
END;
The following lines of bash code order the charsets of the NEXUS by their start position.
sed '/BEGIN SETS;/q' infile > outfile
awk '/BEGIN SETS;/,/END;/' | tail -n +2 | head -n -1 | sort -t' ' -n -k4 >> outfile
sed -n -e '/BEGIN SETS;/,$p' infile | sed -n -e '/END;/,$p' >> outfile
Teaching the teachers II
Teaching the teachers II
Like last year, I held training labs for current high school teachers as part of the NatLab initiative. It was a wonderful experience and great to find out what today’s high school teachers wish to communicate to their students.
Alignment Nex2Phy few-liner
Alignment file format conversion for the efficient
Today, I needed to convert a series of alignments, which were stored in the common nexus format, into newick format. In order to do this efficiently, I wrote the following few-liner.
#!/usr/bin/env python2.7
import sys
from Bio import AlignIO
inFn = sys.argv[1]
inp = open(inFn, 'rU')
outp = open(inFn+'.phy', 'w')
aln = AlignIO.parse(inp, 'nexus')
AlignIO.write(aln, outp, 'phylip')
outp.close()
inp.close()
The above-code is very ordinary and great to have handy.
Teaching freshmen
Currently, I am teaching a class on botany and global biodiversity to freshman-level students. Although the total number of students signed up for my course is large, and thus I have to guide dozens of students through the course materials every day, I love academic teaching and I hope that the students enjoy the course as much as I do.