text: user1453: 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 <module>
    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:
    <Response [200]>
    {
    "status": "QUEUEING"
    }
  
  
  user3005: 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
  
  
original_text: 1453: <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>
   3005: <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>
  