- 论坛徽章:
- 7
|
maybe:- #!/usr/bin/ruby
- $seq = [ 'ATATCCG', 'TCCG', 'ATGTACTG', 'ATGTCTG' ]
- $len = $seq.size
- $sgor = ( 0 .. $len ).map { |x| x * ( x - 1 ) / 2 }
- $that = $sgor[-1]
- $this = { [0] * $len => [ 0, 0 ] }
- $it = [ 0, 1 ].product( *[ [ 0, 1 ] ] * ( $len - 1 ) )
- def gimme pos
- $this[pos] = [ -999999999, 0 ]
- $it.each_with_index do |a, i|
- poz = ( 0 ... $len ).map { |j| pos[j] - a[j] }
- next if poz.min < 0
- char = Hash.new 0
- poz.each_with_index do |p, j|
- a[j] > 0 ? char[ $seq[j][p] ] += 1 : char['-'] += 1
- end
- scor = $this.key?(poz) ? $this[poz][0] : gimme(poz)
- scor = char.values.reduce(scor) {|s, e| s + $sgor[e] }
- scor -= $that
- $this[pos] = [ scor, i ] if scor > $this[pos][0]
- end
- $this[pos][0]
- end
- posi = $seq.map(&:length)
- score = gimme posi
- align = [''] * $len
- until ( i = $this[posi][1] ) == 0
- ( 0 ... $len ).each do |j|
- align[j] += $it[i][j] > 0 ? $seq[j][ posi[j] -= 1 ] : '-'
- end
- end
- puts score, align.map(&:reverse)
- __END__
复制代码 |
|