0

I am developing a workflow for a class I am taking. I am trying to leverage a tool which requires I upload two files and supply an email and a job name.

I have tried many suggestions from this site and read the documentation for requests but nothing seems to work.

This is my (messy) code:

import requests, sys, urllib3, json, os

START_URL = 'https://bmm.crick.ac.uk/~svc-bmm-swarmdock/submit.cgi'
EMAIL = 'someAddress@domain.com'


if len(sys.argv) != 3:
    print('Usage: -a receptor -a ligand')
    sys.exit(1)

'''
with open('./Swarm Dock/pdb/1asu.pdb') as rec_file:
    rec_contents = rec_file.read()
with open('./Swarm Dock/pdb/2msi.pdb') as lig_file:
    lig_contents = lig_file.read()
'''

print('cwd: {}'.format(os.getcwd()))

form_data = {
    'formSubmitBut': 'Submit New Job',
    'jobname': 'test1',
    'email': EMAIL
    } #, 'receptorfile': rec_contents, 'ligandfile': lig_contents}
files = {
    'receptorfile': (open('./Swarm Dock/pdb/1ASU.pdb', 'rb'), 'fakepath/1asu.pdb'), #'receptorfile': ('1ASU',open('./Swarm Dock/pdb/1ASU.pdb', 'rb'), 'plain/text'),
    'ligandfile': (open('./Swarm Dock/pdb/2MSI.pdb', 'rb'), 'fakepath/2msi.pdb')
}

response = requests.post(START_URL, files=files, data=form_data)
print(response.content)

Here is the output:

b'<!DOCTYPE html>\n<html>\n<head>\n   <meta name="robots" content="noarchive">\n   <meta name="description" content="SwarmDock Server">\n   <meta name="keywords" content ="SwarmDock, The Francis Crick Institute, Mieczyslaw Torchala, Iain Moal, Raphael Chaleil, Juan Fernandez-Recio, Paul Bates">\n   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link href=\'https://fonts.googleapis.com/css?family=Crafty+Girls\' rel=\'stylesheet\' type=\'text/css\'><link rel="stylesheet" type="text/css" href="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/sds.css" /><title>SwarmDock Server</title>\n   <script type="text/javascript">\n      function validateForm() {\n         var ea = document.forms["sdsForm"]["email"].value;\n         var jn = document.forms["sdsForm"]["jobname"].value;\n         var rf = document.forms["sdsForm"]["receptorfile"].value;\n         var lf = document.forms["sdsForm"]["ligandfile"].value;\n         var rm = document.forms["sdsForm"]["recnm"].value;\n         var lm = document.forms["sdsForm"]["lignm"].value;\n\t var eapattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z]{2,6}/;\n\t var jnpattern = /^[a-zA-Z0-9 ]/;\n         if (ea.length < 7) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Email address should be at least seven characters long</div><br />\';\n            return (false);\n         }\n         if (!ea.match(eapattern)) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Email address is not correct</div><br />\';\n            return (false);\n         }\n         if (jn.length < 3) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Job name should be at least three characters long</div><br />\';\n            return (false);\n         }\n         if (!jn.match(jnpattern)) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Job name contains characters which are not allowed</div><br />\';\n            return (false);\n         }\n         if (rf.length < 1) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Receptor file is empty or not chosen</div><br />\';\n            return (false);\n         }\n         if (lf.length < 1) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Ligand file is empty or not chosen</div><br />\';\n            return (false);\n         }\n         if ((rm.length < 1) || (rm.length > 25)) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Number of normal modes for receptor has to between 1 and 25</div><br />\';\n            return (false);\n         }\n         if ((lm.length < 1) || (lm.length > 25)) {\n            document.getElementById(\'jserrormsg\').innerHTML = \'<div class="message">Error: Number of normal modes for ligand has to between 1 and 25</div><br />\';\n            return (false);\n         }\n         return (true);\n      }\n      function isblind(type) {\n         if (type == \'blind\') {\n            document.sdsForm.formSubmitBut.value = \'Submit new job\';\n         } else {\n            document.sdsForm.formSubmitBut.value = \'Choose residues\';\n         }\n         return (true);\n      }\n      function confirmChosenFile(type) {\n\tif (type == \'rec\') {\n\t   document.getElementById(\'recfileconfirm\').innerHTML = document.sdsForm.receptorfile.value;\n        }\n        if (type == \'lig\') {\n\t   document.getElementById(\'ligfileconfirm\').innerHTML = document.sdsForm.ligandfile.value;\n        }\n      }\n   </script>\n<script src="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/jmol-latest/JSmol.min.js" type="text/javascript"></script>\n   <script type="text/javascript">\n   var myJmol1, myJmol2, myJmol3, myJmol4, myJmol5, myJmol6;\n   var Info = {\n        width: 600,\n        height: 600,\n        color: "white",\n        use: "JAVA HTML5",\n        j2sPath: "jmol-latest/j2s",\n\tjarPath: "jmol-latest/java",\n        disableJ2SLoadMonitor: true,\n        disableInitialConsole: true,\n        allowJavaScript: true\n   }; \n   </script>\n\n</head>\n\n<body>\n   <div id="container">\n      <div id="header">\n         <h1>SwarmDock Server</h1>\n         <h2>webservice for generating 3D structures of protein-protein complexes</h2>\n         <br />\n         <p id="menu"><a href="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/index.html">Introduction</a>&nbsp;|&nbsp;<a href="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/references.html">References</a>&nbsp;|&nbsp;<a href="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/algorithm.html">Algorithm</a>&nbsp;|&nbsp;<a href="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/submit.cgi">Submit new job</a>&nbsp;|&nbsp;<a href="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/checkStatus.cgi">Check job status</a></p>\n         <br />\n      </div>\n      <div id="content">\n<h2>Submit new job</h2>\n<form action="submit.cgi" name="sdsForm" method="post" enctype="multipart/form-data" onSubmit="return validateForm()">\n  Prepare two PDB files: the first one with ligand, the second one with receptor. <b>Files must have TER after each chain (also after last one). Only standard residues are allowed. Files with missing residues or missing atoms other than H are generally not allowed. However, we will try to model missing residues for you, as well as exchange nonstandard residues to standard ones.</b> If you do not know any residues belonging to receptor\'s or ligand\'s binding site, choose Blind docking. For docking with restraints we offer an additional interface prediction tool. Next click Submit button. When the job is completed, you will get an email message with further instructions. <br /><br />\n  <output id="jserrormsg"></output>\n  <label>Email address:</label><input name="email" type="text" class="w300" value="ser499webscraper@gmail.com" /><br />\n  <label>Job name:</label><input name="jobname" type="text" class="w300" value="test1" /><br /><br />\n  <label>Receptor PDB file:</label><input name="receptorfile" type="file" class="file" onChange="document.sdsForm.blind[1].disabled=false;confirmChosenFile(\'rec\');" /><br />Chosen receptor file: <b id="recfileconfirm"></b><br /><label>Number of normal modes:</label><select name="recnm"><option>1</option><option>2</option><option>3</option><option>4</option><option selected="selected">5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option></select><br /><br />\n  <label>Ligand PDB file:</label><input name="ligandfile" type="file" class="file" onChange="confirmChosenFile(\'lig\');" /><br />Chosen ligand file: <b id="ligfileconfirm"></b><br /><label>Number of normal modes:</label><select name="lignm"><option>1</option><option>2</option><option>3</option><option>4</option><option selected="selected">5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option></select><br /><br />\n  <label>Docking type:</label><input type="radio" name="blind" value="1" class="radio" checked="checked" onClick="isblind(\'blind\')"/>Full blind<input type="radio" name="blind" value="0" class="radio" disabled="disabled" onClick="isblind(\'none\')" />I want to choose interface residues<br />\n  <br />Please note that computations may take <b>up to few days</b>. Execution time depends not only on the size of the structure, but also on computational resources available. If you don\'t receive a response from the server <b>within one week</b> please inform us - we will check the status of your job. The solutions will be deleted one week after you receive the results.<br /><br />\n<input class="submitbutton" name="formSubmitBut" type="submit" value="Submit new job" /><br />\n</form>\n      </div>\n      <div id="footer">\n\t Version: 15.04.01<br /><br />\n         <a href="https://www.crick.ac.uk/"><img src="https://bmm.crick.ac.uk/~svc-bmm-swarmdock/crick.jpg" alt="" /></a>\n      </div>\n   </div>\n</body>\n</html>\n'

As you can see in the output the jobname and email fields are being filled in correctly. I do not understand why the output with id='jserrormsg', id='ligfileconfirm' and id='recfileconfirm' are not displaying any values. It seems like the form isnt being submitted for some reason.

I have tried:

form_data = { 
    'formSubmitBut': 'Submit New Job', 
    'jobname': 'test1', 
    'email': EMAIL }
files = { 
    'receptorfile': open('./Swarm Dock/pdb/1ASU.pdb', 'rb'),
    'ligandfile': open('./Swarm Dock/pdb/2MSI.pdb', 'rb')
}

response = requests.post(START_URL, files=files, data=form_data) 

and

form_data = {
    'formSubmitBut': 'Submit New Job',
    'jobname': 'test1',
    'email': EMAIL
    }
files = {
    'receptorfile': ('1asu.pdb', open('./Swarm Dock/pdb/1ASU.pdb', 'rb'), 'application/vnd.palm', {'Expires': '0'}),
    'ligandfile': ('2msi.pdb', open('./Swarm Dock/pdb/2MSI.pdb', 'rb'), 'application/vnd.palm', {'Expires': '0'})
}

response = requests.post(START_URL, files=files, data=form_data)
print(response.content)

and

import requests, sys, urllib3, json, os
from requests_toolbelt.multipart.encoder import MultipartEncoder

multipart_data = MultipartEncoder(
    fields={
        'receptorfile': ('1asu.pdb', open('./Swarm Dock/pdb/1ASU.pdb', 'rb'), 'text/plain', {'Expires': '0'}),
        'ligandfile': ('2msi.pdb', open('./Swarm Dock/pdb/2MSI.pdb', 'rb'), 'text/plain', {'Expires': '0'}),
        'jobname': 'test1',
        'email': EMAIL
    }
)

response = requests.post(START_URL, data=multipart_data, headers={'Content-Type': multipart_data.content_type})

print('Status Code: {}'.format(response.status_code))
print(response.content)

The response code is 200 and output is the same as above.

King T
  • 1
  • 2
  • here is the right format to send files http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file – Pavan Kumar T S Mar 08 '18 at 18:50
  • I have tried using: form_data = { 'formSubmitBut': 'Submit New Job', 'jobname': 'test1', 'email': EMAIL } #, 'receptorfile': rec_contents, 'ligandfile': lig_contents} files = { 'receptorfile': open('./Swarm Dock/pdb/1ASU.pdb', 'rb'), 'ligandfile': open('./Swarm Dock/pdb/2MSI.pdb', 'rb'), } response = requests.post(START_URL, files=files, data=form_data) This doesnt seem to work either. – King T Mar 08 '18 at 18:54
  • did you receive same response or different – Pavan Kumar T S Mar 08 '18 at 19:11
  • The same response. Does it matter that the page is a cgi script? – King T Mar 08 '18 at 19:13
  • send content-type which holds file type – Pavan Kumar T S Mar 08 '18 at 19:23
  • files = {'file': ('report.pdb', open('report.pdb', 'rb'), 'application/vnd.palm', {'Expires': '0'})} – Pavan Kumar T S Mar 08 '18 at 19:26
  • I tried sending the content-type. I edited the question to display the code I used. I am getting the same response. I also tried sending it with content type text/plain – King T Mar 08 '18 at 19:35
  • can you check status code also and here is another method https://stackoverflow.com/a/35974071/7887883 – Pavan Kumar T S Mar 08 '18 at 19:42
  • The response code is 200 and the output is the same as above. – King T Mar 08 '18 at 19:59

0 Answers0