Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Commit 
							
							·
						
						001c319
	
1
								Parent(s):
							
							9215a83
								
Update code/add_structure.py
Browse files- code/add_structure.py +46 -16
    	
        code/add_structure.py
    CHANGED
    
    | @@ -5,6 +5,7 @@ import zlib | |
| 5 | 
             
            from xml.etree import ElementTree
         | 
| 6 | 
             
            from urllib.parse import urlparse, parse_qs, urlencode
         | 
| 7 | 
             
            import requests
         | 
|  | |
| 8 | 
             
            from requests.adapters import HTTPAdapter, Retry
         | 
| 9 | 
             
            from unipressed import IdMappingClient
         | 
| 10 | 
             
            """
         | 
| @@ -30,7 +31,10 @@ def get_pdb_ids_2(protein_id): | |
| 30 | 
             
                        data={"from": from_db, "to": to_db, "ids": ids},
         | 
| 31 | 
             
                    )
         | 
| 32 | 
             
                    check_response(request)
         | 
| 33 | 
            -
                     | 
|  | |
|  | |
|  | |
| 34 |  | 
| 35 | 
             
                def get_next_link(headers):
         | 
| 36 | 
             
                    re_next_link = re.compile(r'<(.+)>; rel="next"')
         | 
| @@ -40,17 +44,34 @@ def get_pdb_ids_2(protein_id): | |
| 40 | 
             
                            return match.group(1)
         | 
| 41 |  | 
| 42 | 
             
                def check_id_mapping_results_ready(job_id):
         | 
|  | |
| 43 | 
             
                    while True:
         | 
| 44 | 
            -
                         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 45 | 
             
                        check_response(request)
         | 
| 46 | 
             
                        j = request.json()
         | 
| 47 | 
            -
                         | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
             | 
| 52 | 
            -
                                 | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 53 | 
             
                        else:
         | 
|  | |
| 54 | 
             
                            return bool(j["results"] or j["failedIds"])
         | 
| 55 |  | 
| 56 | 
             
                def get_batch(batch_response, file_format, compressed):
         | 
| @@ -74,6 +95,7 @@ def get_pdb_ids_2(protein_id): | |
| 74 |  | 
| 75 | 
             
                def get_id_mapping_results_link(job_id):
         | 
| 76 | 
             
                    url = f"{API_URL}/idmapping/details/{job_id}"
         | 
|  | |
| 77 | 
             
                    request = session.get(url)
         | 
| 78 | 
             
                    check_response(request)
         | 
| 79 | 
             
                    return request.json()["redirectURL"]
         | 
| @@ -144,36 +166,44 @@ def get_pdb_ids_2(protein_id): | |
| 144 | 
             
                job_id = submit_id_mapping(
         | 
| 145 | 
             
                    from_db="UniProtKB_AC-ID", to_db="PDB", ids=protein_id
         | 
| 146 | 
             
                )
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 147 | 
             
                if check_id_mapping_results_ready(job_id):
         | 
| 148 | 
             
                    link = get_id_mapping_results_link(job_id)
         | 
| 149 | 
             
                    results = get_id_mapping_results_search(link)
         | 
| 150 | 
            -
                     | 
| 151 | 
            -
             | 
| 152 | 
            -
                     | 
| 153 | 
            -
             | 
| 154 | 
            -
                return [i['to'] for i in results['results']]
         | 
| 155 | 
             
            def get_pdb_ids(protein_id):
         | 
| 156 | 
             
                try:
         | 
| 157 | 
             
                    request = IdMappingClient.submit(
         | 
| 158 | 
             
                        source="UniProtKB_AC-ID", dest="PDB", ids={protein_id})
         | 
|  | |
| 159 | 
             
                    try:
         | 
| 160 | 
             
                        pdb_list = list(request.each_result())
         | 
|  | |
| 161 | 
             
                        return [i['to'] for i in pdb_list]
         | 
| 162 | 
            -
                    except:
         | 
|  | |
| 163 | 
             
                        get_pdb_ids_2(protein_id)
         | 
| 164 | 
             
                except requests.exceptions.HTTPError:
         | 
|  | |
| 165 | 
             
                    get_pdb_ids_2(protein_id)
         | 
| 166 | 
             
                except KeyError:
         | 
|  | |
| 167 | 
             
                    get_pdb_ids_2(protein_id)
         | 
| 168 | 
             
            """
         | 
| 169 |  | 
| 170 | 
             
            def get_pdb_ids(protein_id):
         | 
| 171 | 
             
                try:
         | 
| 172 | 
             
                    request = IdMappingClient.submit(
         | 
| 173 | 
            -
             | 
| 174 | 
             
                    pdb_list = list(request.each_result())
         | 
| 175 | 
             
                    return [i['to'] for i in pdb_list]
         | 
| 176 | 
            -
             | 
| 177 | 
             
                except requests.exceptions.HTTPError:
         | 
| 178 | 
             
                    return  []
         | 
| 179 | 
             
                except unipressed.id_mapping.core.IdMappingError:
         | 
|  | |
| 5 | 
             
            from xml.etree import ElementTree
         | 
| 6 | 
             
            from urllib.parse import urlparse, parse_qs, urlencode
         | 
| 7 | 
             
            import requests
         | 
| 8 | 
            +
            import unipressed
         | 
| 9 | 
             
            from requests.adapters import HTTPAdapter, Retry
         | 
| 10 | 
             
            from unipressed import IdMappingClient
         | 
| 11 | 
             
            """
         | 
|  | |
| 31 | 
             
                        data={"from": from_db, "to": to_db, "ids": ids},
         | 
| 32 | 
             
                    )
         | 
| 33 | 
             
                    check_response(request)
         | 
| 34 | 
            +
                    if check_response != None:
         | 
| 35 | 
            +
                    	return request.json()["jobId"]
         | 
| 36 | 
            +
                    else:
         | 
| 37 | 
            +
                    	return None
         | 
| 38 |  | 
| 39 | 
             
                def get_next_link(headers):
         | 
| 40 | 
             
                    re_next_link = re.compile(r'<(.+)>; rel="next"')
         | 
|  | |
| 44 | 
             
                            return match.group(1)
         | 
| 45 |  | 
| 46 | 
             
                def check_id_mapping_results_ready(job_id):
         | 
| 47 | 
            +
                    print('entered')
         | 
| 48 | 
             
                    while True:
         | 
| 49 | 
            +
                        print('True')
         | 
| 50 | 
            +
                        print('HR-1')
         | 
| 51 | 
            +
                        try:
         | 
| 52 | 
            +
                            request = session.get(f"{API_URL}/idmapping/status/{job_id}")
         | 
| 53 | 
            +
                        except requests.exceptions.RetryError:
         | 
| 54 | 
            +
                            print('eneted')
         | 
| 55 | 
            +
                            request = None
         | 
| 56 | 
            +
                        print('HR0-22')
         | 
| 57 | 
             
                        check_response(request)
         | 
| 58 | 
             
                        j = request.json()
         | 
| 59 | 
            +
                        print('HR0')
         | 
| 60 | 
            +
                        try:
         | 
| 61 | 
            +
                            print('HR1')
         | 
| 62 | 
            +
                            if "jobStatus" in j:
         | 
| 63 | 
            +
                                print('HR2')
         | 
| 64 | 
            +
                                if j["jobStatus"] == "RUNNING":
         | 
| 65 | 
            +
                                    print(f"Retrying in {POLLING_INTERVAL}s")
         | 
| 66 | 
            +
                                    time.sleep(POLLING_INTERVAL)
         | 
| 67 | 
            +
                                else:
         | 
| 68 | 
            +
                                    print('HR3')
         | 
| 69 | 
            +
                                    raise Exception(j["jobStatus"])
         | 
| 70 | 
            +
                        except:
         | 
| 71 | 
            +
                            print('HR4')
         | 
| 72 | 
            +
                            requests.exceptions.RetryError
         | 
| 73 | 
             
                        else:
         | 
| 74 | 
            +
                            print('HR4')
         | 
| 75 | 
             
                            return bool(j["results"] or j["failedIds"])
         | 
| 76 |  | 
| 77 | 
             
                def get_batch(batch_response, file_format, compressed):
         | 
|  | |
| 95 |  | 
| 96 | 
             
                def get_id_mapping_results_link(job_id):
         | 
| 97 | 
             
                    url = f"{API_URL}/idmapping/details/{job_id}"
         | 
| 98 | 
            +
             | 
| 99 | 
             
                    request = session.get(url)
         | 
| 100 | 
             
                    check_response(request)
         | 
| 101 | 
             
                    return request.json()["redirectURL"]
         | 
|  | |
| 166 | 
             
                job_id = submit_id_mapping(
         | 
| 167 | 
             
                    from_db="UniProtKB_AC-ID", to_db="PDB", ids=protein_id
         | 
| 168 | 
             
                )
         | 
| 169 | 
            +
                print('skhfkh')
         | 
| 170 | 
            +
                print(submit_id_mapping(
         | 
| 171 | 
            +
                    from_db="UniProtKB_AC-ID", to_db="PDB", ids=protein_id
         | 
| 172 | 
            +
                ))
         | 
| 173 | 
            +
                print('nor', check_id_mapping_results_ready(job_id))
         | 
| 174 | 
             
                if check_id_mapping_results_ready(job_id):
         | 
| 175 | 
             
                    link = get_id_mapping_results_link(job_id)
         | 
| 176 | 
             
                    results = get_id_mapping_results_search(link)
         | 
| 177 | 
            +
                    return [i['to'] for i in results['results']]
         | 
| 178 | 
            +
                else:
         | 
| 179 | 
            +
                    print('no i am here')
         | 
| 180 | 
            +
                    return None
         | 
|  | |
| 181 | 
             
            def get_pdb_ids(protein_id):
         | 
| 182 | 
             
                try:
         | 
| 183 | 
             
                    request = IdMappingClient.submit(
         | 
| 184 | 
             
                        source="UniProtKB_AC-ID", dest="PDB", ids={protein_id})
         | 
| 185 | 
            +
             | 
| 186 | 
             
                    try:
         | 
| 187 | 
             
                        pdb_list = list(request.each_result())
         | 
| 188 | 
            +
                        time.sleep(1)
         | 
| 189 | 
             
                        return [i['to'] for i in pdb_list]
         | 
| 190 | 
            +
                    except unipressed.id_mapping.core.IdMappingError:
         | 
| 191 | 
            +
                        print('I AM HERE 1')
         | 
| 192 | 
             
                        get_pdb_ids_2(protein_id)
         | 
| 193 | 
             
                except requests.exceptions.HTTPError:
         | 
| 194 | 
            +
                    print('I AM HERE 2')
         | 
| 195 | 
             
                    get_pdb_ids_2(protein_id)
         | 
| 196 | 
             
                except KeyError:
         | 
| 197 | 
            +
                    print('I AM HERE 3')
         | 
| 198 | 
             
                    get_pdb_ids_2(protein_id)
         | 
| 199 | 
             
            """
         | 
| 200 |  | 
| 201 | 
             
            def get_pdb_ids(protein_id):
         | 
| 202 | 
             
                try:
         | 
| 203 | 
             
                    request = IdMappingClient.submit(
         | 
| 204 | 
            +
                        source="UniProtKB_AC-ID", dest="PDB", ids={protein_id})
         | 
| 205 | 
             
                    pdb_list = list(request.each_result())
         | 
| 206 | 
             
                    return [i['to'] for i in pdb_list]
         | 
|  | |
| 207 | 
             
                except requests.exceptions.HTTPError:
         | 
| 208 | 
             
                    return  []
         | 
| 209 | 
             
                except unipressed.id_mapping.core.IdMappingError:
         | 
