Tuesday, March 29, 2011

Adding PHP module to default OS 10.6.1 PHP stack

The current system is Snow Leopard 10.6.1 and I want to add PostgreSQL support to the default PHP installation.  Snow Leopard comes with PHP 5.3.4 already installed in Apple's weird, distributed way.  However, the current distro for PHP is 5.3.6 at the time of this writing, so what to do?  I found the solution scattered across many different blogs, so I am synthesizing it here.  None of this was my own creation.

First, grab a copy of the source code that matches what is already installed.  Probably won't find it on PHP.net, so try this link: php-5.3.4

I created a /src directory to store source code in.  Copy the tar file into here or a similar directory and unpack it.

Change to that directory:


>cd /src/php-5.3.4

Set some environment variables before doing the configuration

>export MACOSX_DEPLOYMENT_TARGET=10.6.7
>export CFLAGS="-arch x86_64"
>export CXXFLAGS="-arch x86_64"
>export LDFLAGS="-arch x86_64"

Go to the pgsql source directory in php ext folder

>cd ext/pgsql

Compile the extension module

>phpize
>./configure
>make

The extension will be found here

>cd /src/php-5.3.4/ext/pgsql/.libs/
>ls
-rwxr-xr-x  1 Bali  admin   154K Mar 29 12:41 pgsql.so

Copy the extension to the extensions library and make sure it is executable

>sudo cp pgsql.so 
/usr/lib/php/extensions/no-debug-non-zts-20090626/
>cd  
/usr/lib/php/extensions/no-debug-non-zts-20090626/
>sudo chmod +x pgsql.so

Create a copy of the php.ini file if one does not already exist

>sudo cp /etc/php.ini.default /etc/php.ini

Edit the php.ini file and add the following two lines:

extension_dir="/usr/lib/php/extensions/no-debug-non-zts-20090626/"
extension=pgsql.so

Save and then test that the extension is loaded properly by running the following at the command line:

>php -m

You should see a list of installed modules, including pgsql.  Then go back and restart Apache

>/usr/sbin/apachectl graceful

Run phpinfo to verify the module has been loaded.  You may have to scroll down to see it.

That is it.

Friday, February 4, 2011

Installing libsvm-3.0 for Python on OSX 10.6

This was very frustrating to solve and I eventually had to get my friend Kieran (thanks Kieran!) to help me.  Basically the vanilla installation instructions that come with are insufficient (at least for me) to get a working module.  Here are the steps that were required to get everything to work.

1. Run 'make' in the libsvm-3.0 directory

2. Run 'make' in the libsvm-3.0/python directory

In the libsvm-3.0 directory there now should be a .so2 file

3. Create a new directory in your site-packages directory (your pythonpath) called libsvm

4. Copy the .so2 file from libsvm-3.0 and the svm.py, svm.pyc, svmutil.py files from libsvm-3.0/python to site-packages/libsvm

There are a couple things that are missing so now we need to make them.

5. In site-packages/libsvm create a file called __init__.py.  This is an empty file, but it is necessary to get the directory recognized as a python module.

6. Edit svm.py and add the following two lines after the other import statements at the top of the file:

          import os.path
    _PATH = os.path.join( *os.path.split(__file__)[:-1] )


7.  At around line 7 you will see this statement


          # For unix the prefix 'lib' is not considered.

    if find_library('svm'):
       libsvm = CDLL(find_library('svm'))
    elif find_library('libsvm'):
       libsvm = CDLL(find_library('libsvm'))
    else:
       if sys.platform == 'win32':
          libsvm = CDLL('../windows/libsvm.dll')
       else:
          libsvm = CDLL('../libsvm.so.2')

8.  Change this to look like this:


    # For unix the prefix 'lib' is not considered.
    if find_library('svm'):
  libsvm = CDLL(find_library('svm'))
    elif find_library('libsvm'):
  libsvm = CDLL(find_library('libsvm'))
    else:
  if sys.platform == 'win32':
  libsvm = CDLL(os.path.join(_PATH,'windows','libsvm.dll'))
  else:
  libsvm = CDLL(os.path.join(_PATH,'libsvm.so.2'))

9. Once you save svm.py, you should be able to fire up a python interpreter and do 'from libsvm import svm'.  If that works, and a dir(svm) shows you a ton of functions, then you are good to go.

Wednesday, January 19, 2011

Generate all possible proteins from ambiguous DNA

This had me stumped for awhile, but this works pretty well.  Does NOT handle stop codons or gap characters like '-'.  Requires BioPython


import itertools
from Bio.Seq import Seq
from Bio.Data import CodonTable
from Bio.Data import IUPACData</pre>
# Takes Bio.Seq.Seq object as input
# Returns list of all possible proteins
# Assumes sequence is in frame +1
def generateProtFromAmbiguousDNA(s):
   std_nt = CodonTable.unambiguous_dna_by_name["Standard"]
   nonstd = IUPACData.ambiguous_dna_values
   aa_trans = []
   for i in range(0,len(s),3):
      codon = s.tostring()[i:i+3]
      aa = CodonTable.list_possible_proteins(codon,std_nt.forward_table,nonstd) 
      aa_trans.append(aa)
   proteins = list(itertools.product(*aa_trans))
   possible_proteins = []
   for x in proteins:
      possible_proteins.append("".join(x))
   return possible_proteins
def main():
   a = Seq('ATGGCARTTGTAHAC')
   print "DNA: ",a.tostring()
   print "Proteins:"
   foo = generateProtFromAmbiguousDNA(a)
   for s in foo: print s
if __name__ == '__main__':
   main()

Creating a quick codon table

I didn't think this up, the code comes from Peter Collingridge here.  But it is rather elegant.


bases = ['t', 'c', 'a', 'g']
codons = [a+b+c for a in bases for b in bases for c in bases]
amino_acids = "F F L L S S S S Y Y stop stop C C stop W L L L L P P P P H H Q Q R R R R I I I M T T T T N N K K S S R R V V V V A A A A D D E E G G G G".split(' ')
codon_table = dict(zip(codons, amino_acids))

Thursday, January 13, 2011

Update the locate database on the Mac

This is the command for updating the locate database on the OSX system.

sudo /usr/libexec/locate.updatedb


I should figure out how to make this run everyday.

Wednesday, January 5, 2011

Connecting to PostgreSQL with Python and Psycopg2

Basic syntax for making a database connection, executing and retrieving data:



import psycopg2 as pg
# create database connection
try:
   conn = pg.connect("dbname='template1' user='dbuser' host='localhost' password='dbpass'")
except:
   print "Unable to connect to database"
# create database cursor
cur = conn.cursor()
# execute SQL and fetch results
cur.execute("""SELECT datname from pg_database""")
rows = cur.fetchall()
print "\nShow database results:\n"
for row in rows:
   print row[0]

Thursday, December 16, 2010

Density plots in R

To plot the distribution of scores in R using the density function, use

plot(density(dat$scores))
To overlay scores for another variable onto the first plot, use

lines(density(dat$other_scores), col="red")
Simple enough!