File: ascQorder.rb

package info (click to toggle)
genometools 1.6.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 50,412 kB
  • sloc: ansic: 271,241; ruby: 30,339; python: 4,880; sh: 3,193; makefile: 1,194; perl: 219; pascal: 159; haskell: 37; sed: 5
file content (104 lines) | stat: -rwxr-xr-x 2,420 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby

require 'optparse'
require 'ostruct'

Sortinfo = Struct.new("Sortinfo",:len,:seqnum,:startfwdstrand)

def line2sortinfo(line)
  arr = line.split(/\s/)
  return Sortinfo.new(arr[4].to_i,arr[5].to_i,arr[6].to_i)
end

def compare_matches(line0,line1)
  sortinfo0 = line2sortinfo(line0)
  sortinfo1 = line2sortinfo(line1)
  if sortinfo0.seqnum < sortinfo1.seqnum or
       (sortinfo0.seqnum == sortinfo1.seqnum and
        sortinfo0.startfwdstrand + sortinfo0.len <=
        sortinfo1.startfwdstrand + sortinfo1.len)
    return -1
  end
  return 1
end

def parseargs(argv)
  options = OpenStruct.new
  options.inputfile = nil
  opts = OptionParser.new()
  opts.on("-c","--check","check order") do |x|
    options.check = true
  end
  opts.on( '-h', '--help', 'Display this screen' ) do
    puts "Usage: #{$0} [options] [inputfile]"
    puts "read matches from file or STDIN and sort them in ascending order of\nquery numbers. Matches on the same query are sorted in ascending order of the end position in query"
    exit 0
   end
  rest = opts.parse(argv)
  if rest.length == 1
    options.inputfile = rest[0]
  elsif rest.length > 1
    STDERR.puts options.banner
    exit 1
  end
  return options
end

def matches_checkorder(fpin)
  previousline = nil
  fpin.each_line do |line|
    if line.match(/^[0-9]/)
      if not previousline.nil? and compare_matches(previousline,line) > 0
        STDERR.puts "#{previousline.chomp} >\n#{line}"
        exit 1
      end
      previousline = line
    end
  end
end

def matches_orderedoutput(fpin)
  header = Array.new()
  headercomplete = false
  footer = Array.new()
  matchlines = Array.new()
  fpin.each_line do |line|
    if line.match(/^#/)
      if not line.match(/^# seed/)
        if headercomplete
          footer.push(line)
        else
          header.push(line)
        end
      end
    elsif line.match(/^[0-9]/)
      headercomplete = true
      matchlines.push(line)
    end
  end
  matchlines.sort! {|a,b| compare_matches(a,b)}
  puts header
  puts matchlines
  puts footer
end


options = parseargs(ARGV)
if options.inputfile.nil?
  fpin = STDIN
else
  begin
    fpin = File.new(options.inputfile,"r")
  rescue => err
    STDERR.puts "#{$0}: cannot open #{options.inputfile}: #{err}"
    exit 1
  end
end
if options.check
  matches_checkorder(fpin)
else
  matches_orderedoutput(fpin)
end
if not options.inputfile.nil?
  fpin.close_read
end