#!/usr/bin/env ruby require 'optparse' require 'methadone' require 'fasta_read.rb' require 'fileutils' class App include Methadone::Main include Methadone::CLILogging main do |assembly, chromosome, cstart, cend| snp = masked_unmasked(options)[0] subst = masked_unmasked(options)[1] path = "fasta/#{assembly}/#{snp}/chr#{chromosome + subst}.fa" debug("About to read #{assembly} #{snp} chr#{chromosome}:#{cstart}-#{cend}") if File.exist?(path) if options[:output] File.open(options[:output], "w") do |file| file.puts IO.read(path, cend.to_i - cstart.to_i, cstart.to_i) end else puts IO.read(path, cend.to_i - cstart.to_i, cstart.to_i) end else exit_now_with!(assembly_or_chromosome(assembly, chromosome)) end end def self.masked_unmasked(options) options[:snp] ? ["snp", ".subst"] : ["unmasked", ""] end def self.assembly_or_chromosome(assembly, chromosome) Dir.exist?("fasta/#{assembly}") ? [chromosome, "chromosome"] : [assembly, "assembly"] end def self.exit_now_with!(value_and_argument) exit_now!(1, "the '#{value_and_argument[0]}' #{value_and_argument[1]} doesn't exist in directory structure") end # supplemental methods here # Declare command-line interface here description "Extract DNA Fasta sequence from assembly files." # # Accept flags via: # on("--flag VAL","Some flag") # options[flag] will contain VAL # # Specify switches via: on "-o OUTPUTFILE", "--output", "outputs sequence to a file" on "--snp", "return the sequence from the SNP-masked assembly" # on("--[no-]switch","Some switch") # # Or, just call OptionParser methods on opts # # Require an argument arg :assembly, "assembly name (hg19, mm10, etc.)" arg :chromosome, "id of chromosome (1-22 or X/Y)" arg :cstart, "start coordinate (inclusive) within the chromosome" arg :cend, "end coordinate within the chromosome" # # # Make an argument optional # arg :optional_arg, :optional version FastaRead::VERSION use_log_level_option go! end