This is taken straight from here. Content is copied in case the link dies. I needed to redo the key bindings for Sublime after I upgraded the OS to 10.8. So here it is:
To get your Home and End keys working properly on Mac OS X (in my case, Mountain Lion, although this should work in prior versions back to at least Tiger), simply open the Terminal and do this:
$ cd ~/Library
$ mkdir KeyBindings
$ cd KeyBindings
$ nano DefaultKeyBinding.dict
Put these lines in that file, including the curly braces:
{
/* Remap Home / End keys to be correct */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
"^\UF729" = "moveToBeginningOfDocument:"; /* Ctrl + Home */
"^\UF72B" = "moveToEndOfDocument:"; /* Ctrl + End */
"$^\UF729" = "moveToBeginningOfDocumentAndModifySelection:"; /* Shift + Ctrl + Home */
"$^\UF72B" = "moveToEndOfDocumentAndModifySelection:"; /* Shift + Ctrl + End */
}
Press Ctrl+O and then Enter to save the file, and Ctrl+X to exit. Restart your computer to have it take full effect.
code.ex(python)
Python, PostgreSQL, R, sundry computer geek stuff, etc.
Thursday, April 4, 2013
Wednesday, July 11, 2012
Finding values in one table that are not in another
This site has a very good explanation of NOT IN, LEFT JOIN,where NULL, and NOT EXISTS statements for Finding values in one table that are not in another
Labels:
postgres
Wednesday, May 2, 2012
Matching limited elements using Postgres IN clause
So you have two tables, one a sequence table holds unique sequence ids. The other table hold sequence mutations and has a 1 to many relationship with the sequence table. So for a given sequence id in the mutation table, there are many records, one for each mutation.
Now you have two sets of mutations you are interested in, a primary set A and a secondary set B. You want to know how many sequences contain 1 and only 1 mutation from set A and 1 and only 1 from set B.
select count(*) from (
SELECT distinct a.seq_id
FROM sequence a
JOIN seq_mutations b ON b.seq_id = a.seq_id
JOIN seq_mutations c ON c.seq_id = a.seq_id
WHERE b.mutation IN ('mutA','mutB','mutC')
AND c.mutation IN ('mut1', 'mut2', 'mut3', 'mut4', 'mut5')
GROUP BY a.seq_id
HAVING
count(distinct b.mutation) = 1
AND count(distinct c.mutation) = 1) a
This query gives you exact, fine control over the conditions you want to test for. If you then want to test for 1 and only 1 mutation in set A and 2 and only 2 mutations in set B, just change
count(distinct c.mutation) = 2
in the HAVING clause. This also lets you use > and <, so you can ask for 3 or more mutations by using
count(distinct c.mutation) >= 3
You can also vary the count for b.mutation to adjust the number of mutations allowed from set A, so any combination can now be extracted.
Thanks to Jake for the code help.
Now you have two sets of mutations you are interested in, a primary set A and a secondary set B. You want to know how many sequences contain 1 and only 1 mutation from set A and 1 and only 1 from set B.
select count(*) from (
SELECT distinct a.seq_id
FROM sequence a
JOIN seq_mutations b ON b.seq_id = a.seq_id
JOIN seq_mutations c ON c.seq_id = a.seq_id
WHERE b.mutation IN ('mutA','mutB','mutC')
AND c.mutation IN ('mut1', 'mut2', 'mut3', 'mut4', 'mut5')
GROUP BY a.seq_id
HAVING
count(distinct b.mutation) = 1
AND count(distinct c.mutation) = 1) a
This query gives you exact, fine control over the conditions you want to test for. If you then want to test for 1 and only 1 mutation in set A and 2 and only 2 mutations in set B, just change
count(distinct c.mutation) = 2
in the HAVING clause. This also lets you use > and <, so you can ask for 3 or more mutations by using
count(distinct c.mutation) >= 3
You can also vary the count for b.mutation to adjust the number of mutations allowed from set A, so any combination can now be extracted.
Thanks to Jake for the code help.
Labels:
bioinformatics,
postgres
Monday, April 30, 2012
Convert Postgres column from text to numeric type
ALTER TABLE foo ALTER COLUMN col TYPE NUMERIC USING col::numeric
You can use this with data in the table. All data must be convertable (no alpha chars). You can also specify numeric formatting here if you like.
You can use this with data in the table. All data must be convertable (no alpha chars). You can also specify numeric formatting here if you like.
Wednesday, October 5, 2011
cron Hell
Finally solved a nightmare trying to get a python script to execute properly (at all!) under crontab on Linux 64-bit machine.
Things I learned:
1. cron executes using /bin/sh so if you want to execute a bash script, you need to call it explicitly in crontab like this */1 * * * * root /bin/bash /opt/bin/hcv_update.cron
Things I learned:
1. cron executes using /bin/sh so if you want to execute a bash script, you need to call it explicitly in crontab like this */1 * * * * root /bin/bash /opt/bin/hcv_update.cron
2. cron does not have it's own environment variables, so you need to figure out how to get it to see those for the user you want to run as (root). The easiest way to do this is to make a shell script that calls your python script. You have more control over what is going on. Also, you can source your .bash_profile so that the variables are now inherited.
#!/bin/bash
source /root/.bash_profile
# calls python update script
/usr/local/bin/python2.7 /opt/bin/hcv_comm_query.py
3. If you have more than one version of python on the system (RedHat always has 2.4 present), make sure you explicitly call the right one.
4. In this case, was using cx_Oracle as well, so explicitly defined the Oracle paths in the python script just in case. This shouldn't be necessary though if they are already present in the .bash_profile
import os
os.environ["LD_LIBRARY_PATH"]="/usr/lib/oracle/11.2/client64/lib"
os.environ["ORACLE_HOME"]="/usr/lib/oracle/11.2/client64"
os.environ["TNS_ADMIN"]="/usr/lib/oracle/11.2/client64"
import cx_Oracle as cxo
This should solve the problem.
Thursday, September 29, 2011
Django, Apache 2 and mod_wsgi
A pain in the neck to set up, but nice to have.
1. Follow installation instructions for mod_wsgi and django
2. Add to httpd.conf
# ----------- Django / WSGI Configuration ----------
WSGIDaemonProcess processes=2 threads=15
WSGIScriptAlias /mgrm "/home/django/var/www/mgrm/apache/django.wsgi"
<Directory "/home/django/var/www/mgrm/">
Order allow,deny
Allow from all
</Directory>
Alias "/static/admin" "/opt/python2.7/lib/python2.7/site-packages/django/contrib/admin/media/"
<Directory "/opt/python2.7/lib/python2.7/site-packages/django/contrib/admin/media/">
Order allow,deny
Allow from all
</Directory>
*Note that the alias for "static/admin" must match whatever alias and path are in the main settings.py file in your Django project directory
3. Create a django.wsgi file in the place that is specified by the path you used in the WSGIScriptAlias that contains the following:
import os
import sys
# Option one
#sys.path.append('/home/django/var/www')
#sys.path.append('/home/django/var/www/mgrm')
#os.environ['DJANGO_SETTINGS_MODULE'] = 'mgrm.settings'
#os.environ['DJANGO_ENV'] = 'PRODUCTION'
# Option two
# from http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
sys.path.insert(0,'/home/django/var/www/mgrm')
import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
# Common to both options
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
*Note that this file goes inside your django project folder, preferably in an apache folder.
5. Edit main urls.py so that you can use files with both the django server and apache like this:
urlpatterns = patterns('',
# mod_wsgi does NOT pass the '/mgrm' mount point to this application. However,
# the django development server does. So in order to get these urls.py to
# work correctly with both, I created a match group that doesn't create a
# back reference. That match group is this: (?:mgrm/)?
url(r'^(?:mgrm/)?polls/',include('polls.urls')),
# Admin sites are doing some reverse url lookup, and the match group trick
# doesn't work with them. To resolve this issue we create two references:
# one for mod_wsgi, and the other for the development server.
url(r'^admin/',include(admin.site.urls)),
url(r'^mgrm/admin/', include(admin.site.urls)),
)
6. May need to edit templates to add the application name to each url in the template.
1. Follow installation instructions for mod_wsgi and django
2. Add to httpd.conf
# ----------- Django / WSGI Configuration ----------
WSGIDaemonProcess processes=2 threads=15
WSGIScriptAlias /mgrm "/home/django/var/www/mgrm/apache/django.wsgi"
<Directory "/home/django/var/www/mgrm/">
Order allow,deny
Allow from all
</Directory>
Alias "/static/admin" "/opt/python2.7/lib/python2.7/site-packages/django/contrib/admin/media/"
<Directory "/opt/python2.7/lib/python2.7/site-packages/django/contrib/admin/media/">
Order allow,deny
Allow from all
</Directory>
*Note that the alias for "static/admin" must match whatever alias and path are in the main settings.py file in your Django project directory
3. Create a django.wsgi file in the place that is specified by the path you used in the WSGIScriptAlias that contains the following:
import os
import sys
# Option one
#sys.path.append('/home/django/var/www')
#sys.path.append('/home/django/var/www/mgrm')
#os.environ['DJANGO_SETTINGS_MODULE'] = 'mgrm.settings'
#os.environ['DJANGO_ENV'] = 'PRODUCTION'
# Option two
# from http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html
sys.path.insert(0,'/home/django/var/www/mgrm')
import settings
import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')
command.validate()
import django.conf
import django.utils
django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
# Common to both options
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
*Note that this file goes inside your django project folder, preferably in an apache folder.
5. Edit main urls.py so that you can use files with both the django server and apache like this:
urlpatterns = patterns('',
# mod_wsgi does NOT pass the '/mgrm' mount point to this application. However,
# the django development server does. So in order to get these urls.py to
# work correctly with both, I created a match group that doesn't create a
# back reference. That match group is this: (?:mgrm/)?
url(r'^(?:mgrm/)?polls/',include('polls.urls')),
# Admin sites are doing some reverse url lookup, and the match group trick
# doesn't work with them. To resolve this issue we create two references:
# one for mod_wsgi, and the other for the development server.
url(r'^admin/',include(admin.site.urls)),
url(r'^mgrm/admin/', include(admin.site.urls)),
)
6. May need to edit templates to add the application name to each url in the template.
Wednesday, September 14, 2011
How to get Django to see multiple PostgreSQL schemas
Took awhile to figure this out, so here goes.
First create a PostgreSQL user that will be used by Django to connect to the database. This is the user that will be included in the settings.py file for the database connection section.
Log into PostgreSQL as admin/superuser and issue the following command:
GRANT USAGE SCHEMA foo TO django_user;
(Or GRANT USAGE to any role which has django_user as a (direct or indirect) member.)
(Or GRANT ALL ... if that is what you want.)
The next step is to change the default schema search path. To make a permanent change, do the following:
ALTER ROLE django_user SET SEARCH_PATH to "$user",public,your_schema;
Log out and log back in for the change to take effect. You can test the outcome by doing a \dt and you should see all table from all schemas that the role has been granted access to.
You can now run manage.py inspectdb and it will see all tables in all schemas. Don't know yet how it will treat tables with the same name in different schemas, as it is no longer required to prefix the schema name in a query, although it can still be done.
First create a PostgreSQL user that will be used by Django to connect to the database. This is the user that will be included in the settings.py file for the database connection section.
Log into PostgreSQL as admin/superuser and issue the following command:
GRANT USAGE SCHEMA foo TO django_user;
(Or GRANT USAGE to any role which has django_user as a (direct or indirect) member.)
(Or GRANT ALL ... if that is what you want.)
The next step is to change the default schema search path. To make a permanent change, do the following:
ALTER ROLE django_user SET SEARCH_PATH to "$user",public,your_schema;
Log out and log back in for the change to take effect. You can test the outcome by doing a \dt and you should see all table from all schemas that the role has been granted access to.
You can now run manage.py inspectdb and it will see all tables in all schemas. Don't know yet how it will treat tables with the same name in different schemas, as it is no longer required to prefix the schema name in a query, although it can still be done.
Subscribe to:
Posts (Atom)