Saturday, August 20, 2011

Statistical Analysis Functions in R

Lately, I've been using statistical tests on a daily basis. I've noticed that I have to format my data the same way in order to get it into R (tab-delimited flat file essentially). Every other change in order to prep that data structure for any sort of statistical analysis function require minimal modification to the data structure.


Why not kill several birds with the same stone? I've just written a wrapper function around a few smaller, independent R scripts which perform statistical analysis tests.


Get the code here:


https://github.com/ngopal/Statistical-Analysis-Functions


The input to the wrapper is a single, tab-delimited file (with the first row being the header). The output is a few PDF files, each complete with plots for different statistical analysis tests.


The statistical analysis tests which are currently performed are:
  • Principal Component Analysis (PCA)
  • K-means Clustering
  • Hierarchical Clustering
There are a few R scripts in the currently released package which require the user to download external libraries. These R scripts are turned off by default.

I am considering adding more statistical tests to the package--perhaps a t-test with a few box-and-whisker plots.

Now I can run one script and output several plots in one slick shot.

Wednesday, August 10, 2011

Bioinformatics: Extra Tabs in Sam to Bam conversion

Sam files cannot have any trailing tabs at the end of each sam file entry. If you do, the sam-to-bam conversion (samtools view -Sb file.sam > file.bam) will not work.


This has actually been documented here: http://groups.google.com/group/bedtools-discuss/browse_thread/thread/b0535d6769540fe0?pli=1


I was able to get around this however, with a simple sed command:



sed 's/[ \t]*$//' file.sam > file.notrailingtabs.sam

Now, the sam-to-bam conversion will work:

samtools view -Sb file.notrailingtabs.sam > file.notrailingtabs.bam


Tuesday, August 9, 2011

The Scorpion and the Frog


An interesting story derived from eastern philosophy:

One day a scorpion was trapped on the shore of a river, and he needed to get to the other side. 


He asked a frog he saw swimming out in the water to come get him and take him across. The frog refused. 


“You are a scorpion,” the frog said. “If I take you on my back, you’ll sting me.” 


The scorpion said that was foolish, of course he wouldn’t sting him because then he would drown in the river and die, too. 


The frog thought about it, and agreed that it wouldn’t make sense. 


He let the scorpion on his back, and then started paddling across the river. When the frog was halfway out, the scorpion stung him. 


As the frog was dying and they both began to slip under the water, he asked the scorpion why he did it. 


“It’s my nature. I am a scorpion,” he said. “And scorpions sting.”



Tuesday, August 2, 2011

Show and Tell: "AppleScript" a Spotify Alarm Clock on Mac

I don't know about you guys, but I've really been enjoying Spotify. Being one of the first American users has been a real treat. Making the switch from iTunes wasn't painful at all. Sorry iTunes, sometimes the best things in life are free...


A few weeks ago, I used this applescript tutorial to setup iTunes to be my morning alarm clock. I love this. Nothing makes it easier to get out of bed than waking up to a song that energizes you.


I've found a way to use Spotify instead of iTunes in the alarm clock script, and this post is going to be a quick show and tell. It's not as easy as just replacing "iTunes" with "Spotify" in the iTunes alarm clock script.


I would suggest following the tutorial in the link I provided earlier, substituting the script provided for the one I provide in step 1 below. Otherwise, you can follow my attempt at a walkthrough.

  • Open up spotlight and open up "AppleScript Editor." Copy the code below into your editor and save it (with an easy-to-remember name). 
    • Edit out my username with yours
    • Edit out my playlist "muzic" with the name of your playlist
    • Press the Run button to make sure it actually works.
    • Update 8/13/2012: I've added the code in the image below to github as a gist.
  • Next, pop open your terminal window and type in "crontab -e" without the quotes
  • Now you will be in VI editor, so press "i" on your keyboard to "insert," and type in the following line (the one line at the bottom is all you need). Make sure the path points to the script you just made in step 1. Also note that the pound sign on the first line "comments out" the line, so that line of code is ignored.
  • After you are done typing in the code, press "esc" so that a colon show up in the bottom left corner of the screen. Then type in "wq!" to save and quit the editor.
    • As a note, my alarm confguration (shared in the image above) is set to wake me up at 6:01AM. I would look here to find out how to edit the cron job to awaken you at a time of your own choosing.
  • Next, you have to make sure your computer is on maybe 5 minutes before your alarm goes off. This can be done in the "System Preferences" application. 
  • Click on the "Energy Saver" option
  • You don't have to mess with your sleep and display configuration, but you do need to click the "Schedule..." button on the bottom right hand corner
  • Set your mac to wake up or start up 5 minutes before your cron job kicks off. You're all done!

Congratulations, you've just setup your own Spotify alarm clock!