Mercurial > repos > proteore > proteore_data_manager
comparison data_manager/resource_building.py @ 60:da9e74d3c40d draft
"planemo upload commit a69fdb4cdc110c75fea7439f0d97b67158c1bbbf"
| author | proteore |
|---|---|
| date | Tue, 09 Jun 2020 13:19:26 +0000 |
| parents | 149eb9e80717 |
| children | add6aa698fb0 |
comparison
equal
deleted
inserted
replaced
| 59:8e60ad16028a | 60:da9e74d3c40d |
|---|---|
| 278 ftp.cwd(ftp_dir) | 278 ftp.cwd(ftp_dir) |
| 279 ftp.retrbinary("RETR " + file, open(path, 'wb').write) | 279 ftp.retrbinary("RETR " + file, open(path, 'wb').write) |
| 280 ftp.quit() | 280 ftp.quit() |
| 281 return (path) | 281 return (path) |
| 282 | 282 |
| 283 def download_from_nextprot_ftp(file,target_directory) : | 283 def download_from_nextprot_ftp(file,target_directory): |
| 284 ftp_dir = "pub/current_release/ac_lists/" | 284 ftp_dir = "pub/current_release/ac_lists/" |
| 285 path = os.path.join(target_directory, file) | 285 path = os.path.join(target_directory, file) |
| 286 ftp = ftplib.FTP("ftp.nextprot.org") | 286 ftp = ftplib.FTP("ftp.nextprot.org") |
| 287 ftp.login("anonymous", "anonymous") | 287 ftp.login("anonymous", "anonymous") |
| 288 ftp.cwd(ftp_dir) | 288 ftp.cwd(ftp_dir) |
| 289 ftp.retrbinary("RETR " + file, open(path, 'wb').write) | 289 ftp.retrbinary("RETR " + file, open(path, 'wb').write) |
| 290 ftp.quit() | 290 ftp.quit() |
| 291 return (path) | 291 return path |
| 292 | 292 |
| 293 def id_list_from_nextprot_ftp(file,target_directory) : | 293 def id_list_from_nextprot_ftp(file) : |
| 294 ftp_dir = "pub/current_release/ac_lists/" | |
| 295 path = os.path.join(target_directory, file) | |
| 296 ftp = ftplib.FTP("ftp.nextprot.org") | 294 ftp = ftplib.FTP("ftp.nextprot.org") |
| 297 ftp.login("anonymous", "anonymous") | 295 ftp.login("anonymous", "anonymous") |
| 298 ftp.cwd(ftp_dir) | 296 r = StringIO() |
| 299 ftp.retrbinary("RETR " + file, open(path, 'wb').write) | 297 ftp.retrlines("RETR " + file, lambda line: r.write(line + '\n')) |
| 300 ftp.quit() | 298 ftp.quit() |
| 301 with open(path,'r') as nextprot_ids : | 299 r.seek(0) |
| 302 nextprot_ids = nextprot_ids.read().splitlines() | 300 ids = r.readlines() |
| 303 return (nextprot_ids) | 301 ids = [id.strip('\n') for id in ids] |
| 302 | |
| 303 return (ids) | |
| 304 | 304 |
| 305 #return '' if there's no value in a dictionary, avoid error | 305 #return '' if there's no value in a dictionary, avoid error |
| 306 def access_dictionary (dico,key1,key2) : | 306 def access_dictionary (dico,key1,key2) : |
| 307 if key1 in dico : | 307 if key1 in dico : |
| 308 if key2 in dico[key1] : | 308 if key2 in dico[key1] : |
| 542 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_"+interactome+"_dictionaries") | 542 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_"+interactome+"_dictionaries") |
| 543 | 543 |
| 544 ####################################################################################################### | 544 ####################################################################################################### |
| 545 # 5. nextprot (add protein features) | 545 # 5. nextprot (add protein features) |
| 546 ####################################################################################################### | 546 ####################################################################################################### |
| 547 | |
| 548 def Build_nextprot_ref_file(data_manager_dict,target_directory): | 547 def Build_nextprot_ref_file(data_manager_dict,target_directory): |
| 549 nextprot_ids_file = "nextprot_ac_list_all.txt" | 548 |
| 550 ids = id_list_from_nextprot_ftp(nextprot_ids_file,target_directory) | 549 from requests_futures.sessions import FuturesSession |
| 550 from concurrent.futures import ProcessPoolExecutor | |
| 551 | |
| 552 #Get nextprot ids list | |
| 553 ids = id_list_from_nextprot_ftp("pub/current_release/ac_lists/nextprot_ac_list_all.txt") | |
| 551 | 554 |
| 552 output_file = 'nextprot_ref_'+ time.strftime("%d-%m-%Y") + ".tsv" | 555 output_file = 'nextprot_ref_'+ time.strftime("%d-%m-%Y") + ".tsv" |
| 553 path = os.path.join(target_directory,output_file) | 556 path = os.path.join(target_directory,output_file) |
| 554 name = "neXtProt release "+time.strftime("%d-%m-%Y") | 557 name = "neXtProt release "+time.strftime("%d-%m-%Y") |
| 555 release_id = "nextprot_ref_"+time.strftime("%d-%m-%Y") | 558 release_id = "nextprot_ref_"+time.strftime("%d-%m-%Y") |
| 556 | 559 |
| 560 #open output file to write | |
| 557 output = open(path, 'w') | 561 output = open(path, 'w') |
| 558 writer = csv.writer(output,delimiter="\t") | 562 writer = csv.writer(output,delimiter="\t") |
| 559 | 563 writer.writerow(["NextprotID","MW","SeqLength","IsoPoint","Chr","SubcellLocations","Diseases","TMDomains","ProteinExistence"]) |
| 560 nextprot_file=[["NextprotID","MW","SeqLength","IsoPoint","Chr","SubcellLocations","Diseases","TMDomains","ProteinExistence"]] | 564 |
| 561 writer.writerows(nextprot_file) | 565 subset=100 |
| 562 | 566 ids_subsets = [ids[x:x+subset] for x in range(0, len(ids), subset)] |
| 563 for id in ids : | 567 |
| 564 query="https://api.nextprot.org/entry/"+id+".json" | 568 for ids_subset in ids_subsets: |
| 565 try: | 569 |
| 566 resp = requests.get(url=query) | 570 #Open concurent sessions |
| 567 except : | 571 with FuturesSession(executor=ProcessPoolExecutor(max_workers=8)) as session: |
| 568 print ("wainting 1 hour before trying again") | 572 futures = [session.get("https://api.nextprot.org/entry/"+id+".json") for id in ids_subset] |
| 569 time.sleep(3600) | 573 |
| 570 resp = requests.get(url=query) | 574 for id,future in zip(ids_subset,futures) : |
| 571 data = resp.json() | 575 |
| 572 | 576 #Get json dictionary |
| 573 #get info from json dictionary | 577 try: |
| 574 mass_mol = data["entry"]["isoforms"][0]["massAsString"] | 578 res = future.result() |
| 575 seq_length = data['entry']["isoforms"][0]["sequenceLength"] | 579 except: |
| 576 iso_elec_point = data['entry']["isoforms"][0]["isoelectricPointAsString"] | 580 print ("sleep 1 hour") |
| 577 chr_loc = data['entry']["chromosomalLocations"][0]["chromosome"] | 581 time.sleep(3600) |
| 578 protein_existence = "PE"+str(data['entry']["overview"]['proteinExistence']['level']) | 582 res = future.result() |
| 579 | 583 data = res.json() |
| 580 #put all subcell loc in a set | 584 |
| 581 if "subcellular-location" in data['entry']["annotationsByCategory"].keys() : | 585 #get info from json dictionary |
| 582 subcell_locs = data['entry']["annotationsByCategory"]["subcellular-location"] | 586 mass_mol = data["entry"]["isoforms"][0]["massAsString"] |
| 583 all_subcell_locs = set() | 587 seq_length = data['entry']["isoforms"][0]["sequenceLength"] |
| 584 for loc in subcell_locs : | 588 iso_elec_point = data['entry']["isoforms"][0]["isoelectricPointAsString"] |
| 585 all_subcell_locs.add(loc['cvTermName']) | 589 chr_loc = data['entry']["chromosomalLocations"][0]["chromosome"] |
| 586 all_subcell_locs.discard("") | 590 protein_existence = "PE"+str(data['entry']["overview"]['proteinExistence']['level']) |
| 587 all_subcell_locs = ";".join(all_subcell_locs) | 591 |
| 588 else : | 592 #put all subcell loc in a set |
| 589 all_subcell_locs = "NA" | 593 if "subcellular-location" in data['entry']["annotationsByCategory"].keys() : |
| 590 | 594 subcell_locs = data['entry']["annotationsByCategory"]["subcellular-location"] |
| 591 #put all subcell loc in a set | 595 all_subcell_locs = set() |
| 592 if ('disease') in data['entry']['annotationsByCategory'].keys() : | 596 for loc in subcell_locs : |
| 593 diseases = data['entry']['annotationsByCategory']['disease'] | 597 all_subcell_locs.add(loc['cvTermName']) |
| 594 all_diseases = set() | 598 all_subcell_locs.discard("") |
| 595 for disease in diseases : | 599 all_subcell_locs = ";".join(all_subcell_locs) |
| 596 if (disease['cvTermName'] is not None and disease['cvTermName'] != ""): | 600 else : |
| 597 all_diseases.add(disease['cvTermName']) | 601 all_subcell_locs = "NA" |
| 598 if len(all_diseases) > 0 : all_diseases = ";".join(all_diseases) | 602 |
| 599 else : all_diseases="NA" | 603 #put all subcell loc in a set |
| 600 else : | 604 if ('disease') in data['entry']['annotationsByCategory'].keys() : |
| 601 all_diseases="NA" | 605 diseases = data['entry']['annotationsByCategory']['disease'] |
| 602 | 606 all_diseases = set() |
| 603 #get all tm domain | 607 for disease in diseases : |
| 604 nb_domains = 0 | 608 if (disease['cvTermName'] is not None and disease['cvTermName'] != ""): |
| 605 if "transmembrane-region" in data['entry']['annotationsByCategory'].keys(): | 609 all_diseases.add(disease['cvTermName']) |
| 606 tm_domains = data['entry']['annotationsByCategory']["transmembrane-region"] | 610 if len(all_diseases) > 0 : all_diseases = ";".join(all_diseases) |
| 607 all_tm_domains = set() | 611 else : all_diseases="NA" |
| 608 for tm in tm_domains : | 612 else : |
| 609 all_tm_domains.add(tm['cvTermName']) | 613 all_diseases="NA" |
| 610 nb_domains+=1 | 614 |
| 611 #print "nb domains ++" | 615 #get all tm domain |
| 612 #print (nb_domains) | 616 nb_domains = 0 |
| 613 nextprot_file[:] = [] | 617 if "transmembrane-region" in data['entry']['annotationsByCategory'].keys(): |
| 614 nextprot_file.append([id,mass_mol,str(seq_length),iso_elec_point,chr_loc,all_subcell_locs,all_diseases,str(nb_domains),protein_existence]) | 618 tm_domains = data['entry']['annotationsByCategory']["transmembrane-region"] |
| 615 writer.writerows(nextprot_file) | 619 all_tm_domains = set() |
| 616 | 620 for tm in tm_domains : |
| 617 id = str(10000000000 - int(time.strftime("%Y%m%d"))) | 621 all_tm_domains.add(tm['cvTermName']) |
| 622 nb_domains+=1 | |
| 623 | |
| 624 writer.writerow([id,mass_mol,str(seq_length),iso_elec_point,chr_loc,all_subcell_locs,all_diseases,str(nb_domains),protein_existence]) | |
| 625 | |
| 626 id = str(10000000000 - int(time.strftime("%Y%m%d"))) | |
| 618 | 627 |
| 619 data_table_entry = dict(id=id, release=release_id, name = name, value = path) | 628 data_table_entry = dict(id=id, release=release_id, name = name, value = path) |
| 620 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_nextprot_ref") | 629 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_nextprot_ref") |
| 621 | 630 |
| 622 ####################################################################################################### | 631 ####################################################################################################### |
