Exemple #1
0
def test_selected_targets_format(mock_open):

    targets = {'a': 'coffee',
               'b': 'tea'}

    alignments = {}
    for chain_id in targets:
        alignments[chain_id] = TargetTemplateAlignment('', '')
        alignments[chain_id].target_id = targets[chain_id]

    path = tempfile.mktemp()

    class FakeTarFile:
        def __enter__(self, *args, **kwargs):
            return self

        def __exit__(self, *args, **kwargs):
            pass

        def extractfile(self, *args, **kwargs):
            return open(path, 'r')

    mock_open.return_value = FakeTarFile()

    try:
        modeler._write_selected_targets(alignments, path)
        parsed = model_storage.extract_selected_targets('no.tar.gz')
    finally:
        if os.path.isfile(path):
            os.remove(path)

    eq_(parsed, targets)
Exemple #2
0
def get_metadata(job_id):

    """ 
    Get the metadata of the model, created by the modeling job.

    :param jobid: the job_id returned by 'submit'
    :return: The json metadata object. If the job status is not SUCCESS,
             this method returns an error.
    """

    from hommod.application import celery
    result = celery.AsyncResult(job_id)

    if result.status != 'SUCCESS':
        return jsonify({'error': "{} has status {}".format(job_id, result.status)}), 400

    try:
        path = result.get()
    except Exception:
        return jsonify({'error': result.traceback}), 500

    if path is None:
        message = 'Job %s finished, but without creating a model. This could be due to lack of a suitable template.' % job_id
        return jsonify({'error': message}), 500

    try:
        data = {}
        data['selected_targets'] = model_storage.extract_selected_targets(path)
        data['alignments'] = [alignment.as_dict()
                              for alignment in model_storage.extract_alignments(path)]

        return jsonify(data)
    except:
        return jsonify({'error': traceback.format_exc()}), 500
Exemple #3
0
def select_best_model(tar_paths):

    best_path = None
    best_pid = 0.0
    for tar_path in tar_paths:
        model_name = model_storage.get_model_name_from_path(tar_path)
        sequence_id = model_storage.get_sequence_id_from_name(model_name)

        pdb_str = model_storage.extract_model(tar_path)
        chain_order = parse_chain_order_from_string(pdb_str)

        chain_alignments = model_storage.extract_alignments(tar_path)

        if len(chain_order) != len(chain_alignments):
            _log.error("{}: {} chains in pdb file, vs. {} alignments in fasta"
                       .format(tar_path, len(chain_order), len(chain_alignments)))
            continue

        chain_targets = model_storage.extract_selected_targets(tar_path)
        chain_ids = list(filter(lambda chain_id: chain_targets[chain_id] == sequence_id,
                                chain_targets.keys()))
        if len(chain_ids) <= 0:
            _log.error("no main target {} in selected targets of {}".format(sequence_id, tar_path))
            continue

        if chain_ids[0] not in chain_order:
            _log.error("chain {} not in {}".format(chain_ids[0], tar_path))
            continue
        chain_i = chain_order.index(chain_ids[0])

        alignment = chain_alignments[chain_i]
        keys = list(alignment.aligned_sequences.keys())
        if len(keys) != 2:
            _log.error("alignment for chain {} in {} has too many keys: {}".format(chain_ids[0], tar_path, keys))
            continue

        pid = alignment.get_percentage_identity(keys[0], keys[1])
        if pid > best_pid:
            best_pid = pid
            best_path = tar_path

    return best_path
Exemple #4
0
def get_metadata_by_model_id(model_id):

    """
    Get the metadata of the model, created by the modeling job.

    :param model_id: the id returned by 'get_model_if_exists'
    :return: The json metadata object.
    """

    path = model_storage.get_tar_path_from_name(model_id)
    if not os.path.isfile(path):
        return jsonify({'error': "no such model"}), 400

    try:
        data = {}
        data['selected_targets'] = model_storage.extract_selected_targets(path)
        data['alignments'] = [alignment.as_dict()
                              for alignment in model_storage.extract_alignments(path)]

        return jsonify(data)
    except:
        return jsonify({'error': traceback.format_exc()}), 500
Exemple #5
0
def get_metadata_by_model_id(model_id):
    """
    Get the metadata of the model, created by the modeling job.

    :param model_id: the id returned by 'get_model_if_exists'
    :return: The json metadata object.
    """

    path = model_storage.get_tar_path_from_name(model_id)
    if not os.path.isfile(path):
        return jsonify({'error': "no such model"}), 400

    try:
        data = {}
        data['selected_targets'] = model_storage.extract_selected_targets(path)
        data['alignments'] = [
            alignment.as_dict()
            for alignment in model_storage.extract_alignments(path)
        ]

        return jsonify(data)
    except:
        return jsonify({'error': traceback.format_exc()}), 500
Exemple #6
0
def select_best_model(tar_paths):

    best_path = None
    best_pid = 0.0
    for tar_path in tar_paths:
        model_name = model_storage.get_model_name_from_path(tar_path)
        sequence_id = model_storage.get_sequence_id_from_name(model_name)

        pdb_str = model_storage.extract_model(tar_path)
        chain_order = parse_chain_order_from_string(pdb_str)

        chain_alignments = model_storage.extract_alignments(tar_path)

        chain_targets = model_storage.extract_selected_targets(tar_path)
        chain_ids = list(filter(lambda chain_id: chain_targets[chain_id] == sequence_id,
                                chain_targets.keys()))
        if len(chain_ids) <= 0:
            _log.error("no main target {} in selected targets of {}".format(sequence_id, tar_path))
            continue

        chain_i = chain_order.index(chain_ids[0])
        if chain_i == -1:
            _log.error("chain {} not in {}".format(chain_ids[0], tar_path))
            continue

        alignment = chain_alignments[chain_i]
        keys = alignment.aligned_sequences.keys()
        if len(keys) != 2:
            _log.error("alignment for chain {} in {} has too many keys: {}".format(chain_ids[0], tar_path, keys))
            continue

        pid = alignment.get_percentage_identity(keys[0], keys[1])
        if pid > best_pid:
            best_pid = pid
            best_path = tar_path

    return best_path
Exemple #7
0
def get_metadata(job_id):
    """ 
    Get the metadata of the model, created by the modeling job.

    :param jobid: the job_id returned by 'submit'
    :return: The json metadata object. If the job status is not SUCCESS,
             this method returns an error.
    """

    from hommod.application import celery
    result = celery.AsyncResult(job_id)

    if result.status != 'SUCCESS':
        return jsonify(
            {'error': "{} has status {}".format(job_id, result.status)}), 400

    try:
        path = result.get()
    except Exception:
        return jsonify({'error': result.traceback}), 500

    if path is None:
        message = 'Job %s finished, but without creating a model. This could be due to lack of a suitable template.' % job_id
        return jsonify({'error': message}), 500

    try:
        data = {}
        data['selected_targets'] = model_storage.extract_selected_targets(path)
        data['alignments'] = [
            alignment.as_dict()
            for alignment in model_storage.extract_alignments(path)
        ]

        return jsonify(data)
    except:
        return jsonify({'error': traceback.format_exc()}), 500