Example #1
0
def lookup_genome_alignment_index(index_fh, indexed_fh, out_fh=sys.stdout,
                                  key=None, verbose=False):
  """Load a GA index and its indexed file and extract one or more blocks.

  :param index_fh:   the index file to load. Can be a filename or a
                     stream-like object.
  :param indexed_fh: the file that the index was built for,
  :param key:        A single key, iterable of keys, or None. This key will be
                     used for lookup. If None, user is prompted to enter keys
                     interactively.
  """
  # load the genome alignment as a JIT object
  bound_iter = functools.partial(genome_alignment_iterator,
                                 reference_species="hg19", index_friendly=True)
  hash_func = JustInTimeGenomeAlignmentBlock.build_hash
  idx = IndexedFile(record_iterator=bound_iter, record_hash_function=hash_func)
  idx.read_index(index_fh, indexed_fh)

  if key is None:
    while key is None or key.strip() != "":
      sys.stderr.write("[WAITING FOR KEY ENTRY ON STDIN; " +
                       "END WITH EMPTY LINE]\n")
      key = raw_input()
      # we know keys for genome alignments have tabs as delims, so..
      key = '\t'.join(key.split()).strip()
      if key != "":
        out_fh.write(str(idx[key]) + "\n")
      sys.stderr.write("\n")
  else:
    # we know keys for genome alignments have tabs as delims, so..
    key = '\t'.join(key.split())
    out_fh.write(str(idx[key]) + "\n")
Example #2
0
def build_genome_alignment_from_file(ga_path, ref_spec, idx_path=None,
                                     verbose=False):
  """
  build a genome alignment by loading from a single MAF file.

  :param ga_path:  the path to the file to load.
  :param ref_spec: which species in the MAF file is the reference?
  :param idx_path: if provided, use this index to generate a just-in-time
                   genome alignment, instead of loading the file immediately.
  """
  blocks = []
  if (idx_path is not None):
    bound_iter = functools.partial(genome_alignment_iterator,
                                   reference_species=ref_spec)
    hash_func = JustInTimeGenomeAlignmentBlock.build_hash
    factory = IndexedFile(None, bound_iter, hash_func)
    factory.read_index(idx_path, ga_path, verbose=verbose)

    pind = None
    for k in factory:
      if verbose:
        if pind is None:
          total = len(factory)
          pind = ProgressIndicator(totalToDo=total, messagePrefix="completed",
                                   messageSuffix="building alignment blocks ")
        pind.done += 1
        pind.showProgress()
      blocks.append(JustInTimeGenomeAlignmentBlock(factory, k))
  else:
    for b in genome_alignment_iterator(ga_path, ref_spec, verbose=verbose):
      blocks.append(b)
  return GenomeAlignment(blocks, verbose)
Example #3
0
def lookup_genome_alignment_index(index_fh,
                                  indexed_fh,
                                  out_fh=sys.stdout,
                                  key=None,
                                  verbose=False):
    """Load a GA index and its indexed file and extract one or more blocks.

  :param index_fh:   the index file to load. Can be a filename or a
                     stream-like object.
  :param indexed_fh: the file that the index was built for,
  :param key:        A single key, iterable of keys, or None. This key will be
                     used for lookup. If None, user is prompted to enter keys
                     interactively.
  """
    # load the genome alignment as a JIT object
    bound_iter = functools.partial(genome_alignment_iterator,
                                   reference_species="hg19",
                                   index_friendly=True)
    hash_func = JustInTimeGenomeAlignmentBlock.build_hash
    idx = IndexedFile(record_iterator=bound_iter,
                      record_hash_function=hash_func)
    idx.read_index(index_fh, indexed_fh)

    if key is None:
        while key is None or key.strip() != "":
            sys.stderr.write("[WAITING FOR KEY ENTRY ON STDIN; " +
                             "END WITH EMPTY LINE]\n")
            key = raw_input()
            # we know keys for genome alignments have tabs as delims, so..
            key = '\t'.join(key.split()).strip()
            if key != "":
                out_fh.write(str(idx[key]) + "\n")
            sys.stderr.write("\n")
    else:
        # we know keys for genome alignments have tabs as delims, so..
        key = '\t'.join(key.split())
        out_fh.write(str(idx[key]) + "\n")
Example #4
0
def build_genome_alignment_from_file(ga_path,
                                     ref_spec,
                                     idx_path=None,
                                     verbose=False):
    """
  build a genome alignment by loading from a single MAF file.

  :param ga_path:  the path to the file to load.
  :param ref_spec: which species in the MAF file is the reference?
  :param idx_path: if provided, use this index to generate a just-in-time
                   genome alignment, instead of loading the file immediately.
  """
    blocks = []
    if (idx_path is not None):
        bound_iter = functools.partial(genome_alignment_iterator,
                                       reference_species=ref_spec)
        hash_func = JustInTimeGenomeAlignmentBlock.build_hash
        factory = IndexedFile(None, bound_iter, hash_func)
        factory.read_index(idx_path, ga_path, verbose=verbose)

        pind = None
        for k in factory:
            if verbose:
                if pind is None:
                    total = len(factory)
                    pind = ProgressIndicator(
                        totalToDo=total,
                        messagePrefix="completed",
                        messageSuffix="building alignment blocks ")
                pind.done += 1
                pind.showProgress()
            blocks.append(JustInTimeGenomeAlignmentBlock(factory, k))
    else:
        for b in genome_alignment_iterator(ga_path, ref_spec, verbose=verbose):
            blocks.append(b)
    return GenomeAlignment(blocks, verbose)