Install NetBeans jVi plugin

By kenglish

NetBeans is the only IDE with a great VI key binding plugin. This was the sole reason I switched to NetBeans as my Ruby/Rails IDE of choice this year. There are 2 vi plugins for Eclipse: one you have to pay for and the other relies on gVIM. The NetBeans Vi plugin is called jVi and can be found at http://jvi.sourceforge.net. There are a few caveats and extra configuration options that you need to set.

  1. Download the latest jVi release. Unzip the file in your home directory. This will create the directory nbvi-1.2.6.
  2. In the Netbeans menu bar, select Tools | Plugins. Click on the Downloaded tab.
  3. Press the “Add Plugin” button. Browse to the nbvi-1.2.6 directory and select the two file: org-netbeans-modules-jvi.nbm and com-raelity-jvi.nbm. You should now have 2 plugins availabe in the downloaded list: jVi Key Bindings and jVi Core.
  4. Click Install and then click through the installation process. NetBeans will need to restart.
  5. After NetBeans has restarted, from the menu bar, select Tools | Options. Click on the last tab which should be jVi Config.
  6. Select the “Buffer Modifications” panel in the jVi Config screen.
  7. Make sure the ‘expandtab’ value is checked.
  8. Change the value of ’shiftwidth’ to 2.
  9. Change the value of ‘tabstop’ to 2.

The last option for setting the tabstop and expandtab are pretty important. If you don’t use these, jVi will insert tabs into your Ruby files. This could cause your co-workers to complain about you ruining the formatting in the project.

Congratuations, jVi is now setup in NetBeans. Enjoy.

This blog entry was written for NetBeans 6.7 and with jVi version 1.2.6.

categoriaProgramming commento1 Comment dataNovember 22nd, 2009
Read All

Parsing Emboss Water output with Ruby

By kenglish

First, you will need to install the emboss suite on your computer:

sudo apt-get install emboss emboss-lib

If don’t already have the BioRuby installed, you will need that too:

sudo gem install bio --no-ri --no-rdoc

Your first ruby script calling Emboss Water from Ruby:

1
2
3
4
5
require 'rubygems'
require 'bio'
test_filename =ARGV.shift
target_filename =ARGV.shift
result = Bio::EMBOSS.run('water', '-asequence', test_filename, '-bsequence', target_filename)

Unforntunately, there is not a nice report result class in BioRuby for Emboss Water so you will have to parse the output yourself. Here’s an example script that finds percent similarity:

require 'rubygems'
require 'bio'
test_filename =ARGV.shift
target_filename =ARGV.shift
result = Bio::EMBOSS.run('water', '-asequence', test_filename, '-bsequence', target_filename)
# result now has the text output of water...
# Here's an example of looping through each line of the result to get the similary:
 
test_seq = ""
target_seq = ""
similarity = ''
 
 
result.split("\n").each do | line |
  # This mean
  if line =~ /^# Aligned_sequences/
    puts "Seq '#{test_seq}' has similarity to Seq '#{target_seq}' of #{similarity}"  unless (test_seq == "" ) && (target_seq == "")
    test_seq = ""
    target_seq = ""
  end
  # Get sequence numbers 
  if line =~ /^# (\d+): (\d+)/
     test_seq  = $2 if $1 == '1'
     target_seq = $2 if $1 == '2'
  end
  # parse similarity
  if line =~ /^# Similarity:.*\((.*)%\)/
    similarity  = $1
  end
end
 
puts "Seq '#{test_seq}' has similarity to Seq '#{target_seq}' of #{similarity}"

Place this in a file called water.rb and run it with frags.fasta and frags1.fasta and the above script will output this.

$ ruby water.rb fastas/frags1.fasta frags.fasta 
Seq '1' has similarity to Seq '1' of 100.0
Seq '1' has similarity to Seq '2' of 96.6
Seq '1' has similarity to Seq '3' of 64.3
Seq '1' has similarity to Seq '4' of 97.9
Seq '1' has similarity to Seq '5' of 96.9
Seq '1' has similarity to Seq '6' of 94.1
Seq '1' has similarity to Seq '7' of 62.5
Seq '1' has similarity to Seq '8' of 61.1
Seq '1' has similarity to Seq '9' of 62.5
Seq '1' has similarity to Seq '10' of 57.1
Seq '1' has similarity to Seq '11' of 57.4
Seq '1' has similarity to Seq '12' of 97.8
Seq '1' has similarity to Seq '13' of 50.0
Seq '1' has similarity to Seq '14' of 62.5
Seq '1' has similarity to Seq '15' of 97.9
Seq '1' has similarity to Seq '16' of 62.5
Seq '1' has similarity to Seq '17' of 59.1
Seq '1' has similarity to Seq '18' of 55.9
Seq '1' has similarity to Seq '19' of 61.9
Seq '1' has similarity to Seq '20' of 60.0
Seq '1' has similarity to Seq '21' of 56.4
Seq '1' has similarity to Seq '22' of 56.2

Water is the worse name for a program, EVER. Because it is impossible to Google…

categoriaBioinformatics, Programming commentoNo Comments dataNovember 20th, 2009
Read All

Defining Class methods in a Module

By kenglish

The code should speak for itself. Make sense?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Loveable
  module ClassMethods
    def give_hug
    end
  end
  def self.included(base)
    base.extend(ClassMethods)
  end
end
 
class Person
  include Loveable
 
  give_hug
 
end

I fuzzy as to why a certain Rails genius would suggest it is better to do it this way
(see line 7):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module Loveable
  module ClassMethods
    def give_hug
    end
  end
  def self.included(base)
    base.send :extend, ClassMethods
  end
end
 
class Person
  include Loveable
 
  give_hug
 
end

Feel free to comment…

categoriaProgramming commentoNo Comments dataNovember 20th, 2009
Read All

Install thoughtbot shoulda and rcov (the right way)

By kenglish

Install rcov & ruby-prof (rcov-0.9.6 & ruby-prof-0.7.3 at the time of this writing).

sudo gem install ruby-prof rcov --no-ri --no-rdoc

Update your test/test_helper.rb, add:

require 'shoulda/rails'

Install Thoughbot’s Shoulda gem (shoulda-2.10.2 at the time of this writing). Make sure you have added GemCutter as one of your ruby gem sources.

sudo gem install shoulda --no-ri --no-rdoc

Edit your applicaitons main Rakefile and add:

require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
require 'shoulda/tasks'
 
def run_coverage(files)
  rm_f "coverage"
  rm_f "coverage.data"
 
  # turn the files we want to run into a  string
  if files.length == 0
    puts "No files were specified for testing"
    return
  end
 
  files = files.join(" ")
 
  if PLATFORM =~ /darwin/
    exclude = '--exclude "gems/*"'
  else
    exclude = '--exclude "rubygems/*"'
  end
 
  rcov = "rcov --rails -Ilib:test --sort coverage --text-report #{exclude}  --aggregate coverage.data"
  cmd = "#{rcov} #{files}"
  puts cmd
  sh cmd
end
namespace :test do
 
  desc "Measures unit, functional, and integration test coverage"
  task :coverage do
    run_coverage Dir["test/**/*.rb"]
  end
 
  namespace :coverage do
    desc "Runs coverage on unit tests"
    task :units do
      run_coverage Dir["test/unit/**/*.rb"]
    end
    desc "Runs coverage on functional tests"
    task :functionals do
      run_coverage Dir["test/functional/**/*.rb"]
    end
    desc "Runs coverage on integration tests"
    task :integration do
      run_coverage Dir["test/integration/**/*.rb"]
    end
  end
end

Checkout your new coverage rake tasks:

rake -T | grep cov

Should show you:

rake test:coverage                        # Measures unit, functional, and integration test coverage
rake test:coverage:functionals            # Runs coverage on functional tests
rake test:coverage:integration            # Runs coverage on integration tests
rake test:coverage:units                  # Runs coverage on unit tests

categoriaProgramming commentoNo Comments dataNovember 19th, 2009
Read All

Example Bash script to rename directories

By kenglish

This script will rename directories based on a pattern. My goal was to rename my directories so the album year would be in [] and not (). For example, we would move /home/Music/ACDC/Back in Black (1980) to /home/Music/ACDC/Back in Black [1980]. The downside is you have to enter the pattern twice, once for bash and once for sed.

#!/bin/bash
 
find  /home/kenglish/Music -type d | while read DIR; do
 
  if [[ "$DIR" =~ \([0-9]{4}\)$ ]]; then
    NEW_DIR=`echo $DIR | sed 's/(\([0-9][0-9][0-9][0-9]\))$/[\1]/'`
    echo "$DIR    -->    $NEW_DIR" 
    ` mv "$DIR" "$NEW_DIR" `
  fi
done

categoriaLinux commentoNo Comments dataNovember 17th, 2009
Read All

Already some haters of Google Closure

By kenglish

I saw this article on Sitepoint about Google Closure: How not to write JavaScript. The author claims that Closure is just Java programmers trying to make Javascript like Java. Having spent a lot of time doing ExtJS over the past few months, I’ve grown rather fond of Javascript. I would say the worst part about it is the scoping problems.

categoriaProgramming commentoNo Comments dataNovember 15th, 2009
Read All

Bash script to copy files in order to my Coby mp305

By kenglish

I’m one of those people that refuses to get an IPOD. I think they are too expensive and they don’t play nice with Linux.

Last Christmas, I bought myself the 4GB Coby mp305 because it has more capacity than the Sandisk Sansa m200. The interface is crap compared to the Sansa m200. It doesn’t read the mp3 ID3tags at all. The navigation tree is simply the directory structure.

The major flaw is that it does not always sort files in the directory in the correct order. I finally figured out that it sorts files by the order that they were put on the device. However, for some reason in linux if you do “cp -R”, it doesn’t put them on in the proper order.

Here’s my script to put files on the device, it’s call coby_copy.sh:

#!/bin/bash
 
if [ !-d $1 ]; then
   echo "Source Directory does not exists"
   exit
fi
 
if [ !-d $2 ]; then
   echo "Target Directory does not exists"
   exit
fi
 
echo "arg1 = $1 arg2 = $2"
 
IFS=`echo -en "\n\b"`
 
for FILENAME in `find $1 -type f -iname "*mp3" -print | sort | sed 's/^\.\///'` 
do
  DIR=`dirname $FILENAME`
  mkdir -p $2/$DIR
  echo $FILENAME
  cp $FILENAME "$2/$DIR"
done

To run it:

coby_copy.sh "Harry Potter and Leopard-Walk-Up-to-Dragon"  "/mnt/disk/Audio Books"

categoriaLinux, Programming commentoNo Comments dataAugust 18th, 2009
Read All

MsSql: Select table column names

By kenglish

Sometimes I need to match table column names in Microsoft SQL Server. This seems to be the best way to do it:

SELECT COLUMN_NAME, TABLE_NAME 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%COST%'

categoriaProgramming commentoNo Comments dataAugust 18th, 2009
Read All

Install ruby gem libxml-ruby on Ubuntu 9.04 (Jaunty)

By kenglish

Quick note on how to install the libxml-ruby gem on Ubuntu:

sudo apt-get install libxml2 libxml2-dev
sudo gem install libxml-ruby

categoriaLinux, Programming commentoNo Comments dataAugust 18th, 2009
Read All

Setup for CAC card on Linux Ubuntu 9.04 in Firefox

By kenglish

Install CoolKey

sudo apt-get install coolkey -y

Install PCSC Tools:

sudo apt-get install libpcsclite1 pcsc-tools pcscd -y

Install dod-configuration-1.0.2.xpi into firefox by downloading it to a local directory and then doing “File|Open”. You have to get this from https://software.forge.mil which you can only get to with your CAC. Yes, I know. This makes a lot of sense. I got it from a co-worker.

Plugin your CAC card reader and insert your CAC. The green light should come on. Next, you will to set up the CAC in Firefox.

Open Firefox, goto Edit | Preferences | Advanced | Security Devices.

Click Load.

Enter the following:
Module Name : CAC Module
Module filename : /usr/lib/pkcs11/libcoolkeypk11.so

Load Device - Cac Module

The CAC should appear in your list of security devices now. Click “Log in” to test it. It should look something like this:

Device-Manager
Click “Log In” and enter your pin to test it.

Try to bring up https://software.forge.mil. Firefox will complain about the cert so just add an exception for it.

You should be all good. Let me know how this works out.

NOTE: I’m using Firefox 3.5

categoriaLinux commentoNo Comments dataJuly 30th, 2009
Read All