Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Faster indexing #456

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions sourmash/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from .logging import error, set_quiet

from .commands import (categorize, compare, compute, dump, import_csv,
gather, index, sbt_combine, search,
plot, watch, info, storage, migrate, multigather)
gather, index, sbt_combine, search, plot, watch,
info, storage, migrate, multigather, prepare)
from .lca import main as lca_main
from .sig import main as sig_main

Expand Down Expand Up @@ -64,7 +64,9 @@ def main():
'migrate': migrate,
'multigather': multigather,
'sig': sig_main,
'signature': sig_main}
'signature': sig_main,
'prepare': prepare
}
parser = argparse.ArgumentParser(
description='work with compressed biological sequence representations')
parser.add_argument('command', nargs='?')
Expand Down
48 changes: 47 additions & 1 deletion sourmash/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import os.path
import sys
import random
import shutil
import tempfile

import screed
from .sourmash_args import SourmashArgumentParser
Expand Down Expand Up @@ -753,7 +755,7 @@ def index(args):
scaleds.add(ss.minhash.scaled)

leaf = SigLeaf(ss.md5sum(), ss)
tree.add_node(leaf)
tree.add_node(leaf, update_internal=False)
n += 1

if not ss:
Expand Down Expand Up @@ -1402,3 +1404,47 @@ def migrate(args):

notify('saving SBT under "{}".', args.sbt_name)
tree.save(args.sbt_name, structure_only=True)


def prepare(args):
from .sbt import parse_backend_args
from .sbt_storage import FSStorage

parser = argparse.ArgumentParser()
parser.add_argument('sbt_name', help='name of SBT to prepare')
parser.add_argument('-x', help='new nodegraph size', default=1e5)
parser.add_argument('-b', "--backend", type=str,
help='Backend to convert to',
default='FSStorage')
args = parser.parse_args(args)

notify('saving SBT under "{}".', args.sbt_name)

backend, options = parse_backend_args(args.sbt_name, args.backend)

with backend(*options) as storage:
with open(args.sbt_name, 'r') as f:
import json
temptree = json.load(f)

if ((temptree['storage']['backend'] == 'IPFSStorage') and
(backend == FSStorage) and
('preload' in temptree['storage']['args'])):
# Let's take a shortcut... The preload multihash contains the
# directory in the same structure FSStorage expects.
ipfs_args = temptree['storage']['args']
multihash = ipfs_args.pop('preload')

# TODO: in case the IPFS node is not available, need to
# fallback to read-only client
import ipfsapi
client = ipfsapi.connect(**ipfs_args)

dirpath = os.path.join(storage.location, storage.subdir)
with tempfile.TemporaryDirectory() as temp:
client.get(multihash, filepath=temp)
shutil.rmtree(dirpath)
shutil.move(os.path.join(temp, multihash), dirpath)

sbt = load_sbt_index(args.sbt_name, print_version_warning=False)
sbt.save(args.sbt_name, storage=storage)
Loading