/usr/lib/ruby/vendor_ruby/cassiopee.rb is in ruby-cassiopee 0.1.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 | require 'digest/md5'
require 'logger'
require 'zlib'
require 'text'
module Cassiopee
# Calculate the edit or hamming distance between String and pattern
# Extend a String
# Return -1 if max is reached
def computeDistance(pattern,hamming,edit)
if(edit==0)
return computeHamming(pattern,hamming)
else
return computeLevenshtein(pattern,edit)
end
end
# Calculate the edit or hamming distance between String and pattern
# Extend a String
# Return -1 if max is reached
def computeAmbiguousDistance(pattern,hamming,edit,ambiguous)
if(edit==0)
return computeHammingAmbiguous(pattern,hamming,ambiguous)
else
return computeLevenshteinAmbiguous(pattern,edit,ambiguous)
end
end
# Compute Hamming distance but using a mapping matrix of alphabet ambiguity
def computeHammingAmbiguous(pattern,hamming,ambiguous)
nberr = 0
(0..(self.length-1)).each do |c|
if(!isAmbiguousEqual(pattern[c],self[c],ambiguous))
nberr = nberr+1
if(nberr>hamming.to_i)
return -1
end
end
end
return nberr
end
# Calculate number of substitution between string and pattern
# Extend a String
# Return -1 if max is reached
def computeHamming(pattern,hamming)
nberr = 0
(0..(self.length-1)).each do |c|
if(pattern[c] != self[c])
nberr = nberr+1
if(nberr>hamming.to_i)
return -1
end
end
end
return nberr
end
# Calculate the edit distance between string and pattern
# Extend a String
# Return -1 if max is reached
def computeLevenshtein(pattern,edit)
distance = Text::Levenshtein.distance(self, pattern)
if(distance>edit)
return -1
end
return distance
end
# Compute Levenshtein distance but using a mapping matrix of alphabet ambiguity
# Code comes from Text gem, Text::Levenshtein.distance, adapted for ambiguity comparison
def computeLevenshteinAmbiguous(pattern, edit, ambiguous)
prepare =
if "ruby".respond_to?(:encoding)
lambda { |str| str.encode(Encoding::UTF_8).unpack("U*") }
else
rule = $KCODE.match(/^U/i) ? "U*" : "C*"
lambda { |str| str.unpack(rule) }
end
s, t = [self, pattern].map(&prepare)
n = s.length
m = t.length
return m if (0 == n)
return n if (0 == m)
d = (0..m).to_a
x = nil
(0...n).each do |i|
e = i+1
(0...m).each do |j|
cost = (isAmbiguousEqual(s[i],t[j],ambiguous)) ? 0 : 1
x = [
d[j+1] + 1, # insertion
e + 1, # deletion
d[j] + cost # substitution
].min
d[j] = e
e = x
end
d[m] = x
end
if(x>edit)
return -1
end
return x
end
# checks if 2 chars are equal with ambiguity rules
# * ambigous is a Hash of char/Array of char mapping
def isAmbiguousEqual(a,b,ambiguous)
if(ambiguous==nil || (ambiguous[a.chr]==nil && ambiguous[b.chr]==nil ))
if(a==b)
return true
else
return false
end
end
if(a==b || (ambiguous[a.chr]!=nil && ambiguous[a.chr].index(b.chr)!=nil) || (ambiguous[b.chr]!=nil && ambiguous[b.chr].index(a.chr)!=nil))
return true
else
return false
end
end
# Class maning cache of results
class CrawlerCache
FILE_CACHE_EXT = ".sfc"
# Suffix files name/path
attr_accessor :file_suffix
# search exact: 0
# hamming : 1
# edit : 2
attr_accessor :method
# filter
attr_accessor :min_position
attr_accessor :max_position
# max errors
attr_accessor :errors
attr_accessor :cache
$log = Logger.new(STDOUT)
$log.level = Logger::INFO
def setLogger(userlogger)
$log = userlogger
end
def initialize
@file_suffix = "crawler"
end
# Loads cache from file
def loadCache
return Array.new unless File.exists?(@file_suffix+FILE_CACHE_EXT)
begin
file = Zlib::GzipReader.open(@file_suffix+FILE_CACHE_EXT)
rescue Zlib::GzipFile::Error
file = File.open(@file_suffix+FILE_CACHE_EXT, 'r')
ensure
obj = Marshal.load file.read
file.close
if(method!=obj.method || min_position<obj.min_position || max_position>obj.max_position || errors>obj.errors)
return Array.new
end
return filterCache(obj)
end
end
# Save self to cache, with cache object set from obj
def saveCache(obj)
self.cache = obj
marshal_dump = Marshal.dump(self)
sfxpos = File.new(@file_suffix+FILE_CACHE_EXT,'w')
sfxpos = Zlib::GzipWriter.new(sfxpos)
sfxpos.write marshal_dump
sfxpos.close
end
def clearCache
File.delete(@file_suffix+FILE_CACHE_EXT) unless !File.exists?(@file_suffix+FILE_CACHE_EXT)
end
private
# filter cache according to settings
# obj: cache object
def filterCache(cacheobject)
realmatches = Array.new
if(cacheobject==nil)
return realmatches
end
cacheobject.cache.each do |obj|
if(obj[1]>self.errors)
next
end
realpos = Array.new
realpos << obj[2][0]
(1..obj[2].length-1).each do |i|
curpos= obj[2][i]
if((curpos<=max_position || max_position==0) && curpos>=min_position)
realpos << curpos
end
end
if(realpos.length<=1)
next
end
realmatches << Array[obj[0],obj[1],realpos]
end
return realmatches
end
end
# Base class to index and search through a string
class Crawler
# Use alphabet ambiguity (dna/rna) in search, automatically set with loadAmbiguityFile
attr_accessor :useAmbiguity
# Suffix files name/path
attr_accessor :file_suffix
# Max number fo threads to use (not yet used)
attr_accessor :maxthread
# Use persistent suffix file ?
attr_accessor :use_store
# Array of comment characters to skip lines in input sequence file
attr_accessor :comments
# Manage basic cache to store previous match
attr_accessor :useCache
# Ambiguity map (Hash)
attr_accessor :ambiguous
# Method for search FORCE or SUFFIX
# * SUFFIX loads all suffixes and search through them afterwards, interesting for multiple searches (suffixes are reused)
# * FORCE checks matches while crossing the suffixes. Does not keep parsed data for later search
# FORCE method does not yet support optimal filters
attr_accessor :method
METHOD_DIRECT = 0
METHOD_SUFFIX = 1
@min_position = 0
@max_position = 0
# Previous position filter
@prev_min_position = 0
@prev_max_position = 0
@pattern = nil
FILE_SUFFIX_EXT = ".sfx"
FILE_SUFFIX_POS = ".sfp"
SUFFIXLEN = 'suffix_length'
$maxthread = 1
@cache = nil
$log = Logger.new(STDOUT)
$log.level = Logger::INFO
def initialize
@useAmbiguity = false
@ambiguous = nil
@useCache = false
@file_suffix = "crawler"
@method = 0
@prev_min_position = 0
@prev_max_position = 0
@suffix = nil
@suffixmd5 = nil
@position = 0
@suffixes = Hash.new
@matches = Array.new
@curmatch = 0
@use_store = false
@sequence = nil
@comments = Array["#"]
@cache = Cassiopee::CrawlerCache.new
end
def filterLength
filterOptimal(0)
end
def filterCost
filterOptimal(1)
end
# Clear suffixes in memory
# If using use_store, clear the store too
def clear
@suffixes = Hash.new
@matches.clear
@pattern = nil
@prev_max_position = 0
@prev_min_position = 0
@cache.clearCache()
File.delete(@file_suffix+FILE_SUFFIX_POS) unless !File.exists?(@file_suffix+FILE_SUFFIX_POS)
end
# Set Logger level
def setLogLevel(level)
$log.level = level
end
# Index an input file
# Clear existing indexes
def indexFile(f)
# Parse file, map letters to reduced alphabet
# Later on, use binary map instead of ascii map
# Take all suffix, order by length, link to position map on other file
# Store md5 for easier compare? + 20 bytes per suffix
@sequence = readSequence(f)
clear()
@min_position = 0
@max_position = 0
end
# Index an input string
# Clear existing indexes
def indexString(s)
@sequence = s
File.open(@file_suffix+FILE_SUFFIX_EXT, 'w') do |data|
data.puts(@sequence)
end
clear()
@min_position = 0
@max_position = 0
end
# Load ambiguity rules from a file
# File format should be:
# * A=B,C
# D=E,F
# ...
def loadAmbiguityFile(f)
if(!File.exists?(f))
$log.error("File "<< f << "does not exists")
exit(1)
end
@ambiguous = Hash.new
file = File.new(f, "r")
while (line = file.gets)
definition = line.downcase.chomp
ambdef = definition.split('=')
ambequal = ambdef[1].split(',')
@ambiguous[ambdef[0]] = ambequal
end
@useAmbiguity = true
$log.debug("loaded ambiguity rules: " << @ambiguous.inspect())
file.close
end
# Load sequence from a previous index command
def loadIndex
seq = ''
begin
file = File.new(@file_suffix+FILE_SUFFIX_EXT, "r")
while (line = file.gets)
input = line.downcase.chomp
seq << input
end
file.close
rescue => err
$log.error("Exception: #{err}")
exit()
end
@sequence = seq
clear()
@min_position = 0
@max_position = 0
end
# Filter matches to be between min and max start position
# If not using use_store, search speed is improved but existing indexes are cleared
# If max=0, then max is string length
# Must be called after index creation or load
def filter_position(min,max)
if(!use_store)
clear()
end
@prev_min_position = @min_position
@prev_max_position = @max_position
@min_position = min
@max_position = max
end
# Search exact match
def searchExact(s)
if(@useAmbiguity)
return searchApproximate(s,0)
end
s = s.downcase
updateCache(0,0)
@matches = @cache.loadCache()
if(@matches.length>0)
return cache?(@matches)
end
#@matches.clear
@pattern = Digest::MD5.hexdigest(s)
parseSuffixes(@sequence,s.length,s.length,0,s)
return @matches unless(method == METHOD_SUFFIX)
# Search required length, compare (compare md5?)
# MD5 = 128 bits, easier to compare for large strings
matchsize = @pattern.length
@suffixes.each do |md5val,posArray|
if (isMatchEqual?(md5val))
match = Array[md5val, 0, posArray]
$log.debug "Match: " << match.inspect
@matches << match
end
end
return cache?(@matches)
end
# Search an approximate string
#
# * support insertion, deletion, substitution
# * If edit > 0, use Hamming
# * Else use Levenshtein
def searchApproximate(s,edit)
if(edit==0 && !@useAmbiguity)
return searchExact(s)
end
allowederrors = edit
if(edit>=0)
useHamming = true
minmatchsize = s.length
maxmatchsize = s.length
updateCache(1,edit)
@matches = @cache.loadCache()
else
useHamming = false
edit = edit * (-1)
minmatchsize = s.length - edit
maxmatchsize = s.length + edit
updateCache(2,edit)
@matches = @cache.loadCache()
end
if(@matches.length>0)
return @matches
end
s = s.downcase
#@matches.clear
@pattern = Digest::MD5.hexdigest(s)
parseSuffixes(@sequence,minmatchsize,maxmatchsize,allowederrors,s)
return cache?(@matches) unless(method == METHOD_SUFFIX)
@suffixes.each do |md5val,posArray|
if(md5val == SUFFIXLEN)
next
end
if (md5val == @pattern)
filteredPosArray = filter(posArray)
match = Array[md5val, 0, filteredPosArray]
$log.debug "Match: " << match.inspect
@matches << match
else
if(posArray[0]>= minmatchsize && posArray[0] <= maxmatchsize)
# Get string
seq = extractSuffix(posArray[1],posArray[0])
errors = isApproximateEqual?(seq,s,useHamming,edit)
if(errors>=0)
filteredPosArray = filter(posArray)
match = Array[md5val, errors, filteredPosArray]
$log.debug "Match: " << match.inspect
@matches << match
end
end
end
end
return cache?(@matches)
end
# Filter the array of positions with defined position filter
def filter(posArray)
$log.debug("filter the position with " << @min_position.to_s << " and " << @max_position.to_s)
if(@min_position==0 && @max_position==0)
return posArray
end
filteredArray = Array.new
i = 0
posArray.each do |pos|
if(i==0)
# First elt of array is match length
filteredArray << pos
end
if(i>0 && pos>=@min_position && pos<=@max_position)
filteredArray << pos
end
i +=1
end
return filteredArray
end
# Extract un suffix from suffix file based on md5 match
def extractSuffix(start,len)
sequence = ''
begin
file = File.new(@file_suffix+FILE_SUFFIX_EXT, "r")
file.pos = start
sequence = file.read(len)
file.close
rescue => err
puts "Exception: #{err}"
return nil
end
return sequence
end
# Iterates over matches
def next
if(@curmatch<@matches.length)
@curmatch = @curmatch + 1
return @matches[@curmatch-1]
else
@curmatch = 0
return nil
end
end
def to_pos
positions = Hash.new
@matches.each do |match|
# match = Array[md5val, errors, posArray]
i=0
len = 0
match[2].each do |pos|
if(i==0)
len = pos
else
if(positions.has_key?(pos))
posmatch = positions[pos]
posmatch << Array[len,match[1]]
else
posmatch = Array.new
posmatch << Array[len,match[1]]
positions[pos] = posmatch
end
end
i += 1
end
end
return positions.sort
end
def to_s
puts '{ matches: "' << @matches.length << '" }'
end
private
# If cache is used, store results for later retrieval, else return matches directly
def cache?(results)
if(@useCache)
@cache.saveCache(results)
end
return results
end
# Update cache object with current object parameters
# * method: 0 -> exact, 1 -> hamming, 2 -> edit
def updateCache(method,errors)
@cache.file_suffix = @file_suffix
@cache.min_position = @min_position
@cache.max_position = @max_position
@cache.method = method
@cache.errors = errors
end
# check if md5 is equal to pattern
def isMatchEqual?(s)
if(@pattern == s)
return true
end
return false
end
# check if string is approximatly equal to pattern
# s: string to compare
# pattern: base pattern used
# useHamming: use Hamming or edit distance
# edit : allowed errors
def isApproximateEqual?(s,pattern,useHamming,edit)
errors = -1
s.extend(Cassiopee)
if(useHamming)
if(@useAmbiguity && @ambiguous!=nil)
errors = s.computeHammingAmbiguous(pattern,edit,@ambiguous)
else
errors = s.computeHamming(pattern,edit)
end
else
if(@useAmbiguity && @ambiguous!=nil)
errors = s.computeLevenshteinAmbiguous(pattern,edit,@ambigous)
else
errors = s.computeLevenshtein(pattern,edit)
end
end
end
# Parse input string
#
# * creates a suffix file
# * creates a suffix position file
def parseSuffixes(s,minlen,maxlen,edit=0,pat=nil)
# Controls
if(minlen<=0)
minlen = 1
end
if(maxlen>@sequence.length)
maxlen = @sequence.length
end
if(!use_store)
minpos = @min_position
if(@max_position==0)
maxpos = @sequence.length
else
maxpos = @max_position
end
else
minpos = 0
maxpos = @sequence.length - minlen
end
suffixlen = nil
$log.info('Start indexing')
loaded = false
# Hash in memory already contain suffixes for searched lengths
if(@suffixes != nil && !@suffixes.empty?)
suffixlen = @suffixes[SUFFIXLEN]
if(suffixlen!=nil && !suffixlen.empty?)
loaded = true
(maxlen).downto(minlen) do |len|
if(suffixlen.index(len)==nil)
loaded = false
break
end
end
end
end
if(@use_store && loaded)
$log.debug('already in memory, skip file loading')
end
# If not already in memory
if(@use_store && !loaded)
@suffixes = loadSuffixes(@file_suffix+FILE_SUFFIX_POS)
suffixlen = @suffixes[SUFFIXLEN]
end
nbSuffix = 0
changed = false
# Load suffix between maxlen and minlen
(maxlen).downto(minlen) do |i|
$log.debug('parse for length ' << i.to_s)
if(suffixlen!=nil && suffixlen.index(i)!=nil)
$log.debug('length '<<i <<'already parsed')
next
end
changed = true
prev_progress = -1
(minpos..(maxpos)).each do |j|
# if position+length longer than sequence length, skip it
if(j+i>@sequence.length)
next
end
@suffix = s[j,i]
@suffixmd5 = Digest::MD5.hexdigest(@suffix)
@position = j
progress = (@position * 100).div(@sequence.length)
if((progress % 10) == 0 && progress > prev_progress)
prev_progress = progress
$log.debug("progress: " << progress.to_s)
end
if(method==METHOD_DIRECT)
if(edit==0 && !@useAmbiguity)
if(isMatchEqual?(@suffixmd5))
errors = 0
else
errors = -1
end
else
if(edit>=0)
useHamming = true
allowederrors = edit
else
useHamming = false
allowederrors = edit * (-1)
end
errors = isApproximateEqual?(@suffix,pat,useHamming,allowederrors)
end
if(errors>=0)
match = Array[@suffixmd5, errors, Array[i,j]]
$log.debug "Match: " << match.inspect
@matches << match
end
else
nbSuffix += addSuffix(@suffixmd5, @position,i)
end
end
$log.debug("Nb suffix found: " << nbSuffix.to_s << ' for length ' << i.to_s) unless method==METHOD_DIRECT
end
if(@use_store && changed)
$log.info("Store suffixes")
marshal_dump = Marshal.dump(@suffixes)
sfxpos = File.new(@file_suffix+FILE_SUFFIX_POS,'w')
sfxpos = Zlib::GzipWriter.new(sfxpos)
sfxpos.write marshal_dump
sfxpos.close
end
$log.info('End of indexing')
end
# Add a suffix in Hashmap
def addSuffix(md5val,position,len)
if(@suffixes.has_key?(md5val))
# Add position
@suffixes[md5val] << position
else
# Add position, write new suffix
# First elt is size of elt
@suffixes[md5val] = Array[len, position]
if(@suffixes.has_key?(SUFFIXLEN))
@suffixes[SUFFIXLEN] << len
else
@suffixes[SUFFIXLEN] = Array[len]
end
end
return 1
end
# read input string, and concat content
def readSequence(s)
$log.debug('read input sequence')
counter = 1
sequence = ''
begin
file = File.new(s, "r")
File.delete(@file_suffix+FILE_SUFFIX_EXT) unless !File.exists?(@file_suffix+FILE_SUFFIX_EXT)
File.open(@file_suffix+FILE_SUFFIX_EXT, 'w') do |data|
while (line = file.gets)
counter = counter + 1
input = line.downcase.chomp
skip = false
comments.each do |c|
if(input[0] == c[0])
# Line start with a comment char, skip it
$log.debug("skip line")
skip = true
break
end
end
if(!skip)
sequence << input
data.puts input
end
end
end
file.close
rescue => err
puts "Exception: #{err}"
err
end
$log.debug('data file created')
return sequence
end
# Load suffix position file in memory
def loadSuffixes(file_name)
return Hash.new unless File.exists?(@file_suffix+FILE_SUFFIX_POS)
begin
file = Zlib::GzipReader.open(file_name)
rescue Zlib::GzipFile::Error
file = File.open(file_name, 'r')
ensure
obj = Marshal.load file.read
file.close
return obj
end
end
# Filter @matches to keep only the longest or the error less matches for a same start position
def filterOptimal(type)
positions = Hash.new
@matches.each do |match|
# match = Array[md5val, errors, posArray]
i=0
len = 0
match[2].each do |pos|
if(i==0)
len = pos
else
if(positions.has_key?(pos))
posmatch = positions[pos]
posmatch << Array[len,match[1],match[0]]
#positions[pos] << posmatch
else
posmatch = Array.new
posmatch << Array[len,match[1],match[0]]
positions[pos] = posmatch
end
end
i += 1
end
end
matchtoremove = Array.new
positions.each do |pos,posmatch|
optimal = nil
match = nil
count = 0
newoptimal = nil
newmatch = nil
(0..posmatch.length-1).each do |i|
solution = posmatch[i]
if(i==0)
if(type==0)
# length
optimal = solution[0]
else
# cost
optimal = solution[1]
end
match = solution[2].to_s
#count += 1
next
end
newmatch = solution[2].to_s
if(type==0)
# length
newoptimal = solution[0]
if(newoptimal.to_i>optimal.to_i)
optimal = newoptimal
matchtoremove << match
match = newmatch
else
matchtoremove << newmatch
end
else
# cost
newoptimal = solution[1]
if(newoptimal<optimal)
optimal = newoptimal
matchtoremove << match
match = newmatch
else
matchtoremove << newmatch
end
end
count += 1
end
end
newmatches = Array.new
@matches.each do |match|
found = false
matchtoremove.each do |item|
if(match[0]==item)
found = true
break
end
end
if(!found)
newmatches << match
end
end
@matches = newmatches
end
end
end
next
end
changed = true
prev_progress = -1
(minpos..(maxpos)).each do |j|
# if position+length longer than sequence length, skip it
if(j+i>@sequence.length)
next
end
@suffix = s[j,i]
@suffixmd5 = Digest::MD5.hexdigest(@suffix)
@position = j
progress = (@position * 100).div(@sequence.length)
if((progress % 10) == 0 && progress > prev_progress)
prev_progress = progress
$log.debug("progress: " << progress.to_s)
end
if(method==METHOD_DIRECT)
if(edit==0 && !@useAmbiguity)
if(isMatchEqual?(@suffixmd5))
errors = 0
else
errors = -1
end
else
if(edit>=0)
useHamming = true
allowederrors = edit
else
useHamming = false
allowederrors = edit * (-1)
end
errors = isApproximateEqual?(@suffix,pat,useHamming,allowederrors)
end
if(errors>=0)
match = Array[@suffixmd5, errors, Array[i,j]]
$log.debug "Match: " << match.inspect
@matches << match
end
else
nbSuffix += addSuffix(@suffixmd5, @position,i)
end
end
$log.debug("Nb suffix found: " << nbSuffix.to_s << ' for length ' << i.to_s) unless method==METHOD_DIRECT
end
if(@use_store && changed)
$log.info("Store suffixes")
marshal_dump = Marshal.dump(@suffixes)
sfxpos = File.new(@file_suffix+FILE_SUFFIX_POS,'w')
sfxpos = Zlib::GzipWriter.new(sfxpos)
sfxpos.write marshal_dump
sfxpos.close
end
$log.info('End of indexing')
end
# Add a suffix in Hashmap
def addSuffix(md5val,position,len)
if(@suffixes.has_key?(md5val))
# Add position
@suffixes[md5val] << position
else
# Add position, write new suffix
# First elt is size of elt
@suffixes[md5val] = Array[len, position]
if(@suffixes.has_key?(SUFFIXLEN))
@suffixes[SUFFIXLEN] << len
else
@suffixes[SUFFIXLEN] = Array[len]
end
end
return 1
end
# read input string, and concat content
def readSequence(s)
$log.debug('read input sequence')
counter = 1
sequence = ''
begin
file = File.new(s, "r")
File.delete(@file_suffix+FILE_SUFFIX_EXT) unless !File.exists?(@file_suffix+FILE_SUFFIX_EXT)
File.open(@file_suffix+FILE_SUFFIX_EXT, 'w') do |data|
while (line = file.gets)
counter = counter + 1
input = line.downcase.chomp
skip = false
comments.each do |c|
if(input[0] == c[0])
# Line start with a comment char, skip it
$log.debug("skip line")
skip = true
break
end
end
if(!skip)
sequence << input
data.puts input
end
end
end
file.close
rescue => err
puts "Exception: #{err}"
err
end
$log.debug('data file created')
return sequence
end
# Load suffix position file in memory
def loadSuffixes(file_name)
return Hash.new unless File.exists?(@file_suffix+FILE_SUFFIX_POS)
begin
file = Zlib::GzipReader.open(file_name)
rescue Zlib::GzipFile::Error
file = File.open(file_name, 'r')
ensure
obj = Marshal.load file.read
file.close
return obj
end
end
# Filter @matches to keep only the longest or the error less matches for a same start position
def filterOptimal(type)
positions = Hash.new
@matches.each do |match|
# match = Array[md5val, errors, posArray]
i=0
len = 0
match[2].each do |pos|
if(i==0)
len = pos
else
if(positions.has_key?(pos))
posmatch = positions[pos]
posmatch << Array[len,match[1],match[0]]
#positions[pos] << posmatch
else
posmatch = Array.new
posmatch << Array[len,match[1],match[0]]
positions[pos] = posmatch
end
end
i += 1
end
end
matchtoremove = Array.new
positions.each do |pos,posmatch|
optimal = nil
match = nil
count = 0
newoptimal = nil
newmatch = nil
(0..posmatch.length-1).each do |i|
solution = posmatch[i]
if(i==0)
if(type==0)
# length
optimal = solution[0]
else
# cost
optimal = solution[1]
end
match = solution[2].to_s
#count += 1
next
end
newmatch = solution[2].to_s
if(type==0)
# length
newoptimal = solution[0]
if(newoptimal.to_i>optimal.to_i)
optimal = newoptimal
matchtoremove << match
match = newmatch
else
matchtoremove << newmatch
end
else
# cost
newoptimal = solution[1]
if(newoptimal<optimal)
optimal = newoptimal
matchtoremove << match
match = newmatch
else
matchtoremove << newmatch
end
end
count += 1
end
end
newmatches = Array.new
@matches.each do |match|
found = false
matchtoremove.each do |item|
if(match[0]==item)
found = true
break
end
end
if(!found)
newmatches << match
end
end
@matches = newmatches
end
end
end
|