<p>So this is connected to how QMEAN is implemented as a server. It also could be true to many bioinformatics resources so I think this is important.</p>

<p>Posting to QMEAN "submit" triggers the job that is processed on our HPC cluster. That takes time and in the meantime JSON response just tells you that the job is QUEUEING/RUNNING or when it is ready it tells you COMPLETED. The answer suggested by you is the correct assuming you wish to wait for the job completion (which might take quite a lot).</p>

<p>So just check in while loop when the status is COMPLETED and fetch the result eg.</p>

<pre><code>import time
import json
import requests
def qmean_score(download_url):
    # Request the resource
    zscore = None
    while True:
        r = requests.get(download_url, verify=True)

        scores = json.dumps(r.json(), indent=4, sort_keys=True)
        jdata = json.loads(scores)
        if jdata["status"] == "COMPLETED":
            zscore = jdata['input_data']['models'][0]['global_scores']['qmean4']['zscore']
            break
        else:
            time.sleep(1000)
    return zscore
</code></pre>

CC BY-SA 4.0
2018-06-28T15:55:00.443Z
But this doesn’t differ between interactive and batch usage. OP just got lucky when running the code from an interactive session.
Konrad Rudolph
CC BY-SA 4.0
2018-07-11T12:52:03.197Z
It does in a sense that one can submit many targets, save them in eg. file and after a while, one can return to them and retrieve all the date with JSON. It is just a convenience API for people that would like to do it in the command line and not doing this by hand. It is also used in CASP and CAMEO.
guma44
CC BY-SA 4.0
2018-06-28T13:20:43.110Z
2018-06-28T13:20:43.110Z
guma44
<p>I want to use a script to evaluate a protein structure with QMEAN scoring.</p>

<p>I can't understand why the script suggested by SWISS-MODEL (<a href="https://swissmodel.expasy.org/qmean/help#help_programmatic_access" rel="nofollow noreferrer">access to QMEAN by json object</a>) runs only if I put the commands on the shell. If I build a module, the result of the second request (request.get), return a QUEUEING status, not COMPLETE status.</p>

<p>Here is the code that I have been using:</p>

<pre><code>import json
import requests

def qmean_page(structure):
""" return the Qmean4 score from SWISSMODEL QMEAN tool"""

    url = "https://swissmodel.expasy.org/qmean/submit/"

    # Request the resource
    response = requests.post(url=url, data={"email": "XXX@XXX.XX"} ,files={"structure": open(structure,'r'),},timeout=3,verify=True)

    json.dumps(response.json(), indent=4, sort_keys=True)
    download_json = str(response.json()['download_url'])

    return download_json

def qmean_score(download_url):
# Request the resource
    r = requests.get(download_url, verify=True)

    scores = json.dumps(r.json(), indent=4, sort_keys=True)
    print scores
    jdata = json.loads(scores)
    zscore = jdata['input_data']['models'][0]['global_scores']['qmean4']['zscore']
    return zscore


s = 'P02489.B99990005.pdb'
url_d = qmean_page(s)
print url_d
score  = qmean_score(url_d)
print score
</code></pre>

<p><strong>CASE A:</strong> If I comment the last 5 line, I can import qmean.py (the function file) in python terminal. </p>

<pre><code>&gt;&gt;&gt; import qmean
&gt;&gt;&gt; s = 'P02489.B99990005.pdb'
&gt;&gt;&gt; url_d = qmean.qmean_page(s)
&gt;&gt;&gt; print url_d
https://swissmodel.expasy.org/qmean/project/TgEjha.json
&gt;&gt;&gt; score  = qmean.qmean_score(url_d)
{
"input_data": {
    "info": {
        "model_001": {
            "chain_mapping": {
                " ": "A"
            }
        }
    }, 
    "meta": {
        "created": "2018-01-18T10:04:29.978", 
        "email": "XXX@XXX.XX", 
        "project_name": "Project", 
        "seqres_uploaded": false
    }, 
    "method": "QMEANDisCo", 
    "models": [
        {
            "error_status": null, 
            "global_scores": {
                "acc_agreement": {
                    "norm": 0.5606936416184971, 
                    "zscore": -1.0459143647477258
                }, 
                "all_atom": {
                    "norm": -0.012626634534455788, 
                    "zscore": -2.1825427852787604
                }, 
                "cbeta": {
                    "norm": -0.004260555683274807, 
                    "zscore": -1.9157144117276892
                }, 
                "qmean4": {
                    "norm": 0.6275190794418422, 
                    "zscore": -3.5006904416158258
                }, 
                "qmean6": {
                    "norm": 0.6339071011931049, 
                    "zscore": -3.0507104571131918
                }, 
                "solvation": {
                    "norm": -0.609099435253941, 
                    "zscore": -1.2499924754236265
                }, 
                "ss_agreement": {
                    "norm": 0.6706229586994028, 
                    "zscore": 0.05371252044245716
                }, 
                "torsion": {
                    "norm": 0.0033300496230023145, 
                    "zscore": -2.7188811736675764
                }
            }, 
            "model_pdb": "https://swissmodel.expasy.org/qmean/project/TgEjha/model_001.pdb", 
            "modelid": "model_001", 
            "name": "P02489.B99990005.pdb", 
            "seqres": [
                {
                    "atomseq": "MDVTIQHPWFKRTLGPFYPSRLFDQFFGEGLFEYDLLPFLSSTISPYYRQSLFRTVLDSGISEVRSDRDKFVIFLDVKHFSPEDLTVKVQDDFVEIHGKHNERQDDHGYISREFHRRYRLPSNVDQSALSCSLSADGMLTFCGPKIQTGLDATHAERAIPVSREEKPTSAPSS", 
                    "chain_name": " ", 
                    "name": "seq_chain_0", 
                    "sequence": "MDVTIQHPWFKRTLGPFYPSRLFDQFFGEGLFEYDLLPFLSSTISPYYRQSLFRTVLDSGISEVRSDRDKFVIFLDVKHFSPEDLTVKVQDDFVEIHGKHNERQDDHGYISREFHRRYRLPSNVDQSALSCSLSADGMLTFCGPKIQTGLDATHAERAIPVSREEKPTSAPSS"
                }
            ]
        }
    ], 
    "options": {
        "qmeanbrane": false, 
        "qmeandisco": true
    }, 
    "project_type": "default", 
    "sequences": []
}, 
"status": "COMPLETED"
}
&gt;&gt;&gt; 
</code></pre>

<p><strong>CASE B:</strong> If I uncomment the last 5 lines and run 'python qmean.py', it doesn't work.</p>

<pre><code>  andrea$ python qmean.py
  https://swissmodel.expasy.org/qmean/project/zrcLCK.json
  {
  "status": "QUEUEING"
  }
  Traceback (most recent call last):
  File "qmean.py", line 31, in &lt;module&gt;
  score  = qmean_score(url_d)
  File "qmean.py", line 24, in qmean_score
  zscore = jdata['input_data']['models'][0]['global_scores']['qmean4']['zscore']
  KeyError: 'input_data'
</code></pre>

<p>If I print 'r' variable of 'qmean_score' function, It returns:</p>

<pre><code>  &lt;Response [200]&gt;
  {
  "status": "QUEUEING"
  }
</code></pre>

CC BY-SA 3.0
2018-01-18T09:23:36.720Z
You need to use `if __main__:` if you want to use that file as a module and in command line. But what is your question? You seem to have solved your problems.
llrs
CC BY-SA 3.0
2018-01-18T13:34:17.560Z
No, it is not solved because I could have a result like case A also when I call my function as case B
Andrea Spinelli
CC BY-SA 3.0
2018-01-22T16:14:40.300Z
I resolve the problem by using a while loop.
Andrea Spinelli
CC BY-SA 3.0
2018-02-19T10:41:44.253Z
maybe you could post your answer and accept it, for people trying to figure out what was the issue, or delete your question if you think that it won't be useful for other people.
aechchiki
<python>
QMean standalone evaluation
# QMean standalone evaluation

(Asked by: username_0 on 2018-01-18)

I want to use a script to evaluate a protein structure with QMEAN scoring.
I can't understand why the script suggested by SWISS-MODEL (access to QMEAN by json object) runs only if I put the commands on the shell. If I build a module, the result of the second request (request.get), return a QUEUEING status, not COMPLETE status.
Here is the code that I have been using:
import json
import requests

def qmean_page(structure):
""" return the Qmean4 score from SWISSMODEL QMEAN tool"""

    url = "https://swissmodel.expasy.org/qmean/submit/"

    # Request the resource
    response = requests.post(url=url, data={"email": "XXX@XXX.XX"} ,files={"structure": open(structure,'r'),},timeout=3,verify=True)

    json.dumps(response.json(), indent=4, sort_keys=True)
    download_json = str(response.json()['download_url'])

    return download_json

def qmean_score(download_url):
# Request the resource
    r = requests.get(download_url, verify=True)

    scores = json.dumps(r.json(), indent=4, sort_keys=True)
    print scores
    jdata = json.loads(scores)
    zscore = jdata['input_data']['models'][0]['global_scores']['qmean4']['zscore']
    return zscore


s = 'P02489.B99990005.pdb'
url_d = qmean_page(s)
print url_d
score  = qmean_score(url_d)
print score

CASE A: If I comment the last 5 line, I can import qmean.py (the function file) in python terminal. 
>>> import qmean
>>> s = 'P02489.B99990005.pdb'
>>> url_d = qmean.qmean_page(s)
>>> print url_d
https://swissmodel.expasy.org/qmean/project/TgEjha.json
>>> score  = qmean.qmean_score(url_d)
{
"input_data": {
    "info": {
        "model_001": {
            "chain_mapping": {
                " ": "A"
            }
        }
    }, 
    "meta": {
        "created": "2018-01-18T10:04:29.978", 
        "email": "XXX@XXX.XX", 
        "project_name": "Project", 
        "seqres_uploaded": false
    }, 
    "method": "QMEANDisCo", 
    "models": [
        {
            "error_status": null, 
            "global_scores": {
                "acc_agreement": {
                    "norm": 0.5606936416184971, 
                    "zscore": -1.0459143647477258
                }, 
                "all_atom": {
                    "norm": -0.012626634534455788, 
                    "zscore": -2.1825427852787604
                }, 
                "cbeta": {
                    "norm": -0.004260555683274807, 
                    "zscore": -1.9157144117276892
                }, 
                "qmean4": {
                    "norm": 0.6275190794418422, 
                    "zscore": -3.5006904416158258
                }, 
                "qmean6": {
                    "norm": 0.6339071011931049, 
                    "zscore": -3.0507104571131918
                }, 
                "solvation": {
                    "norm": -0.609099435253941, 
                    "zscore": -1.2499924754236265
                }, 
                "ss_agreement": {
                    "norm": 0.6706229586994028, 
                    "zscore": 0.05371252044245716
                }, 
                "torsion": {
                    "norm": 0.0033300496230023145, 
                    "zscore": -2.7188811736675764
                }
            }, 
            "model_pdb": "https://swissmodel.expasy.org/qmean/project/TgEjha/model_001.pdb", 
            "modelid": "model_001", 
            "name": "P02489.B99990005.pdb", 
            "seqres": [
                {
                    "atomseq": "MDVTIQHPWFKRTLGPFYPSRLFDQFFGEGLFEYDLLPFLSSTISPYYRQSLFRTVLDSGISEVRSDRDKFVIFLDVKHFSPEDLTVKVQDDFVEIHGKHNERQDDHGYISREFHRRYRLPSNVDQSALSCSLSADGMLTFCGPKIQTGLDATHAERAIPVSREEKPTSAPSS", 
                    "chain_name": " ", 
                    "name": "seq_chain_0", 
                    "sequence": "MDVTIQHPWFKRTLGPFYPSRLFDQFFGEGLFEYDLLPFLSSTISPYYRQSLFRTVLDSGISEVRSDRDKFVIFLDVKHFSPEDLTVKVQDDFVEIHGKHNERQDDHGYISREFHRRYRLPSNVDQSALSCSLSADGMLTFCGPKIQTGLDATHAERAIPVSREEKPTSAPSS"
                }
            ]
        }
    ], 
    "options": {
        "qmeanbrane": false, 
        "qmeandisco": true
    }, 
    "project_type": "default", 
    "sequences": []
}, 
"status": "COMPLETED"
}
>>> 

CASE B: If I uncomment the last 5 lines and run 'python qmean.py', it doesn't work.
  andrea$ python qmean.py
  https://swissmodel.expasy.org/qmean/project/zrcLCK.json
  {
  "status": "QUEUEING"
  }
  Traceback (most recent call last):
  File "qmean.py", line 31, in 
  score  = qmean_score(url_d)
  File "qmean.py", line 24, in qmean_score
  zscore = jdata['input_data']['models'][0]['global_scores']['qmean4']['zscore']
  KeyError: 'input_data'

If I print 'r' variable of 'qmean_score' function, It returns:
  
  {
  "status": "QUEUEING"
  }

**Tags:** python
> **Comments:**
> **username_1**: You need to use `if __main__:` if you want to use that file as a module and in command line. But what is your question? You seem to have solved your problems.  
> **username_0**: No, it is not solved because I could have a result like case A also when I call my function as case B  
> **username_0**: I resolve the problem by using a while loop.  
> **username_2**: maybe you could post your answer and accept it, for people trying to figure out what was the issue, or delete your question if you think that it won't be useful for other people.  

---

## 1 answers

### Answer by username_3 on 2018-06-28. Score: 2

So this is connected to how QMEAN is implemented as a server. It also could be true to many bioinformatics resources so I think this is important.
Posting to QMEAN "submit" triggers the job that is processed on our HPC cluster. That takes time and in the meantime JSON response just tells you that the job is QUEUEING/RUNNING or when it is ready it tells you COMPLETED. The answer suggested by you is the correct assuming you wish to wait for the job completion (which might take quite a lot).
So just check in while loop when the status is COMPLETED and fetch the result eg.
import time
import json
import requests
def qmean_score(download_url):
    # Request the resource
    zscore = None
    while True:
        r = requests.get(download_url, verify=True)

        scores = json.dumps(r.json(), indent=4, sort_keys=True)
        jdata = json.loads(scores)
        if jdata["status"] == "COMPLETED":
            zscore = jdata['input_data']['models'][0]['global_scores']['qmean4']['zscore']
            break
        else:
            time.sleep(1000)
    return zscore

> **Comments:**
> **username_4**: But this doesn’t differ between interactive and batch usage. OP just got lucky when running the code from an interactive session.  
> **username_3**: It does in a sense that one can submit many targets, save them in eg. file and after a while, one can return to them and retrieve all the date with JSON. It is just a convenience API for people that would like to do it in the command line and not doing this by hand. It is also used in CASP and CAMEO.  

---

