<commit_before>module Transrate

  class Samtools

    class SamtoolsError < StandardError; end

    # Get the path to the samtools binary built when bio-samtools
    # was installed
    def self.path
      if !@path
        which_samtools = Cmd.new("which samtools")
        which_samtools.run
        if !which_samtools.status.success?
          raise SamtoolsError.new("could not find samtools in the path")
        end
        @path = which_samtools.stdout.split("\n").first
      end
      return @path
    end

    # Run a samtools command
    def self.run cmd
      runcmd = Cmd.new "#{Samtools.path} #{cmd}"
      runcmd.run
      if !runcmd.status.success?
        logger.warn "Samtools command failed: #{runcmd}" +
                    "\n#{runcmd.stderr}"
      end
      runcmd.stdout
    end

    # Convert a sam file to a bam file, returning the path to the bamfile
    def self.sam_to_bam samfile
      bamfile = File.basename(samfile, '.sam') + '.bam'
      bamfile = File.expand_path bamfile
      if !File.exist?(bamfile)
        Samtools.run "view -bS #{File.expand_path samfile} > #{bamfile}"
      end
      bamfile
    end

    # Sort a bam file, returning the path to the sorted bamfile
    def self.sort_bam bamfile, threads=4
      # the sort command behaves inconsistently with the other commands:
      # it takes an output prefix rather than a filename
      # and automatically adds the .bam extension
      sorted = File.basename(bamfile, '.bam') + '.sorted'
      if !File.exist?("#{sorted}.bam")
        cmd = "sort"
        cmd << " -l #{threads}"
        cmd << " #{File.expand_path bamfile} #{sorted}"
        Samtools.run cmd
      end
      File.expand_path(sorted + '.bam')
    end

    # Sort a bam file by readname only, returning the path to th
    # sorted bamfile
    def self.readsort_bam bamfile, threads=4
      # the sort command behaves inconsistently with the other commands:
      # it takes an output prefix rather than a filename
      # and automatically adds the .bam extension
      sorted = File.basename(bamfile, '.bam') + '.sorted'
      if !File.exist?("#{sorted}.bam")
        cmd = "sort"
        cmd << " -l #{threads}"
        cmd << " -n" # sort by read name only
        cmd << " #{File.expand_path bamfile} #{sorted}"
        Samtools.run cmd
      end<commit_msg>fix samtools sort threads argument (#96)<commit_after>module Transrate

  class Samtools

    class SamtoolsError < StandardError; end

    # Get the path to the samtools binary built when bio-samtools
    # was installed
    def self.path
      if !@path
        which_samtools = Cmd.new("which samtools")
        which_samtools.run
        if !which_samtools.status.success?
          raise SamtoolsError.new("could not find samtools in the path")
        end
        @path = which_samtools.stdout.split("\n").first
      end
      return @path
    end

    # Run a samtools command
    def self.run cmd
      runcmd = Cmd.new "#{Samtools.path} #{cmd}"
      runcmd.run
      if !runcmd.status.success?
        logger.warn "Samtools command failed: #{runcmd}" +
                    "\n#{runcmd.stderr}"
      end
      runcmd.stdout
    end

    # Convert a sam file to a bam file, returning the path to the bamfile
    def self.sam_to_bam samfile
      bamfile = File.basename(samfile, '.sam') + '.bam'
      bamfile = File.expand_path bamfile
      if !File.exist?(bamfile)
        Samtools.run "view -bS #{File.expand_path samfile} > #{bamfile}"
      end
      bamfile
    end

    # Sort a bam file, returning the path to the sorted bamfile
    def self.sort_bam bamfile, threads=4
      # the sort command behaves inconsistently with the other commands:
      # it takes an output prefix rather than a filename
      # and automatically adds the .bam extension
      sorted = File.basename(bamfile, '.bam') + '.sorted'
      if !File.exist?("#{sorted}.bam")
        cmd = "sort"
        cmd << " -@ #{threads}"
        cmd << " #{File.expand_path bamfile} #{sorted}"
        Samtools.run cmd
      end
      File.expand_path(sorted + '.bam')
    end

    # Sort a bam file by readname only, returning the path to th
    # sorted bamfile
    def self.readsort_bam bamfile, threads=4
      # the sort command behaves inconsistently with the other commands:
      # it takes an output prefix rather than a filename
      # and automatically adds the .bam extension
      sorted = File.basename(bamfile, '.bam') + '.sorted'
      if !File.exist?("#{sorted}.bam")
        cmd = "sort"
        cmd << " -@ #{threads}"
        cmd << " -n" # sort by read name only
        cmd << " #{File.expand_path bamfile} #{sorted}"
        Samtools.run cmd
      end
{'fraction_non_alphanumeric': 0.10010672358591248, 'fraction_numerical': 0.0012806830309498398, 'mean_word_length': 2.272346368715084, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4505859', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1425, 'n_words': 462}