Showing posts with label postgresql. Show all posts
Showing posts with label postgresql. Show all posts

Postgres 8.3: Many ways to join a table

| Thursday, July 30, 2009

In BioSQL, PhyloDB module, there are tables like this:



This becomes a problem when I want to retrieve values that are related to one tree. For example, here are some data from the table

tree_idterm_idvaluerank
21"Wagner78"0
22"Single Tree"0
23"Wagner78"0
25"885"0
26"Ladiges, P. Y.; Humphries, C. J."0
27"A cladistic study of Arillastrum, Angophora, and Eucalyptus (Myrtaceae). "0
28"Transformed cladistic; character compatibility; branch and bound, and Farris-Wagner methods ...0


There is another table term that has term_id, and name. For example, the term (term_id=2) has the name 'dc.title'
I found a few ways to join the data and create a tabular format that each row belongs to one tree_id.

First, I can use what is called self join :



Here I used different table aliases for the same table, and each alias represents a different instance of the table.

Another way is to use FULL OUTER JOIN:


Interestingly, I also tried to use the PostgreSQL XML support to generate a piece of XML from that table. Here instead of using JOIN, I used UNION:



This will produce some thing like:



For a table with 5287 different tree_id, and a total of 42320 rows, the three method takes 1080ms, 1136ms, 1338ms (average of three tests). With no surprise that the last method takes longer as it puts the XML together.

Setting up BioSQL 1.0.1 on Postgresql 8.3, ubuntu 9.04

| Tuesday, July 21, 2009

BioSQL is a relational model that covers many kinds of data models in biology. The core schema has models for sequences, features, annotations, taxonomy (focused on NCBI taxonomy), and ontologies. It also has a PhyloDB extension module.
In this post, I'll record how I setup BioSQL on a Postgresql database on Ubuntu Jaunty (9.04)
Install PostgreSQL
There is a very good step-by-step instruction on how to install PostgreSQL on Ubuntu. Here is the link:
https://help.ubuntu.com/community/PostgreSQL
Even though it doesn't cover Ubuntu version after 8.04, and it says it's for PostgreSQL 8.1, I can still follow all the steps to install PostgreSQL 8.3 on Jaunty.
Install BioSQL
The BioSQL website has detailed installation instructions. But it is also very verbose, and the page has instructions for MySQL, PostgreSQL, and Oracle, even steps on how to install the databases. I wish there were separate pages for each database, though. Anyway, creating a BioSQL database on PostgreSQL is very straight forward. Here is what you all need to do:
> createdb biosql 
> psql biosql < biosqldb-pg.sql

To add the PhyloDB extension, first follow the instruction to check it out from SVN. Then do
> psql biosql < biosql-phylodb-pg.sql

Import NCBI Taxonomy into BioSQL
BioSQL has several scripts to import data into the database. Here I am just explaining how to import the NCBI taxonomy into the database.
First bioperl and bioperl-db need to be installed. Again, these steps are included in the BioSQL installation page. However, it's much easier to do it the Ubuntu way.
> sudo apt-get install bioperl

That'll install bioperl 1.5.2, with bioperl-db.
Then simply run (suppose you have already setup the PostgreSQL and you can run it with your username.
> load_ncbi_taxonomy.pl --diver Pg --download yes<br /><br />Update: 7/23/2009<br />Hilmar has pointed out that the bioperl in my post is only 1.5.2, and it seems in Karmic (9.10), ubuntu will have BioPERL 1.6.0 available in universe.<br /> <br /><br /><br />


install postgresql on ubuntu

| Monday, July 13, 2009

It turns out that I have to create database myself. I decided to install postgresql on my ubuntu server, and dump treebase data into it.

To install postgresql 8.3 on Ubuntu 9.04, basically I followed the steps in this article. Although the author was addressing an earlier version of Ubuntu (8.04), I found the steps are still correct.

http://hocuspokus.net/2008/05/install-postgresql-on-ubuntu-804

This post is based on postgresql 8.1. It also shows how to make the current user a superuser.

https://help.ubuntu.com/community/PostgreSQL


I also installed pgadmin3 on another ubuntu box where I have a ubuntu desktop installed. I found that in order to access the server remotely, I have to do the following on the server:

1. Add port 5432 to the allowed list
2. Give user a password. Do something like
<code>sudo su postgres -c psql 
=# ALTER USER myusername WITH PASSWORD 'password';
=# \q
</code>