A collection of computer systems and programming tips that you may find useful.
 
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Wednesday, September 8, 2010

Mouse and Trackball Setup with MacPymol

I was having problems configuring a Kensington Expert Mouse Trackball to work correctly with the MacPymol protein modeling software on a Mac OS X 10.6.3 Intel Mac.

The Kensington mouse drivers (and specifically their MouseWorks software) has not been updated for several years and they are not supporting the trackball on Mac OS X Snow Leopard. But in fact the software works... MouseWorks is a 32bit version of a Preference Pane and so Preferences will have to shift to that version before you can interact with it.

But with just the trackball on that machine I was not able to configure the buttons correctly. That was the only mouse I had on that machine.

I noticed references in the MacPymol docs saying that the Apple Mighty Mouse with the little ball works well with the software. Even though I don't like that mouse, I did have one lying around so I plugged that it and configured it. With following setup it works as expected with MacPymol:

Left Button = Primary Button
Ball Button = Button 3
Right Button = Secondary Button

In 3-Button Viewing Mode this setup gives you Rotation on Left Button down and drag, Zoom on Right Button down and drag and XY translation on Ball Button down and drag.

Interestingly, once I had the Mighty Mouse setup like this, I could then get the Trackball working correctly. In Kensington MouseWOrks I have it set up as follows:
Left Lower button = Click
Right Lower Button = Right-Click
Left Upper Button = Middle-Click

MouseWorks knows about MacPymol (somehow) and you can setup MacPymol application settings - but I don't have this figured out. Unfortunately I'm not finding any more info in the MacPymol docs/wiki.

Hope this is useful


 

Thursday, August 26, 2010

Printing Colored Text in UNIX Terminals from Ruby

Outputting text in color in a UNIX console/Xterminal is kind of old school - some might say obsolete - but once in a while it is very useful. One example is the Red/Green coloring of test results. You don't get a lot of options but enough to highlight relevant text.

The cryptic escape codes are explained in this Wiki page.

This Ruby script outputs fore and background colors and a couple of the other useful modifiers.
#!/usr/bin/ruby

puts "\033[1mForeground Colors...\033[0m\n"
puts " \033[30mBlack (30)\033[0m\n"
puts " \033[31mRed (31)\033[0m\n"
puts " \033[32mGreen (32)\033[0m\n"
puts " \033[33mYellow (33)\033[0m\n"
puts " \033[34mBlue (34)\033[0m\n"
puts " \033[35mMagenta (35)\033[0m\n"
puts " \033[36mCyan (36)\033[0m\n"
puts " \033[37mWhite (37)\033[0m\n"
puts ''

puts "\033[1mBackground Colors...\033[0m\n"
puts " \033[40m\033[37mBlack (40), White Text\033[0m\n"
puts " \033[41mRed (41)\033[0m\n"
puts " \033[42mGreen (42)\033[0m\n"
puts " \033[43mYellow (43)\033[0m\n"
puts " \033[44mBlue (44)\033[0m\n"
puts " \033[45mMagenta (45)\033[0m\n"
puts " \033[46mCyan (46)\033[0m\n"
puts " \033[47mWhite (47)\033[0m\n"
puts ''
puts "\033[1mModifiers...\033[0m\n"
puts " Reset (0)"
puts " \033[1mBold (1)\033[0m\n"
puts " \033[4mUnderlined (4)\033[0m\n"
Note that the exact colors will vary between different terminals. So try it and see.

The script will show you enough to create your own colored terminal output. But what if you sometimes want your output to go to a terminal (TTY) and other times to a file? In the latter, all the escape codes will give you garbage characters when you view the file. The trick here is to test whether our not your script is writing to a TTY with 'STDOUT.tty?'. If so then add the escape codes, if not then leave them out.


 

Wednesday, August 11, 2010

SSL Certificates and Apache2

The process of generating a new or renewal SSL certificate for a web site can appear quite daunting given the terminology involved. In reality it is pretty simple. Here are the steps I used for renewing a Wildcard SSL cert, using rapidssl.com as the vendor.

A wildcard certificate costs more than an single fully qualified domain name, but it lets me secure any FQDN under my main domain.

The basics steps are:
1: Generate a Key
2: Generate a CSR (Certificate Signing Request)
3: Send that the vendor and give them some money
4: Receive an SSL Certificate back via email
5: Paste that into a file on your host
6: Restart Apache and verify operation

In Detail, here are my specific steps:

1: Start the new or renewal certificate process on the vendor's web site

2: Create a SSL Key
You should only need to do this once and then you can reuse it multiple times.
Create a directory in which to create and save your SSL files. For the purposes of this tutorial I'm using 'example.com as the domain. You can put the key into any file but give it one that makes sense, i.e. example.com.key
# openssl genrsa 1024 > example.com.key
Generating RSA private key, 1024 bit long modulus
.................................++++++
.........++++++
e is 65537 (0x10001)
If you want to protect the key with a passphrase then add the -des3 option to the command. If you do then you need to enter the passphrase whenever your server starts up. if you do NOT then set the permissions on your files correctly (see below).

3: Create a CSR (Certificate Signing Request)
This process will ask you several questions, of which the MOST IMPORTANT is the COMMON NAME. This the Domain Name that you want the certificate for. For a single domain this might be www.example.com. For a Wildcard certificate use an asterisk in the name like *.example.com.

The prompt from the openssl program is unclear when it asks for YOUR name - it wants the domain name. Clear on that, right?

It also asks for a challenge password - with rapidssl.com this wasn't used, so don't worry about it. Enter the other info as appropriate for your organization.
# openssl req -new -key ./example.com.key > example.com.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:Washington
Locality Name (eg, city) []:Seattle
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Craic Computing LLC
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:*.example.com
Email Address []:youraddress@example.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

4: Send the CSR to your Vendor
Cut and paste the contents of the file in full into the Vendor's web form. With rapidssl.com there is a phone verification process that you have to go through after they receive it. Basically an automated call comes through and you have to enter a confirmation code that they put on their web site. Plus you need to do the whole credit card and contact information thing on their site. It all ends with several emails back from them, one of which includes the Certificate.

5: Paste the certificate into a file on your host
Once again, choose an appropriate name, e.g. example.com.crt
Verify the contents of the certificate file with:
# openssl x509 -text -in example.com.crt
You will see all sorts of information.

6: CHANGE THE FILE PERMISSIONS - CHANGE THEM NOW!
# chmod 400 example.com.key
# chmod a-w example.com.crt


7: Setup Apache2
Modify your configuration files so that Apache knows where to find the certificate. In my case I'm using something like this:
<VirtualHost *:443>
ServerName www.example.com
ServerAlias *.example.com
DocumentRoot "/path/to/my/site"
SSLEngine on
SSLOptions +StrictRequire
SSLCertificateFile /mnt/ssl_certs/example.com.crt
SSLCertificateKeyFile /mnt/ssl_certs/example.com.key
</VirtualHost>
You can put the certificate and key in any files you want, you just need to tell Apache where they. Your Apache configuration might be different from this.

Restart apache!

8: Test it out
Go to the site in your browser. You should see a lock icon. Click on that and view the certificate details. It should all be good.

9: COPY THE KEY AND CERTIFICATE FILES TO A BACKUP MACHINE - COPY THEM NOW!

 

Friday, August 6, 2010

Maximum Fixnum value in Ruby

The Maximum Fixnum value that can be represented in Ruby is calculated as: (2**(0.size * 8 -2) -1)

Wednesday, June 30, 2010

Rails Routes and Models with 'Uncountable' Names

Rails follows the convention of using singular and plural forms of model names for various purposes. For example user.rb for a Model name and users_controller.rb for the controller.

Every once in a while you need a model where the singular and plural forms are the same. I just ran into that with a model called 'species'. This can cause you all sorts of hurt but here is a way round it.

First of all, decide if you really need to use that name. If you can figure out an equivalent where singular and plural forms are different then use it - much simpler... but if like me there is no good alternative then you do this:

1: Tell the built-in pluralization functions to skip your name.

In config/initializers/inflections.rb add this:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( species )
end
2: Create your model, controller, table etc in the normal way using 'species' as the model name.

3: In your routes.rb give the :species resource an artificial singular form:
ActionController::Routing::Routes.draw do |map|
map.resources :species, :singular => :species_instance

Doing this allows the routing magic to distinguish between singular and plural. But the downside is that you need to change a bunch of controller and view paths.

4: run 'rake routes' to see what Rails expects your new routes to look like.

5: In your controller, change any 'redirect_to @species' lines to 'redirect_to species_instance_path(@species)' (Should be just create and update actions).

6: In your index.html.erb view change:
link_to "Show", species
to
link_to "Show", species_instance_path(species)
In doing so you make it explicit that you want the singular form.
Similarly, change the following:
"Edit", edit_species_path(species)
becomes
"Edit", edit_species_instance_path(species)

link_to "Destroy", species ...
becomes
link_to "Destroy", species_instance_path(species) ...

and finally
link_to "New Species", new_species_path
becomes
link_to "New Species", new_species_instance_path

7: In your show, new and edit view pages, make similar changes. Links back to the index page can stay as 'species_path' but edit, delete and new links should be updated as shown above.

It's messy, for sure, but it solves the problem and allows you to retain 'natural' names in the user interface and in the URLs that users will see.



 

Tuesday, May 11, 2010

Compiling Ghostscript on Intel 64 bit Mac OS X

Ran into problems getting Ghostscript to compile on an Intel Mac running Snow Leopard.

The error messages were telling me it was trying to compile a 32 executable, and then after fixing that it complained that libraries in /opt/local/lib were compiled for 32 bit. The thing is, except for some old mac ports stuff, I don't use /opt/local!

I was able to fix compile successfully with three steps.

1: Take the old /opt/local/lib code out of the picture by renaming the directory to a temporary name. I can bring it back if it turns out something actually relies on it.

2: Rub ./configure with CFLAGS and LDFLAGS defined. These end up getting passed to 'make'
$ ./configure CFLAGS='-arch x86_64' LDFLAGS='-arch x86_64'


3: Compile it 'make' (with no flags) and install with 'sudo make install'

Not sure yet if it can pick up all the fonts that it needs - we'll soon see...


 

Image Resizing with Rails Paperclip and ImageMagick

The Rails Paperclip plugin uses ImageMagick for creating thumbnail images, etc.

You specify the size of images using ImageMagick's Geometry syntax which can be a little confusing. Here are the aspects of that which are most useful for Paperclip users.

The basic specification is <width>x<height> in pixels, optionally followed by a modifier. In some cases you can omit either width or height.

  • 256x256

  • This specifies the Maximum dimensions for the image, while preserving the aspect ratio of the image. So if your image were actually 512x256 it would be resized to 256x128 in order to fit inside the specified size.

  • 256x256!

  • This specifies the Exact image size, so a 512x256 image would be changed into 256x256, losing the aspect ratio.

  • 256x

  • This specifies the desired width of the target image, leaving the height to be set so as to preserve the aspect ratio.

  • x256

  • This specifies the desired height of the target image, while preserving the aspect ratio.

  • 256x256^

  • This specifies the Minimum size for the target image, so the resized image may be larger than these dimensions.

  • 256x256>

  • This specifies that the image will be resized only if it is Larger than the dimensions.

  • 256x256<

  • This specifies that the image will be resized only if it is Smaller than the dimensions.


Other options exist within the syntax. See the ImageMagick docs for more details.


 

Archive of Tips