Mercurial > repos > dchristiany > data_manager_proteore
annotate data_manager/resource_building.py @ 14:f8ed6fc5f3ae draft
planemo upload commit 3787f45c66a9897be103392b34ce498ae193a803-dirty
author | dchristiany |
---|---|
date | Fri, 04 Jan 2019 06:11:49 -0500 |
parents | a1530507fee4 |
children | 83f57ba70416 |
rev | line source |
---|---|
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
1 """ |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
2 The purpose of this script is to create source files from different databases to be used in other proteore tools |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
3 """ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
4 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
5 import os, sys, argparse, requests, time, csv, re |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
6 from io import BytesIO |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
7 from zipfile import ZipFile |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
8 from galaxy.util.json import from_json_string, to_json_string |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
9 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
10 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
11 # General functions |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
12 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
13 def unzip(url, output_file): |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
14 """ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
15 Get a zip file content from a link and unzip |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
16 """ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
17 content = requests.get(url) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
18 zipfile = ZipFile(BytesIO(content.content)) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
19 output_content = "" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
20 output_content += zipfile.open(zipfile.namelist()[0]).read() |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
21 output = open(output_file, "w") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
22 output.write(output_content) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
23 output.close() |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
24 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
25 def _add_data_table_entry(data_manager_dict, data_table_entry,data_table): |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
26 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {}) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
27 data_manager_dict['data_tables'][data_table] = data_manager_dict['data_tables'].get(data_table, []) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
28 data_manager_dict['data_tables'][data_table].append(data_table_entry) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
29 return data_manager_dict |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
30 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
31 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
32 # 1. Human Protein Atlas |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
33 # - Normal tissue |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
34 # - Pathology |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
35 # - Full Atlas |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
36 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
37 def HPA_sources(data_manager_dict, tissue, target_directory): |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
38 if tissue == "HPA_normal_tissue": |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
39 tissue_name = "HPA normal tissue" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
40 url = "https://www.proteinatlas.org/download/normal_tissue.tsv.zip" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
41 elif tissue == "HPA_pathology": |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
42 tissue_name = "HPA pathology" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
43 url = "https://www.proteinatlas.org/download/pathology.tsv.zip" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
44 elif tissue == "HPA_full_atlas": |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
45 tissue_name = "HPA full atlas" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
46 url = "https://www.proteinatlas.org/download/proteinatlas.tsv.zip" |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
47 |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
48 output_file = tissue +"_"+ time.strftime("%d-%m-%Y") + ".tsv" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
49 path = os.path.join(target_directory, output_file) |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
50 unzip(url, path) #download and save file |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
51 tissue_name = tissue_name + " " + time.strftime("%d/%m/%Y") |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
52 tissue_id = tissue_name.replace(" ","_").replace("/","-") |
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
53 |
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
54 data_table_entry = dict(id=tissue_id, name = tissue_name, value = tissue, path = path) |
12
60cb0a5ae661
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
10
diff
changeset
|
55 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_protein_atlas") |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
56 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
57 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
58 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
59 # 2. Peptide Atlas |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
60 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
61 def peptide_atlas_sources(data_manager_dict, tissue, target_directory): |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
62 # Define PA Human build released number (here early 2018) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
63 atlas_build_id = "472" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
64 # Define organism_id (here Human) - to be upraded when other organism added to the project |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
65 organism_id = "2" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
66 # Extract sample_category_id and output filename |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
67 sample_category_id = tissue.split("-")[0] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
68 output_file = tissue.split("-")[1] +"_"+ time.strftime("%d-%m-%Y") + ".tsv" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
69 query = "https://db.systemsbiology.net/sbeams/cgi/PeptideAtlas/GetPeptides?atlas_build_id=" + \ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
70 atlas_build_id + "&display_options=ShowMappings&organism_id= " + \ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
71 organism_id + "&sample_category_id=" + sample_category_id + \ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
72 "&QUERY_NAME=AT_GetPeptides&output_mode=tsv&apply_action=QUERY" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
73 download = requests.get(query) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
74 decoded_content = download.content.decode('utf-8') |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
75 cr = csv.reader(decoded_content.splitlines(), delimiter='\t') |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
76 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
77 #build dictionary by only keeping uniprot accession (not isoform) as key and sum of observations as value |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
78 uni_dict = build_dictionary(cr) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
79 |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
80 #columns of data table peptide_atlas |
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
81 date = time.strftime("%d-%m-%Y") |
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
82 tissue = tissue.split("-")[1] |
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
83 tissue_id = tissue+"_"+date |
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
84 tissue_name = tissue_id.replace("-","/").replace("_"," ") |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
85 path = os.path.join(target_directory,output_file) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
86 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
87 with open(path,"wb") as out : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
88 w = csv.writer(out,delimiter='\t') |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
89 w.writerow(["Uniprot_AC","nb_obs"]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
90 w.writerows(uni_dict.items()) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
91 |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
92 data_table_entry = dict(id=tissue_id, name=tissue_name, value = path, tissue = tissue) |
12
60cb0a5ae661
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
10
diff
changeset
|
93 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_peptide_atlas") |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
94 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
95 #function to count the number of observations by uniprot id |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
96 def build_dictionary (csv) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
97 uni_dict = {} |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
98 for line in csv : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
99 if "-" not in line[2] and check_uniprot_access(line[2]) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
100 if line[2] in uni_dict : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
101 uni_dict[line[2]] += int(line[4]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
102 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
103 uni_dict[line[2]] = int(line[4]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
104 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
105 return uni_dict |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
106 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
107 #function to check if an id is an uniprot accession number : return True or False- |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
108 def check_uniprot_access (id) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
109 uniprot_pattern = re.compile("[OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
110 if uniprot_pattern.match(id) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
111 return True |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
112 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
113 return False |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
114 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
115 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
116 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
117 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
118 # 3. ID mapping file |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
119 ####################################################################################################### |
13
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
120 import ftplib, gzip, pickle |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
121 csv.field_size_limit(sys.maxsize) # to handle big files |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
122 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
123 def id_mapping_sources (data_manager_dict, species, target_directory) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
124 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
125 human = species == "human" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
126 species_dict = { "human" : "HUMAN_9606", "mouse" : "MOUSE_10090", "rat" : "RAT_10116" } |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
127 files=["idmapping_selected.tab.gz","idmapping.dat.gz"] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
128 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
129 #header |
13
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
130 if human : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
131 ids_list = ["UniProt-AC","UniProt-ID","GeneID","RefSeq","GI","PDB","GO","PIR","MIM","UniGene","Ensembl_Gene","Ensembl_Transcript","Ensembl_Protein","neXtProt","BioGrid","STRING","KEGG"] |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
132 else : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
133 ids_list = ["UniProt-AC","UniProt-ID","GeneID","RefSeq","GI","PDB","GO","PIR","MIM","UniGene","Ensembl_Gene","Ensembl_Transcript","Ensembl_Protein","BioGrid","STRING","KEGG"] |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
134 tab = [ids_list] |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
135 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
136 #print("header ok") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
137 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
138 #selected.tab and keep only ids of interest |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
139 selected_tab_file=species_dict[species]+"_"+files[0] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
140 tab_path = download_from_uniprot_ftp(selected_tab_file,target_directory) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
141 with gzip.open(tab_path,"rt") as select : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
142 tab_reader = csv.reader(select,delimiter="\t") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
143 for line in tab_reader : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
144 tab.append([line[i] for i in [0,1,2,3,4,5,6,11,13,14,18,19,20]]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
145 os.remove(tab_path) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
146 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
147 #print("selected_tab ok") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
148 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
149 """ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
150 Supplementary ID to get from HUMAN_9606_idmapping.dat : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
151 -NextProt,BioGrid,STRING,KEGG |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
152 """ |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
153 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
154 if human : ids = ['neXtProt','BioGrid','STRING','KEGG' ] #ids to get from dat_file |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
155 else : ids = ['BioGrid','STRING','KEGG' ] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
156 unidict = {} |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
157 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
158 #keep only ids of interest in dictionaries |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
159 dat_file=species_dict[species]+"_"+files[1] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
160 dat_path = download_from_uniprot_ftp(dat_file,target_directory) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
161 with gzip.open(dat_path,"rt") as dat : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
162 dat_reader = csv.reader(dat,delimiter="\t") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
163 for line in dat_reader : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
164 uniprotID=line[0] #UniProtID as key |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
165 id_type=line[1] #ID type of corresponding id, key of sub-dictionnary |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
166 cor_id=line[2] #corresponding id |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
167 if "-" not in id_type : #we don't keep isoform |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
168 if id_type in ids and uniprotID in unidict : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
169 if id_type in unidict[uniprotID] : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
170 unidict[uniprotID][id_type]= ";".join([unidict[uniprotID][id_type],cor_id]) #if there is already a value in the dictionnary |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
171 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
172 unidict[uniprotID].update({ id_type : cor_id }) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
173 elif id_type in ids : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
174 unidict[uniprotID]={id_type : cor_id} |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
175 os.remove(dat_path) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
176 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
177 #print("dat_file ok") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
178 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
179 #add ids from idmapping.dat to the final tab |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
180 for line in tab[1:] : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
181 uniprotID=line[0] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
182 if human : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
183 if uniprotID in unidict : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
184 nextprot = access_dictionary(unidict,uniprotID,'neXtProt') |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
185 if nextprot != '' : nextprot = clean_nextprot_id(nextprot,line[0]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
186 line.extend([nextprot,access_dictionary(unidict,uniprotID,'BioGrid'),access_dictionary(unidict,uniprotID,'STRING'), |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
187 access_dictionary(unidict,uniprotID,'KEGG')]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
188 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
189 line.extend(["","","",""]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
190 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
191 if uniprotID in unidict : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
192 line.extend([access_dictionary(unidict,uniprotID,'BioGrid'),access_dictionary(unidict,uniprotID,'STRING'), |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
193 access_dictionary(unidict,uniprotID,'KEGG')]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
194 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
195 line.extend(["","",""]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
196 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
197 #print ("tab ok") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
198 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
199 #add missing nextprot ID for human |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
200 if human : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
201 #build next_dict |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
202 nextprot_ids = id_list_from_nextprot_ftp("nextprot_ac_list_all.txt",target_directory) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
203 next_dict = {} |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
204 for nextid in nextprot_ids : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
205 next_dict[nextid.replace("NX_","")] = nextid |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
206 os.remove(os.path.join(target_directory,"nextprot_ac_list_all.txt")) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
207 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
208 #add missing nextprot ID |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
209 for line in tab[1:] : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
210 uniprotID=line[0] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
211 nextprotID=line[13] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
212 if nextprotID == '' and uniprotID in next_dict : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
213 line[13]=next_dict[uniprotID] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
214 |
13
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
215 #create empty dictionary and dictionary index |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
216 ids_dictionary, ids_dictionary_index = create_ids_dictionary(ids_list) |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
217 |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
218 #fill dictionary and sub dictionaries with ids |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
219 for line in tab[1:] : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
220 for index, ids in enumerate(line) : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
221 other_id_type_index = [accession_id for accession_id in ids_dictionary_index.keys() if accession_id!=index] |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
222 for id in ids.replace(" ","").split(";") : #if there's more than one id, one key per id (example : GO) |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
223 if id not in ids_dictionary[ids_dictionary_index[index]] : #if the key is not created yet |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
224 ids_dictionary[ids_dictionary_index[index]][id]={} |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
225 for other_id_type in other_id_type_index : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
226 if ids_dictionary_index[other_id_type] not in ids_dictionary[ids_dictionary_index[index]][id] : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
227 ids_dictionary[ids_dictionary_index[index]][id][ids_dictionary_index[other_id_type]] = set(line[other_id_type].replace(" ","").split(";")) |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
228 else : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
229 ids_dictionary[ids_dictionary_index[index]][id][ids_dictionary_index[other_id_type]] |= set(line[other_id_type].replace(" ","").split(";")) |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
230 if len(ids_dictionary[ids_dictionary_index[index]][id][ids_dictionary_index[other_id_type]]) > 1 and '' in ids_dictionary[ids_dictionary_index[index]][id][ids_dictionary_index[other_id_type]] : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
231 ids_dictionary[ids_dictionary_index[index]][id][ids_dictionary_index[other_id_type]].remove('') |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
232 |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
233 ##writing output files |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
234 output_file = species+"_id_mapping_"+ time.strftime("%d-%m-%Y") + ".pickle" |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
235 path = os.path.join(target_directory,output_file) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
236 |
13
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
237 #save ids_dictionary |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
238 with open(output_dict, 'wb') as handle: |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
239 pickle.dump(ids_dictionary, handle, protocol=pickle.HIGHEST_PROTOCOL) |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
240 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
241 name_dict={"human" : "Homo sapiens", "mouse" : "Mus musculus", "rat" : "Rattus norvegicus"} |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
242 name = name_dict[species]+" "+time.strftime("%d/%m/%Y") |
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
243 id = species+"_id_mapping_"+ time.strftime("%d-%m-%Y") |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
244 |
10
2f153b41b6fe
planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents:
7
diff
changeset
|
245 data_table_entry = dict(id=id, name = name, value = species, path = path) |
13
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
246 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_id_mapping_dictionaries") |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
247 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
248 def download_from_uniprot_ftp(file,target_directory) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
249 ftp_dir = "pub/databases/uniprot/current_release/knowledgebase/idmapping/by_organism/" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
250 path = os.path.join(target_directory, file) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
251 ftp = ftplib.FTP("ftp.uniprot.org") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
252 ftp.login("anonymous", "anonymous") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
253 ftp.cwd(ftp_dir) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
254 ftp.retrbinary("RETR " + file, open(path, 'wb').write) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
255 ftp.quit() |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
256 return (path) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
257 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
258 def id_list_from_nextprot_ftp(file,target_directory) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
259 ftp_dir = "pub/current_release/ac_lists/" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
260 path = os.path.join(target_directory, file) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
261 ftp = ftplib.FTP("ftp.nextprot.org") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
262 ftp.login("anonymous", "anonymous") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
263 ftp.cwd(ftp_dir) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
264 ftp.retrbinary("RETR " + file, open(path, 'wb').write) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
265 ftp.quit() |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
266 with open(path,'r') as nextprot_ids : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
267 nextprot_ids = nextprot_ids.read().splitlines() |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
268 return (nextprot_ids) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
269 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
270 #return '' if there's no value in a dictionary, avoid error |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
271 def access_dictionary (dico,key1,key2) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
272 if key1 in dico : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
273 if key2 in dico[key1] : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
274 return (dico[key1][key2]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
275 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
276 return ("") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
277 #print (key2,"not in ",dico,"[",key1,"]") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
278 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
279 return ('') |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
280 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
281 #if there are several nextprot ID for one uniprotID, return the uniprot like ID |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
282 def clean_nextprot_id (next_id,uniprotAc) : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
283 if len(next_id.split(";")) > 1 : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
284 tmp = next_id.split(";") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
285 if "NX_"+uniprotAc in tmp : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
286 return ("NX_"+uniprotAc) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
287 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
288 return (tmp[1]) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
289 else : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
290 return (next_id) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
291 |
13
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
292 #create empty dictionary with index for tab |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
293 def create_ids_dictionary (ids_list) : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
294 ids_dictionary = {} |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
295 for id_type in ids_list : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
296 ids_dictionary[id_type]={} |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
297 ids_dictionary_index = {} |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
298 |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
299 for i,id in enumerate(ids_list) : |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
300 ids_dictionary_index[i]=id |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
301 |
a1530507fee4
planemo upload commit 08d8f131da0e66113519ffaa7f7e7632cb3d1eff-dirty
dchristiany
parents:
12
diff
changeset
|
302 return(ids_dictionary,ids_dictionary_index) |
0
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
303 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
304 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
305 # Main function |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
306 ####################################################################################################### |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
307 def main(): |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
308 parser = argparse.ArgumentParser() |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
309 parser.add_argument("--hpa", metavar = ("HPA_OPTION")) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
310 parser.add_argument("--peptideatlas", metavar=("SAMPLE_CATEGORY_ID")) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
311 parser.add_argument("--id_mapping", metavar = ("ID_MAPPING_SPECIES")) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
312 parser.add_argument("-o", "--output") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
313 args = parser.parse_args() |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
314 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
315 data_manager_dict = {} |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
316 # Extract json file params |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
317 filename = args.output |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
318 params = from_json_string(open(filename).read()) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
319 target_directory = params[ 'output_data' ][0]['extra_files_path'] |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
320 os.mkdir(target_directory) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
321 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
322 ## Download source files from HPA |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
323 try: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
324 hpa = args.hpa |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
325 except NameError: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
326 hpa = None |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
327 if hpa is not None: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
328 #target_directory = "/projet/galaxydev/galaxy/tools/proteore/ProteoRE/tools/resources_building/test-data/" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
329 hpa = hpa.split(",") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
330 for hpa_tissue in hpa: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
331 HPA_sources(data_manager_dict, hpa_tissue, target_directory) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
332 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
333 ## Download source file from Peptide Atlas query |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
334 try: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
335 peptide_atlas = args.peptideatlas |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
336 except NameError: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
337 peptide_atlas = None |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
338 if peptide_atlas is not None: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
339 #target_directory = "/projet/galaxydev/galaxy/tools/proteore/ProteoRE/tools/resources_building/test-data/" |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
340 peptide_atlas = peptide_atlas.split(",") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
341 for pa_tissue in peptide_atlas: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
342 peptide_atlas_sources(data_manager_dict, pa_tissue, target_directory) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
343 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
344 ## Download ID_mapping source file from Uniprot |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
345 try: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
346 id_mapping=args.id_mapping |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
347 except NameError: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
348 id_mapping = None |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
349 if id_mapping is not None: |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
350 id_mapping = id_mapping .split(",") |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
351 for species in id_mapping : |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
352 id_mapping_sources(data_manager_dict, species, target_directory) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
353 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
354 #save info to json file |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
355 filename = args.output |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
356 open(filename, 'wb').write(to_json_string(data_manager_dict)) |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
357 |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
358 if __name__ == "__main__": |
2de84fea8367
planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff
changeset
|
359 main() |