[BioRuby] RegEx search example fasta file

pjotr at pckassa.com pjotr at pckassa.com
Sun Mar 21 08:33:00 EST 2004


Can this go in the sample directory of bioruby - I have added it to
the Wiki. Comments welcome.

Pj.


#! /usr/bin/ruby
#
#   $Id: fastasearch,v 1.1 2004/03/21 13:18:41 wrk Exp $
#   $Source: /home/cvs/home/pjotr/lwrk/luw/fasta/fastasearch,v $
#

# require 'profile'

COPYRIGHT = "GPL (c) 2003-2004"

usage = <<USAGE

    Search fasta file(s) tags using a regular expression (regex)

    Usage: fastasearch [-q query] filename(s)

    Example:

      ruby fastasearch -q '/([Hh]uman|[Hh]omo sapiens)/' nr.fa

    For more information see 

        http://thebird.nl/bioinformatics/
	
    Pjotr Prins
    Wageningen University and Research Centre
    http://www.wur.nl/
    http://www.dpw.wageningen-ur.nl/nema/

USAGE

# --------------------------------------------------------------------

srcpath=File.dirname($0)
libpath=File.dirname(srcpath)+'/lib'
$: << srcpath         # ---- Add start path to search libraries
$: << libpath

require 'getoptlong'
require 'bio'

# ---- Parse command line
opts = GetoptLong.new(
 [ "--help", "-h", GetoptLong::NO_ARGUMENT ],
 [ "--query", "-q", GetoptLong::REQUIRED_ARGUMENT ]
)

do_help       = false
query=nil

opts.each do | opt, arg |
   do_help   |= (opt == '--help')
   query = arg if (opt == '--query')
end

# ---- Print usage
if (do_help || ARGV.size==0)
  print usage
  exit 1
end

if !query
  print "Give query: "
  query = $stdin.gets.chomp
end

ARGV.each do | fn |
  $stderr.print "Loading #{fn}..."
  f = Bio::FlatFile.auto(fn)
  $stderr.print " detected: #{f.dbclass}\n"
  f.each_entry do | e |
    if e.definition =~ /#{query}/
      print '>',e.definition,e.data
    end
  end
end



More information about the BioRuby mailing list