Permalink
Browse files

Trim sequences to codon boundary

  • Loading branch information...
1 parent f7bb8c4 commit 16e376e33889d8816267955502a9ba671c186ba3 @pjotrp committed Apr 26, 2013
Showing with 52 additions and 3 deletions.
  1. +52 −3 bin/fasta_filter.rb
View
55 bin/fasta_filter.rb
@@ -4,10 +4,59 @@
#
require 'bigbio'
+require 'optparse'
+require 'ostruct'
-count = 0
-FastaReader::emit_fastarecord(-> { gets }) { | rec |
- p count += 1
+class OptParser
+
+ #
+ # Return a structure describing the options.
+ #
+ def self.parse(args)
+ # The options specified on the command line will be collected in *options*.
+ # We set default values here.
+ options = OpenStruct.new
+ options.codonize = false
+ options.verbose = false
+
+ opt_parser = OptionParser.new do |opts|
+ opts.banner = "Usage: fasta_filter.rb [options]"
+
+ opts.separator ""
+ opts.separator "Specific options:"
+
+ opts.on("--codonize",
+ "Trim sequence to be at multiple of 3 nucleotides") do |b|
+ options.codonize = b
+ end
+
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
+ options.verbose = v
+ end
+
+ opts.separator ""
+ opts.separator "Other options:"
+
+ opts.on_tail("-h", "--help", "Show this message") do
+ puts opts
+ exit
+ end
+
+ end
+
+ opt_parser.parse!(args)
+ options
+ end # parse()
+
+end # class OptParser
+
+options = OptParser.parse(ARGV)
+
+FastaReader::emit_fastarecord(-> { ARGF.gets }) { | rec |
+ if options.codonize
+ size = rec.seq.size
+ rec.seq = rec.seq[0..size - (size % 3) - 1]
+ end
print rec.to_fasta
}

0 comments on commit 16e376e

Please sign in to comment.