<commit_before>#!/usr/bin/env ruby
require 'optparse'
require 'logger'
require 'spreadsheet'
require 'csv'
require 'bio'

$logger = Logger.new(STDERR)

# Initialize logger
def setup_logger(loglevel)
  case loglevel
  when "debug"
    $logger.level = Logger::DEBUG
  when "warn"
    $logger.level = Logger::WARN
  when "info"
    $logger.level = Logger::INFO
  else
    $logger.level = Logger::ERROR
  end
end

def setup_options(args)
  options = {:out_file =>  "junctions_table.xls", :cut_off => 1000, :membrane_file => ""}
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: junctions.rb [options] junctions.bed hg19_refseq_genes_anno.gtf fasta.fa"
    opts.separator ""
    opts.on("-o", "--out_file [OUT_FILE]",
      :REQUIRED,String,
      "File for the output, Default: junctions_canditates_table.xls") do |anno_file|
      options[:out_file] = anno_file
    end

    opts.on("-m", "--mebrane_file [MEMBRANE_FILE]",
      :REQUIRED,String,
      "Text with membrane genes.") do |anno_file|
      options[:membrane_file] = anno_file
    end

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[:log_level] = "info"
    end

    opts.on("-d", "--debug", "Run in debug mode") do |v|
      options[:log_level] = "debug"
    end

    opts.on("-c", "--cut_off",:REQUIRED, Integer, "Set cut_off default is 1000") do |v|
      options[:cut_off] = v
    end

  end

  args = ["-h"] if args.length == 0
  opt_parser.parse!(args)
  raise "Please specify input files" if args.length != 3
  options
end

def read_annotation(anno_file)
  gene_info = {}
  CSV.read(anno_file, { :col_sep => "\t",:headers => :first_row }).each do |row|
    #puts row
    #puts row["name2"]
    gene_info[{:chrom => row["chrom"],:name => row["name"],:txStart => row["txStart"],:txEnd => row["txEnd"]}] = {:chrom => row["chrom"], :strand => row["strand"],
      :txStart => row["txStart"], :txEnd => row["txEnd"], :name2 => row["name2"],
      :exonStarts => row["exonStarts"], :exonEnds => row["exonEnds"],
      :exonCount => row["exonCount"]}
  end
  gene_info
end

def read_index(fai_file)
  fai_index = {}
  File.open(fai_file).each do |line|
    line.chomp!
    chr,length,start = line.split("\t")
    length = length.to_i
    start = start.to_i
    bias = length/15
    fai_index[chr] = {:start => start, :stop => start+length+bias-1}
  end
  fai_index
end

def match_junctions(junctions,gene_info,out_file,membrane_names, fasta)
  book = Spreadsheet::Workbook.new
  sheet1 = book.create_worksheet
  sheet1.row(0).push 'Pos', 'ID', '# Skipped Exons', 'New Exon', 'Within exon',
    '# reads', 'Refseq ID'

  fai_index = read_index("#{fasta}.fai")
  $logger.debug("FAI index: #{fai_index}")
  #fasta_file = File.open("/Users/hayer/Downloads/mm9_ucsc.fa").read
  fasta_file = File.open(fasta).read  #lines.map {|e| e.strip }.join("")
  seq_hash = {}
  fai_index.each_pair do |name, index|
    seq_hash[name] = fasta_file[index[:start]..index[:stop]].delete("\n")<commit_msg>hitting the pause button<commit_after>#!/usr/bin/env ruby
require 'optparse'
require 'logger'
require 'spreadsheet'
require 'csv'
require 'bio'

$logger = Logger.new(STDERR)

# Initialize logger
def setup_logger(loglevel)
  case loglevel
  when "debug"
    $logger.level = Logger::DEBUG
  when "warn"
    $logger.level = Logger::WARN
  when "info"
    $logger.level = Logger::INFO
  else
    $logger.level = Logger::ERROR
  end
end

def setup_options(args)
  options = {:out_file =>  "junctions_table.xls", :cut_off => 1000, :membrane_file => ""}
  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: junctions.rb [options] junctions.bed hg19_refseq_genes_anno.gtf fasta.fa"
    opts.separator ""
    opts.on("-o", "--out_file [OUT_FILE]",
      :REQUIRED,String,
      "File for the output, Default: junctions_canditates_table.xls") do |anno_file|
      options[:out_file] = anno_file
    end

    opts.on("-m", "--mebrane_file [MEMBRANE_FILE]",
      :REQUIRED,String,
      "Text with membrane genes.") do |anno_file|
      options[:membrane_file] = anno_file
    end

    opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options[:log_level] = "info"
    end

    opts.on("-d", "--debug", "Run in debug mode") do |v|
      options[:log_level] = "debug"
    end

    opts.on("-c", "--cut_off",:REQUIRED, Integer, "Set cut_off default is 1000") do |v|
      options[:cut_off] = v
    end

  end

  args = ["-h"] if args.length == 0
  opt_parser.parse!(args)
  raise "Please specify input files" if args.length != 3
  options
end

def read_annotation(anno_file)
  gene_info = {}
  CSV.read(anno_file, { :col_sep => "\t",:headers => :first_row }).each do |row|
    #puts row
    #puts row["name2"]
    gene_info[{:chrom => row["chrom"],:name => row["name"],:txStart => row["txStart"],:txEnd => row["txEnd"]}] = {:chrom => row["chrom"], :strand => row["strand"],
      :txStart => row["txStart"], :txEnd => row["txEnd"], :name2 => row["name2"],
      :exonStarts => row["exonStarts"], :exonEnds => row["exonEnds"],
      :exonCount => row["exonCount"]}
  end
  gene_info
end

def read_index(fai_file)
  fai_index = {}
  File.open(fai_file).each do |line|
    line.chomp!
    chr,length,start = line.split("\t")
    length = length.to_i
    start = start.to_i
    bias = length/5
    fai_index[chr] = {:start => start, :stop => start+length+bias-1}
  end
  fai_index
end

def match_junctions(junctions,gene_info,out_file,membrane_names, fasta)
  book = Spreadsheet::Workbook.new
  sheet1 = book.create_worksheet
  sheet1.row(0).push 'Pos', 'ID', '# Skipped Exons', 'New Exon', 'Within exon',
    '# reads', 'Refseq ID'

  fai_index = read_index("#{fasta}.fai")
  $logger.debug("FAI index: #{fai_index}")
  #fasta_file = File.open("/Users/hayer/Downloads/mm9_ucsc.fa").read
  fasta_file = File.open(fasta).read  #lines.map {|e| e.strip }.join("")
  seq_hash = {}
  fai_index.each_pair do |name, index|
    seq_hash[name] = fasta_file[index[:start]..index[:stop]].delete("\n")
{'fraction_non_alphanumeric': 0.17407283661877715, 'fraction_numerical': 0.007183427998663549, 'mean_word_length': 3.7667197452229297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '705899', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2204, 'n_words': 472}