January 21st, 2011 § § permalink
Simplify your bash PATH management with pathmunge. The pathmunge will add a directory to the beginning of your PATH if it is missing from your PATH. This means that you can safely .source ~/.bashrc without ending up with duplicate folders in your path (which you would get if you had used export PATH=/add/this/dir:$PATH). It is also clearer what folders you are adding to your path. This goes in your ~/.bashrc:
pathmunge () {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
pathmunge /usr/local/blah/bin
pathmunge ~/bin
export PATH
# now PATH is ~/bin:/usr/local/blah/bin:<old path>
As a bonus, you can use pathmunge /dir/to/add after to append the directory to the end of your path instead of the beginning.
Code from TDLP Bash Beginners Guide
January 16th, 2011 § § permalink
It seems like a real object oriented language shouldn’t require “self” as the first argument to all its class methods. It reminds me of Matlab’s hackish inefficient classes. Down with the explicit self!
Not having to type semicolons at the end of lines is nice, but needing to type colons at the end of function definitions and if statements is not. I always forget it. And when I put it in, using the colon makes me feel like I’m only using the left curly brace; like something is missing.
class Moose
def __init__()
print('moo')
/rant
January 15th, 2011 § § permalink
A simple BED file reader in Python.
import csv
class CommentedFileReader:
"""
Helper class for file reading.
Skips lines starting with '#'
tsv_file = csv.reader(CommentedFileReader("inputfile.txt"),
delimiter='\t')
for row in tsv_file:
print row[2] # prints column 3 of each line
"""
def __init__(self, f, commentstring="#"):
self.f = open(f, 'rU')
self.commentstring = commentstring
def next(self):
line = self.f.next()
while line.startswith(self.commentstring):
line = self.f.next()
return line
def __iter__(self):
return self
csv.register_dialect('bed', delimiter = '\t',
quoting = csv.QUOTE_NONE,
skipinitialspace = True)
class BEDReader(csv.DictReader):
"""
Read BED files into a DictReader.
See BEDReader.FIELDS for field names
Example:
bed = BEDReader("file.bed")
for line in bed:
# print the chromStart
print(line['chromStart'])
"""
FIELDS = ('chrom', 'chromStart', 'chromEnd',
'name', 'score', 'strand',
'thickStart', 'thickEnd',
'itemRgb',
'blockCount', 'blockSizes', 'blockStarts')
def __init__(self, filename):
csv.DictReader.__init__(self, CommentedFileReader(filename), dialect='bed',
fieldnames=self.FIELDS)
if __name__ == "__main__":
bed = BEDReader("data.txt")
for line in bed:
print(line['chromStart'])
October 20th, 2010 § § permalink
I don’t like the Matlab plot axes where you are forced to have a box around your figure. By default, Python’s matplotlib plots like Matlab, but you can customize the axes to your liking.

Modified from the matplotlib spine demo
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(facecolor='white')
x = np.linspace(0,2*np.pi,100)
y = 2*np.sin(x)
ax = fig.add_subplot(1,1,1, aspect='equal')
ax.set_title(r'$y=\sin(t)$')
ax.plot(x,y)
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.autoscale_view(tight=True)
ax.set_ylim(-2.5,2.5)
ax.set_xlim((0,2*np.pi))
ax.set_xticks([0,np.pi,2*np.pi])
ax.set_xticklabels(['', r'$\pi$', r'$2\pi$'])
ax.text(2*np.pi + .1, -.2, r'$t$') # Manually adjusted
plt.savefig('plot.pdf')
plt.show()
July 18th, 2010 § § permalink
Some examples using the subprocess python module.
Make a system call three different ways:
#! /usr/bin/env python
import subprocess
# Use a sequence of args
return_code = subprocess.call(["echo", "hello sequence"])
# Set shell=true so we can use a simple string for the command
return_code = subprocess.call("echo hello string", shell=True)
# subprocess.call() is equivalent to using subprocess.Popen() and wait()
proc = subprocess.Popen("echo hello popen", shell=True)
return_code = proc.wait() # wait for process to finish so we can get the return code
Control stderr and stdout:
#! /usr/bin/env python
import subprocess
# Put stderr and stdout into pipes
proc = subprocess.Popen("echo hello stdout; echo hello stderr >&2", \
shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
return_code = proc.wait()
# Read from pipes
for line in proc.stdout:
print("stdout: " + line.rstrip())
for line in proc.stderr:
print("stderr: " + line.rstrip())
July 15th, 2010 § § permalink
ssh-copy-id moose@moosechips.com
If you get ERROR: No identities found, you need to create a ssh key. (Create one without a password when prompted.)
Then run ssh-copy-id again.
If your remote user has a different login name, you can setup a ssh config file so it will use the right user name. Create a text file at ~/.ssh/config with
Host moosechips.com
User moose
Now you can login via ssh
April 2nd, 2010 § § permalink
When trying to run Mathematica over SSH using Cygwin, you will probably get some error message like
ssh -Y user@host
mathematica
xset: bad font path element (#23), possible causes are:
Directory does not exist or has wrong permissions
Directory missing fonts.dir
Incorrect font server address or syntax
This is because you need to install the Mathematica fonts into Cygwin’s X server. This solution worked for me using the latest Cygwin 1.7.2: » Read the rest of this entry «
March 8th, 2010 § § permalink
Reach gitweb at a subdirectory, i.e. “http://moosechips.com/gitweb”. The gitweb README and INSTALL files come with instructions on setting up access to your root web directory, but chances are you’re already using that for your homepage. Here’s the setup for installing to a subdirectory. (This worked for me with git-1.7.0.1)
» Read the rest of this entry «
August 11th, 2009 § § permalink
Instructions:
- Get the latest build from the arrozcru autobuilds page
- Unzip the folder into C:/Program Files/ffmpeg
- Add C:/Program Files/ffmpeg/bin to your system’s PATH environment variable
Optional libx264 preset setup:
If you use libx264 presets (by using the -vpre flag) you need to do the following setup.
- Create a HOME environment variable for your user pointing to your home directory. (e.g. for Vista C:/Users/moose or for XP C:/Documents and Settings/moose )
- Create a .ffmpeg folder in your home directory
- Copy the preset files from C:/Program Files/ffmpeg/share/*.ffpreset into %HOME%/.ffmpeg
- Now you can open a command prompt and use ffmpeg. :D (e.g. This is my Vimeo video conversion command. ffmpeg -i input.mov -vcodec libx264 -vpre hq -crf 24 -g 25 -acodec libmp3lame -ab 192k -ar 44100 output.mp4 )
*note: libfaac is not included in the build since libfaac is considered to be a non-free plugin. :(
May 12th, 2009 § § permalink
Thanks to Mozilla for providing excellent linux font DPI support.
On my Gentoo setup, I added a Xft.dpi setting go my ~/.Xdefaults file. Now my fonts look normal again. :)
Before:

After:

May 10th, 2009 § § permalink
Instructions for installing xorg-server and hal on gentoo.
I’m writing this because the gentoo documentation for X setup is very outdated. Gentoo now uses xorg 1.5 which uses hal to setup devices automatically instead of having to configure /etc/X11/xorg.conf. The Xorg 1.5 update guide might be a little helpful, but it’s missing some info and is a bit vague in places.
Xorg-server 1.5 setup
Make sure your kernel is compiled with Event interface support. (If not, see Gentoo Handbook: Configuring the kernel for help recompiling your kernel)
Device Drivers --->
Input device support --->
--- Input device support
[*] Event interface
Add X and hal USE flags (the “–alpha-order” is optional, but I like to keep my USE flags organized)
flagedit --alpha-order +hal +X
(or equivalently with euse. euse is from the app-portage/gentoolkit package)
Add to /etc/make.conf
INPUT_DEVICES="evdev"
# Change the video driver to match your video card
# or install a bunch and let hal choose
VIDEO_DRIVER="nv nvidia radeon radeonhd intel sis via vesa fbdev"
Install xorg-server (which will also install hal since the hal USE flag is now enabled)
Add hal policy files so hal can auto-detect your devices. Doesn’t hurt to add them all, hal will figure out what it needs.
cp /usr/share/hal/fdi/policy/10osvendor/* /etc/hal/fdi/policy
Add hald to startup and start hald
rc-config add hald default
/etc/init.d/hald start
Don’t need any /etc/X11/xorg.conf file
mv /etc/X11/xorg.conf /etc/X11/xorg.conf.bak
Now startx
May 10th, 2009 § § permalink
I recently configured the wireless on my X61 Tablet for Gentoo. I have a Intel 4965 AGN wireless card.
lspci | grep -i wireless
03:00.0 Network Controller: Intel Corporation PRO/Wireless 4965 AG or AGN Network Connection (rev 61)
Relevant kernel options (from gentoo-sources 2.6.28-r5 kernel)
Device Drivers --->
[*] Network device support --->
Wireless LAN --->
{*} Intel Wireless Wifi Core
<M> Intel Wireless WiFi Next Gen AGN
[*] Intel Wireless WiFi 4965AGN
Install wpa_supplicant and wireless-tools
emerge -av wpa_supplicant wireless-tools
Create symlink so Gentoo will initialize the wlan0 at startup
cd /etc/init.d
ln -s net.lo net.wlan0
/etc/conf.d/net
modules=( "wpa_supplicant" )
iwconfig_wlan0="mode managed"
wpa_supplicant_wlan0="-Dwext"
/etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=wheel
network={
ssid="MySSID"
scan_ssid=1
key_mgmt=WPA-PSK
psk="password"
priority=5
}
I was getting SCICSIFFLAG File Not Found errors when trying to bring up my wlan0 interface. A look at the dmesg log revealed that the iwlagn module was trying to access iwlwifi-4965-2.ucode, so I emerged iwl4965-ucode to fix that.
Reinitialize the interface to see if everything worked
/etc/init.d/net.wlan0 restart
May 7th, 2009 § § permalink
Creating a bootable usb stick with the Gparted live iso in Ubuntu. Gparted is a graphical partition editor which can resize, move, copy, create, delete your hard drive partitions.
Overview:
- Format your usb stick to Fat16 and make it bootable using gparted.
- Install the gparted-live*.iso using unetbootin. (unetbootin also automatically installs the syslinux bootloader)
- Reinstall syslinux onto usb stick. (Gparted’s live cd requires syslinux newer than ver 3.71. Currently (May 7 2009) Ubuntu 9.04 Jaunty has syslinux 3.63. Use
apt-cache show syslinux | grep "^Filename" to check Ubuntu’s version)
Installation Steps
1. Install gparted and unetbootin
sudo aptitude install gparted unetbootin
2. Download the gparted live iso and the latest syslinux.
wget http://superb-west.dl.sourceforge.net/sourceforge/gparted/gparted-live-0.4.4-1.iso
wget http://www.kernel.org/pub/linux/utils/boot/syslinux/syslinux-3.80.tar.bz2
3. Insert your usb stick into the computer, and copy any files you want to save to your hard drive. You will be reformatting your stick and will lose all the files on the usb drive.
4. Run gparted.
Select your usb drive. (Probably something like /dev/sdc). Right click on the partition and select Unmount to unmount the drive. Format the drive to Fat16 and apply. Then make the usb drive bootable by going to Manage Flags and selecting the boot flag.

Exit gparted.
5. Remount your usb drive by unplugging and replugging your usb stick.
6. Run unetbootin
Select Diskimage ISO and locate the gparted-*.iso that you downloaded. Make sure the correct partition is selected at the bottom and click OK.

Don’t reboot yet. Just exit unetbootin.
7. Now, you would be done, except that Ubuntu’s syslinux is version 3.63 and the gparted live iso requires at least syslinux version 3.71, so you need to reinstall syslinux onto your usb stick.
Unpack the syslinux tarball you downloaded.
tar xjvf syslinux-3.80.tar.bz2
Now install syslinux onto your usb stick
cd syslinux-3.80/linux
sudo ./syslinux -s /dev/sdc1
8. Now you’re done. Reboot using your new gparted bootable usb stick.
End Notes:
This method should work on other bootable isos too.
You could try to use Fat32 if your motherboard supports booting from Fat32 USB devices. Fat16 is generally more compatible though.
If your USB stick is bigger than the the allowable Fat16 size (2gb), make two partitions: first a 1-2gb Fat16 partition for your boot drive, and a second Fat32 partition with the rest of the space.
April 17th, 2009 § § permalink
Add these lines to your ~/.bashrc to make colorful grep | less output.
alias grep='grep --color=always'
export LESS='-R'
April 10th, 2009 § § permalink
Often the unix commands basename and dirname are useful in shell scripts, but how do you use these in c or c++?.
The C way: libgen.h
If you’re working in c, the posix header libgen.h provides dirname and basename functions, with some limitations: They only work with char *, and dirname will modify the char * you pass into it so you need to make a copy of your original path if you wish to keep it. Example code:
// libgen_example.c
// Demonstrate libgen.h usage and pitfalls
// Compile: gcc -g -o libgen_example libgen_example.c
#include
<libgen.h> //basename and dirname
#include <string.h> //strlen and strcpy
#include <stdio.h> //printf
#include <stdlib.h> //malloc
int main(int argc, char* argv[]) {
// Demonstrate how dirname changes the path you input to it
char path[] = "default/path/to/nowhere.txt";
printf("path='%s'\n", path);
char * dir = dirname(path);
printf("path='%s' dir='%s'\n", path, dir);
char * base = basename(path);
printf("path='%s' dir='%s' base='%s'\n\n",
path, dir, base);
// Since dirname changed path, base is now "to"
// instead of "nowhere.txt" as we might have expected.
// Output:
// path='default/path/to/nowhere.txt'
// path='default/path/to' dir='default/path/to'
// path='default/path/to' dir='default/path/to' base='to'
//========
// Demonstrate c string copying
const char * another_path = "another/path";
char * modifiable_copy = (char *)malloc(strlen(another_path) + 1);
strcpy(modifiable_copy, another_path);
char * another_dir = dirname(modifiable_copy);
printf("another_path='%s'\n", another_path);
printf("modifiable_copy='%s'\n", modifiable_copy);
printf("another_dir='%s'\n\n", another_dir);
// note that after this free, another_dir is now also
// invalid since it pointed to the modifiable_copy's memory
free(modifiable_copy);
// Output:
// another_path='another/path'
// modifiable_copy='another'
// another_dir='another'
//========
// const strings declared as char * can cause segfaults
char * char_pointer_path = "char/pointer/path";
printf("char_pointer_path='%s'\n", char_pointer_path);
char * char_pointer_dir = dirname(char_pointer_path);
printf("char_pointer_path='%s' char_pointer_dir='%s'\n",
char_pointer_path, char_pointer_dir);
// Output:
// char_pointer_path='char/pointer/path'
// Segmentation fault
}
The C++ way: boost::filesystem
If you’re looking for a better c++ solution, see the c++ boost filesystem library. boost::filesystem::path.parent_path() can be used like dirname and boost::filesystem::path.filename() acts like basename.
The boost::filesystem library also has many other useful functions for dealing with files and paths (e.g. fs::complete(path) for obtaining an absolute path, fs::exists(path) will check if the path exists, fs::create_directories(path), fs::remove(path), fs::copy_file(from,to) name a few). Note that some of these are functions are boost::filesystem::path methods and some are in boost::filesystem. According to the faq, operations done by the operating system are provided in boost::filesystem functions, but functions just performed on the lexical path (just path string manipulations) are provided as boost::filesystem::path methods.
// filesystem_example.cpp
// Demonstrate using boost::filesystem
// Compile: g++ -I /usr/local/include/boost-1_38/ -L /usr/local/lib -lboost_filesystem-gcc41-mt-1_38 -o filesystem_example filesystem_example.cpp
#include <boost/filesystem.hpp>
#include <iostream>
namespace fs = boost::filesystem;
int main(int argc, char* argv[])
{
fs::path pathname("default/path/to/nowhere.txt");
std::string dirname = pathname.parent_path().string();
std::string basename = pathname.filename();
std::cout << "pathname = " << pathname << std::endl;
std::cout << "dirname = " << dirname << std::endl;
std::cout << "basename = " << basename << std::endl;
// Output:
// pathname = default/path/to/nowhere.txt
// dirname = default/path/to
// basename = nowhere.txt
std::cout << std::endl;
std::cout << "extension = " << pathname.extension() << std::endl;
std::cout << "complete = " << fs::complete(pathname) << std::endl;
std::cout << "exists = " << ((fs::exists(pathname)) ? "true" : "false") << std::endl;
// Output:
// extension = .txt
// complete = /home/moose/default/path/to/nowhere.txt
// exists = false
}