Method: Bio::FlatFileIndex::Indexer.update_index

Defined in:
lib/bio/io/flatfile/indexer.rb

.update_index(name, parser, options, *files) ⇒ Object



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
# File 'lib/bio/io/flatfile/indexer.rb', line 621

def self.update_index(name, parser, options, *files)
  db = DataBank.open(name)

  if parser then
    raise 'file format mismatch' if db.format != parser.format
  else

    begin
      dbclass_orig =
        Bio::FlatFile.autodetect_file(db.fileids[0].filename)
    rescue TypeError, Errno::ENOENT
    end
    begin
      dbclass_new =
        Bio::FlatFile.autodetect_file(files[0])
    rescue TypeError, Errno::ENOENT
    end

    case db.format
    when 'swiss', 'embl'
      parser = Parser.new(db.format)
      if dbclass_new and dbclass_new != parser.dbclass
        raise 'file format mismatch'
      end
    when 'genbank'
      dbclass = dbclass_orig or dbclass_new
      if dbclass == Bio::GenBank or dbclass == Bio::GenPept
        parser = Parser.new(dbclass_orig)
      elsif !dbclass then
        raise 'cannnot determine format. please specify manually.'
      else
        raise 'file format mismatch'
      end
      if dbclass_new and dbclass_new != parser.dbclass
        raise 'file format mismatch'
      end
    else
      raise 'unsupported format'
    end
  end

  parser.set_primary_namespace(db.primary.name)
  parser.add_secondary_namespaces(*db.secondary.names)

  if options['renew'] then
    newfiles = db.fileids.filenames.find_all do |x|
      FileTest.exist?(x)
    end
    newfiles.concat(files)
    newfiles2 = newfiles.sort
    newfiles2.uniq!
    newfiles3 = []
    newfiles.each do |x|
      newfiles3 << x if newfiles2.delete(x)
    end
    t = db.index_type
    db.close
    case t
    when MAGIC_BDB
      Indexer::makeindexBDB(name, parser, options, *newfiles3)
    when MAGIC_FLAT
      Indexer::makeindexFlat(name, parser, options, *newfiles3)
    else
      raise 'Unsupported index type'
    end
    return true
  end

  need_update = []
  newfiles = files.dup
  db.fileids.cache_all
  db.fileids.each_with_index do |f, i|
    need_update << i unless f.check
    newfiles.delete(f.filename)
  end

  b = db.fileids.size
  begin
    db.fileids.recalc
  rescue Errno::ENOENT => evar
    DEBUG.print "Error: #{evar}\n"
    DEBUG.print "assumed --renew option\n"
    db.close
    options = options.dup
    options['renew'] = true
    update_index(name, parser, options, *files)
    return true
  end
  # add new files
  db.fileids.add(*newfiles)
  db.fileids.recalc

  need_update.concat((b...(b + newfiles.size)).to_a)

  DEBUG.print "writing DabaBank...\n"
  db.write('wb', BDBdefault::flag_append)

  case db.index_type
  when MAGIC_BDB
    addindex_bdb(db, BDBdefault::flag_append,
                 need_update, parser, options)
  when MAGIC_FLAT
    addindex_flat(db, :add, need_update, parser, options)
  else
    raise 'Unsupported index type'
  end

  db.close
  true
end