annotate data_manager/resource_building.py @ 44:f89f008bf80d draft

planemo upload commit 9b701c1faf8be4835b4e7236780ee9ee26f9a373-dirty
author dchristiany
date Thu, 31 Jan 2019 08:54:10 -0500
parents 3febf3d1139a
children 80fc0b28e227
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
85532a48e4e4 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 17
diff changeset
1 # -*- coding: utf-8 -*-
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
2 """
10
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
3 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
4 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
5
21
0a79066992fc planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 20
diff changeset
6 import os, sys, argparse, requests, time, csv, re, json, shutil, zipfile
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
7 from io import BytesIO
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
8 from zipfile import ZipFile
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
9 from galaxy.util.json import from_json_string, to_json_string
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
10
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
11 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
12 # General functions
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
13 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
14 def unzip(url, output_file):
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
15 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
16 Get a zip file content from a link and unzip
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
17 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
18 content = requests.get(url)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
19 zipfile = ZipFile(BytesIO(content.content))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
20 output_content = ""
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
21 output_content += zipfile.open(zipfile.namelist()[0]).read()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
22 output = open(output_file, "w")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
23 output.write(output_content)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
24 output.close()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
25
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
26 def _add_data_table_entry(data_manager_dict, data_table_entry,data_table):
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
27 data_manager_dict['data_tables'] = data_manager_dict.get('data_tables', {})
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
28 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
29 data_manager_dict['data_tables'][data_table].append(data_table_entry)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
30 return data_manager_dict
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
31
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
32 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
33 # 1. Human Protein Atlas
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
34 # - Normal tissue
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
35 # - Pathology
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
36 # - Full Atlas
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
37 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
38 def HPA_sources(data_manager_dict, tissue, target_directory):
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
39 if tissue == "HPA_normal_tissue":
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
40 tissue_name = "HPA normal tissue"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
41 url = "https://www.proteinatlas.org/download/normal_tissue.tsv.zip"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
42 elif tissue == "HPA_pathology":
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
43 tissue_name = "HPA pathology"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
44 url = "https://www.proteinatlas.org/download/pathology.tsv.zip"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
45 elif tissue == "HPA_full_atlas":
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
46 tissue_name = "HPA full atlas"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
47 url = "https://www.proteinatlas.org/download/proteinatlas.tsv.zip"
10
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
48
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
49 output_file = tissue +"_"+ time.strftime("%d-%m-%Y") + ".tsv"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
50 path = os.path.join(target_directory, output_file)
10
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
51 unzip(url, path) #download and save file
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
52 tissue_name = tissue_name + " " + time.strftime("%d/%m/%Y")
10
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
53 tissue_id = tissue_name.replace(" ","_").replace("/","-")
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
54
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
55 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
56 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_protein_atlas")
0
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 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
60 # 2. Peptide Atlas
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
61 #######################################################################################################
43
3febf3d1139a planemo upload commit 9b701c1faf8be4835b4e7236780ee9ee26f9a373-dirty
dchristiany
parents: 42
diff changeset
62 def peptide_atlas_sources(data_manager_dict, tissue, date, target_directory):
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
63 # 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
64 organism_id = "2"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
65 # Extract sample_category_id and output filename
34
0c0586ac3e29 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 33
diff changeset
66 tissue=tissue.split(".")
29
871a7347ca24 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 28
diff changeset
67 sample_category_id = tissue[0]
34
0c0586ac3e29 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 33
diff changeset
68 tissue_name = tissue[1]
43
3febf3d1139a planemo upload commit 9b701c1faf8be4835b4e7236780ee9ee26f9a373-dirty
dchristiany
parents: 42
diff changeset
69 output_file = tissue_name+"_"+date + ".tsv"
30
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
70
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
71 query="https://db.systemsbiology.net/sbeams/cgi/PeptideAtlas/GetProteins?&atlas_build_id="+ \
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
72 sample_category_id+"&display_options=ShowAbundances&organism_id="+organism_id+ \
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
73 "&redundancy_constraint=4&presence_level_constraint=1%2C2&gene_annotation_level_constraint=leaf\
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
74 &QUERY_NAME=AT_GetProteins&action=QUERY&output_mode=tsv&apply_action=QUERY"
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
75
30
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
76 with requests.Session() as s:
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
77 download = s.get(query)
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
78 decoded_content = download.content.decode('utf-8')
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
79 cr = csv.reader(decoded_content.splitlines(), delimiter='\t')
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
80
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
81 uni_dict = build_dictionary(cr)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
82
10
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
83 #columns of data table peptide_atlas
34
0c0586ac3e29 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 33
diff changeset
84 tissue_id = tissue_name+"_"+date
0c0586ac3e29 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 33
diff changeset
85 name = tissue_id.replace("-","/").replace("_"," ")
36
a4811c440b45 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 35
diff changeset
86 path = os.path.join(target_directory,output_file)
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
87
30
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
88 with open(path,"w") as out :
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
89 w = csv.writer(out,delimiter='\t')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
90 w.writerow(["Uniprot_AC","nb_obs"])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
91 w.writerows(uni_dict.items())
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
92
34
0c0586ac3e29 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 33
diff changeset
93 data_table_entry = dict(id=tissue_id, name=name, value = path, tissue = tissue_name)
12
60cb0a5ae661 planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 10
diff changeset
94 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_peptide_atlas")
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
95
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
96 #function to count the number of observations by uniprot id
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
97 def build_dictionary (csv) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
98 uni_dict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
99 for line in csv :
30
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
100 if "-" not in line[0] and check_uniprot_access(line[0]) :
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
101 if line[0] in uni_dict :
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
102 uni_dict[line[0]] += int(line[5])
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
103 else :
30
b8271b9a1049 planemo upload commit c89c5deac442c0c2aa52b24f2c5af4b290773fc0-dirty
dchristiany
parents: 29
diff changeset
104 uni_dict[line[0]] = int(line[5])
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
105
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
106 return uni_dict
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
107
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
108 #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
109 def check_uniprot_access (id) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
110 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
111 if uniprot_pattern.match(id) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
112 return True
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
113 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
114 return False
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 # 3. ID mapping file
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
118 #######################################################################################################
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
119 import ftplib, gzip
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
120 csv.field_size_limit(sys.maxsize) # to handle big files
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
121
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
122 def id_mapping_sources (data_manager_dict, species, target_directory) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
123
28
d235909789ca planemo upload commit e694c4b0df30a4286ba09721696e8ec3af25fd97-dirty
dchristiany
parents: 27
diff changeset
124 human = species == "Human"
d235909789ca planemo upload commit e694c4b0df30a4286ba09721696e8ec3af25fd97-dirty
dchristiany
parents: 27
diff changeset
125 species_dict = { "Human" : "HUMAN_9606", "Mouse" : "MOUSE_10090", "Rat" : "RAT_10116" }
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
126 files=["idmapping_selected.tab.gz","idmapping.dat.gz"]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
127
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
128 #header
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
129 if human : tab = [["UniProt-AC","UniProt-ID","GeneID","RefSeq","GI","PDB","GO","PIR","MIM","UniGene","Ensembl_Gene","Ensembl_Transcript","Ensembl_Protein","neXtProt","BioGrid","STRING","KEGG"]]
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
130 else : tab = [["UniProt-AC","UniProt-ID","GeneID","RefSeq","GI","PDB","GO","PIR","MIM","UniGene","Ensembl_Gene","Ensembl_Transcript","Ensembl_Protein","BioGrid","STRING","KEGG"]]
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
131
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
132 #print("header ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
133
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
134 #get selected.tab and keep only ids of interest
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
135 selected_tab_file=species_dict[species]+"_"+files[0]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
136 tab_path = download_from_uniprot_ftp(selected_tab_file,target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
137 with gzip.open(tab_path,"rt") as select :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
138 tab_reader = csv.reader(select,delimiter="\t")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
139 for line in tab_reader :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
140 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
141 os.remove(tab_path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
142
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
143 #print("selected_tab ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
144
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
145 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
146 Supplementary ID to get from HUMAN_9606_idmapping.dat :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
147 -NextProt,BioGrid,STRING,KEGG
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
148 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
149
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
150 #there's more id type for human
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
151 if human : ids = ['neXtProt','BioGrid','STRING','KEGG' ] #ids to get from dat_file
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
152 else : ids = ['BioGrid','STRING','KEGG' ]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
153 unidict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
154
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
155 #keep only ids of interest in dictionaries
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
156 dat_file=species_dict[species]+"_"+files[1]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
157 dat_path = download_from_uniprot_ftp(dat_file,target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
158 with gzip.open(dat_path,"rt") as dat :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
159 dat_reader = csv.reader(dat,delimiter="\t")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
160 for line in dat_reader :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
161 uniprotID=line[0] #UniProtID as key
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
162 id_type=line[1] #ID type of corresponding id, key of sub-dictionnary
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
163 cor_id=line[2] #corresponding id
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
164 if "-" not in id_type : #we don't keep isoform
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
165 if id_type in ids and uniprotID in unidict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
166 if id_type in unidict[uniprotID] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
167 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
168 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
169 unidict[uniprotID].update({ id_type : cor_id })
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
170 elif id_type in ids :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
171 unidict[uniprotID]={id_type : cor_id}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
172 os.remove(dat_path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
173
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
174 #print("dat_file ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
175
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
176 #add ids from idmapping.dat to the final tab
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
177 for line in tab[1:] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
178 uniprotID=line[0]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
179 if human :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
180 if uniprotID in unidict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
181 nextprot = access_dictionary(unidict,uniprotID,'neXtProt')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
182 if nextprot != '' : nextprot = clean_nextprot_id(nextprot,line[0])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
183 line.extend([nextprot,access_dictionary(unidict,uniprotID,'BioGrid'),access_dictionary(unidict,uniprotID,'STRING'),
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
184 access_dictionary(unidict,uniprotID,'KEGG')])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
185 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
186 line.extend(["","","",""])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
187 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
188 if uniprotID in unidict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
189 line.extend([access_dictionary(unidict,uniprotID,'BioGrid'),access_dictionary(unidict,uniprotID,'STRING'),
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
190 access_dictionary(unidict,uniprotID,'KEGG')])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
191 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
192 line.extend(["","",""])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
193
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
194 #print ("tab ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
195
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
196 #add missing nextprot ID for human
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
197 if human :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
198 #build next_dict
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
199 nextprot_ids = id_list_from_nextprot_ftp("nextprot_ac_list_all.txt",target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
200 next_dict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
201 for nextid in nextprot_ids :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
202 next_dict[nextid.replace("NX_","")] = nextid
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
203 os.remove(os.path.join(target_directory,"nextprot_ac_list_all.txt"))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
204
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
205 #add missing nextprot ID
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
206 for line in tab[1:] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
207 uniprotID=line[0]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
208 nextprotID=line[13]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
209 if nextprotID == '' and uniprotID in next_dict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
210 line[13]=next_dict[uniprotID]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
211
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
212 output_file = species+"_id_mapping_"+ time.strftime("%d-%m-%Y") + ".tsv"
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
213 path = os.path.join(target_directory,output_file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
214
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
215 with open(path,"w") as out :
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
216 w = csv.writer(out,delimiter='\t')
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
217 w.writerows(tab)
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
218
28
d235909789ca planemo upload commit e694c4b0df30a4286ba09721696e8ec3af25fd97-dirty
dchristiany
parents: 27
diff changeset
219 name_dict={"Human" : "Homo sapiens", "Mouse" : "Mus musculus", "Rat" : "Rattus norvegicus"}
d235909789ca planemo upload commit e694c4b0df30a4286ba09721696e8ec3af25fd97-dirty
dchristiany
parents: 27
diff changeset
220 name = species +" (" + name_dict[species]+" "+time.strftime("%d/%m/%Y")+")"
10
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
221 id = species+"_id_mapping_"+ time.strftime("%d-%m-%Y")
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
222
10
2f153b41b6fe planemo upload commit e5e768b479ddc6b36270a1b5b0443a4c80d693bc-dirty
dchristiany
parents: 7
diff changeset
223 data_table_entry = dict(id=id, name = name, value = species, path = path)
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
224 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_id_mapping")
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
225
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
226 def download_from_uniprot_ftp(file,target_directory) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
227 ftp_dir = "pub/databases/uniprot/current_release/knowledgebase/idmapping/by_organism/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
228 path = os.path.join(target_directory, file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
229 ftp = ftplib.FTP("ftp.uniprot.org")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
230 ftp.login("anonymous", "anonymous")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
231 ftp.cwd(ftp_dir)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
232 ftp.retrbinary("RETR " + file, open(path, 'wb').write)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
233 ftp.quit()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
234 return (path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
235
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
236 def id_list_from_nextprot_ftp(file,target_directory) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
237 ftp_dir = "pub/current_release/ac_lists/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
238 path = os.path.join(target_directory, file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
239 ftp = ftplib.FTP("ftp.nextprot.org")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
240 ftp.login("anonymous", "anonymous")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
241 ftp.cwd(ftp_dir)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
242 ftp.retrbinary("RETR " + file, open(path, 'wb').write)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
243 ftp.quit()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
244 with open(path,'r') as nextprot_ids :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
245 nextprot_ids = nextprot_ids.read().splitlines()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
246 return (nextprot_ids)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
247
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
248 #return '' if there's no value in a dictionary, avoid error
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
249 def access_dictionary (dico,key1,key2) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
250 if key1 in dico :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
251 if key2 in dico[key1] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
252 return (dico[key1][key2])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
253 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
254 return ("")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
255 #print (key2,"not in ",dico,"[",key1,"]")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
256 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
257 return ('')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
258
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
259 #if there are several nextprot ID for one uniprotID, return the uniprot like ID
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
260 def clean_nextprot_id (next_id,uniprotAc) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
261 if len(next_id.split(";")) > 1 :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
262 tmp = next_id.split(";")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
263 if "NX_"+uniprotAc in tmp :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
264 return ("NX_"+uniprotAc)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
265 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
266 return (tmp[1])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
267 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
268 return (next_id)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
269
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
270
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
271 #######################################################################################################
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
272 # 4. Build protein interaction maps files
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
273 #######################################################################################################
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
274
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
275 def PPI_ref_files(data_manager_dict, species, interactome, target_directory):
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
276
28
d235909789ca planemo upload commit e694c4b0df30a4286ba09721696e8ec3af25fd97-dirty
dchristiany
parents: 27
diff changeset
277 species_dict={'Human':'Homo sapiens',"Mouse":"Mus musculus","Rat":"Rattus norvegicus"}
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
278
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
279 ##BioGRID
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
280 if interactome=="biogrid":
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
281
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
282 tab2_link="https://downloads.thebiogrid.org/Download/BioGRID/Release-Archive/BIOGRID-3.5.167/BIOGRID-ORGANISM-3.5.167.tab2.zip"
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
283
39
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
284 #download zip file
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
285 r = requests.get(tab2_link)
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
286 with open("BioGRID.zip", "wb") as code:
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
287 code.write(r.content)
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
288
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
289 #unzip files
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
290 with zipfile.ZipFile("BioGRID.zip", 'r') as zip_ref:
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
291 if not os.path.exists("tmp_BioGRID"): os.makedirs("tmp_BioGRID")
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
292 zip_ref.extractall("tmp_BioGRID")
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
293
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
294 #import file of interest and build dictionary
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
295 file_path="tmp_BioGRID/BIOGRID-ORGANISM-"+species_dict[species].replace(" ","_")+"-3.5.167.tab2.txt"
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
296 with open(file_path,"r") as handle :
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
297 tab_file = csv.reader(handle,delimiter="\t")
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
298 dico_network = {}
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
299 GeneID_index=1
39
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
300 network_cols=[1,2,7,8,11,12,14,18,20]
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
301 for line in tab_file :
22
778cc97cb115 planemo upload commit e2e4ac8c2c4dc5675d4c415ff192a925ca5e6b98-dirty
dchristiany
parents: 21
diff changeset
302 if line[GeneID_index] not in dico_network:
778cc97cb115 planemo upload commit e2e4ac8c2c4dc5675d4c415ff192a925ca5e6b98-dirty
dchristiany
parents: 21
diff changeset
303 dico_network[line[GeneID_index]]=[[line[i] for i in network_cols]]
778cc97cb115 planemo upload commit e2e4ac8c2c4dc5675d4c415ff192a925ca5e6b98-dirty
dchristiany
parents: 21
diff changeset
304 else:
778cc97cb115 planemo upload commit e2e4ac8c2c4dc5675d4c415ff192a925ca5e6b98-dirty
dchristiany
parents: 21
diff changeset
305 dico_network[line[GeneID_index]].append([line[i] for i in network_cols])
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
306
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
307 #delete tmp_BioGRID directory
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
308 os.remove("BioGRID.zip")
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
309 shutil.rmtree("tmp_BioGRID", ignore_errors=True)
39
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
310
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
311 #download NCBI2Reactome.txt file and build dictionary
39
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
312 with requests.Session() as s:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
313 r = s.get('https://www.reactome.org/download/current/NCBI2Reactome.txt')
40
fddf4a3847f4 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 39
diff changeset
314 r.encoding ="utf-8"
fddf4a3847f4 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 39
diff changeset
315 tab_file = csv.reader(r.content.splitlines(), delimiter='\t')
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
316
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
317 dico_nodes = {}
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
318 uniProt_index=0
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
319 pathway_description_index=3
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
320 species_index=5
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
321 for line in tab_file :
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
322 if line[species_index]==species_dict[species]:
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
323 if line[uniProt_index] in dico_nodes :
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
324 dico_nodes[line[uniProt_index]].append(line[pathway_description_index])
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
325 else :
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
326 dico_nodes[line[uniProt_index]] = [line[pathway_description_index]]
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
327
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
328 dico={}
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
329 dico['network']=dico_network
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
330 dico['nodes']=dico_nodes
39
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
331
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
332 ##Bioplex
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
333 elif interactome=="bioplex":
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
334
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
335 with requests.Session() as s:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
336 r = s.get('http://bioplex.hms.harvard.edu/data/BioPlex_interactionList_v4a.tsv')
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
337 r = r.content.decode('utf-8')
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
338 bioplex = csv.reader(r.splitlines(), delimiter='\t')
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
339
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
340 dico_network = {}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
341 dico_network["GeneID"]={}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
342 network_geneid_cols=[0,1,4,5,8]
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
343 dico_network["UniProt-AC"]={}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
344 network_uniprot_cols=[2,3,4,5,8]
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
345 dico_GeneID_to_UniProt = {}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
346 for line in bioplex :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
347 if line[0] not in dico_network["GeneID"]:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
348 dico_network["GeneID"][line[0]]=[[line[i] for i in network_geneid_cols]]
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
349 else :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
350 dico_network["GeneID"][line[0]].append([line[i] for i in network_geneid_cols])
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
351 if line[1] not in dico_network["UniProt-AC"]:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
352 dico_network["UniProt-AC"][line[2]]=[[line[i] for i in network_uniprot_cols]]
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
353 else:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
354 dico_network["UniProt-AC"][line[2]].append([line[i] for i in network_uniprot_cols])
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
355 dico_GeneID_to_UniProt[line[0]]=line[2]
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
356
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
357 with requests.Session() as s:
40
fddf4a3847f4 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 39
diff changeset
358 r = s.get('https://reactome.org/download/current/UniProt2Reactome.txt')
fddf4a3847f4 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 39
diff changeset
359 r.encoding ="utf-8"
fddf4a3847f4 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 39
diff changeset
360 tab_file = csv.reader(r.content.splitlines(), delimiter='\t')
39
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
361
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
362 dico_nodes_uniprot = {}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
363 uniProt_index=0
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
364 pathway_description_index=3
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
365 species_index=5
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
366 for line in tab_file :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
367 if line[species_index]==species_dict[species]:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
368 if line[uniProt_index] in dico_nodes_uniprot :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
369 dico_nodes_uniprot[line[uniProt_index]].append(line[pathway_description_index])
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
370 else :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
371 dico_nodes_uniprot[line[uniProt_index]] = [line[pathway_description_index]]
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
372
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
373 with requests.Session() as s:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
374 r = s.get('https://www.reactome.org/download/current/NCBI2Reactome.txt')
40
fddf4a3847f4 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 39
diff changeset
375 r.encoding ="utf-8"
fddf4a3847f4 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 39
diff changeset
376 tab_file = csv.reader(r.content.splitlines(), delimiter='\t')
39
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
377
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
378 dico_nodes_geneid = {}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
379 uniProt_index=0
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
380 pathway_description_index=3
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
381 species_index=5
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
382 for line in tab_file :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
383 if line[species_index]==species_dict[species]:
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
384 if line[uniProt_index] in dico_nodes_geneid :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
385 dico_nodes_geneid[line[uniProt_index]].append(line[pathway_description_index])
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
386 else :
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
387 dico_nodes_geneid[line[uniProt_index]] = [line[pathway_description_index]]
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
388
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
389 dico={}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
390 dico_nodes={}
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
391 dico_nodes['GeneID']=dico_nodes_geneid
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
392 dico_nodes['UniProt-AC']=dico_nodes_uniprot
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
393 dico['network']=dico_network
ec6252ad1a8e planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 36
diff changeset
394 dico['nodes']=dico_nodes
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
395 dico['convert']=dico_GeneID_to_UniProt
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
396
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
397 #writing output
42
5a37a086c9a8 planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 41
diff changeset
398 output_file = species+'_'+interactome+'_'+ time.strftime("%d-%m-%Y") + ".json"
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
399 path = os.path.join(target_directory,output_file)
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
400 name = species+" ("+species_dict[species]+") "+time.strftime("%d/%m/%Y")
27
592c59530c32 planemo upload commit e694c4b0df30a4286ba09721696e8ec3af25fd97-dirty
dchristiany
parents: 26
diff changeset
401 id = species+"_"+interactome+"_"+ time.strftime("%d-%m-%Y")
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
402
41
4062484f2cee planemo upload commit 43e2a01d7519104c2c16510e4dbdc023e65c49c7-dirty
dchristiany
parents: 40
diff changeset
403 with open(path, 'w') as handle:
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
404 json.dump(dico, handle, sort_keys=True)
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
405
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
406 data_table_entry = dict(id=id, name = name, value = species, path = path)
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
407 _add_data_table_entry(data_manager_dict, data_table_entry, "proteore_"+interactome+"_dictionaries")
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
408
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
409
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
410 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
411 # Main function
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
412 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
413 def main():
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
414 parser = argparse.ArgumentParser()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
415 parser.add_argument("--hpa", metavar = ("HPA_OPTION"))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
416 parser.add_argument("--peptideatlas", metavar=("SAMPLE_CATEGORY_ID"))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
417 parser.add_argument("--id_mapping", metavar = ("ID_MAPPING_SPECIES"))
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
418 parser.add_argument("--interactome", metavar = ("PPI"))
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
419 parser.add_argument("--species")
43
3febf3d1139a planemo upload commit 9b701c1faf8be4835b4e7236780ee9ee26f9a373-dirty
dchristiany
parents: 42
diff changeset
420 parser.add_argument("--date")
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
421 parser.add_argument("-o", "--output")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
422 args = parser.parse_args()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
423
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
424 data_manager_dict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
425 # Extract json file params
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
426 filename = args.output
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
427 params = from_json_string(open(filename).read())
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
428 target_directory = params[ 'output_data' ][0]['extra_files_path']
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
429 os.mkdir(target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
430
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
431 ## Download source files from HPA
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
432 try:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
433 hpa = args.hpa
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
434 except NameError:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
435 hpa = None
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
436 if hpa is not None:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
437 #target_directory = "/projet/galaxydev/galaxy/tools/proteore/ProteoRE/tools/resources_building/test-data/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
438 hpa = hpa.split(",")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
439 for hpa_tissue in hpa:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
440 HPA_sources(data_manager_dict, hpa_tissue, target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
441
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
442 ## Download source file from Peptide Atlas query
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
443 try:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
444 peptide_atlas = args.peptideatlas
43
3febf3d1139a planemo upload commit 9b701c1faf8be4835b4e7236780ee9ee26f9a373-dirty
dchristiany
parents: 42
diff changeset
445 date = args.date
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
446 except NameError:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
447 peptide_atlas = None
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
448 if peptide_atlas is not None:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
449 #target_directory = "/projet/galaxydev/galaxy/tools/proteore/ProteoRE/tools/resources_building/test-data/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
450 peptide_atlas = peptide_atlas.split(",")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
451 for pa_tissue in peptide_atlas:
43
3febf3d1139a planemo upload commit 9b701c1faf8be4835b4e7236780ee9ee26f9a373-dirty
dchristiany
parents: 42
diff changeset
452 peptide_atlas_sources(data_manager_dict, pa_tissue, date, target_directory)
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
453
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
454 ## Download ID_mapping source file from Uniprot
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
455 try:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
456 id_mapping=args.id_mapping
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
457 except NameError:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
458 id_mapping = None
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
459 if id_mapping is not None:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
460 id_mapping = id_mapping .split(",")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
461 for species in id_mapping :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
462 id_mapping_sources(data_manager_dict, species, target_directory)
15
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
463
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
464 ## Download PPI ref files from biogrid/bioplex/humap
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
465 try:
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
466 interactome=args.interactome
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
467 species=args.species
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
468 except NameError:
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
469 interactome=None
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
470 species=None
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
471 if interactome is not None and species is not None:
83f57ba70416 planemo upload commit 8040003119a3d54866ec6ee9b9f659f2af554817-dirty
dchristiany
parents: 13
diff changeset
472 PPI_ref_files(data_manager_dict, species, interactome, target_directory)
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
473
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
474 #save info to json file
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
475 filename = args.output
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
476 open(filename, 'wb').write(to_json_string(data_manager_dict))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
477
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
478 if __name__ == "__main__":
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
479 main()