annotate data_manager/resource_building.py @ 3:c4ac72465a95 draft

planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
author dchristiany
date Fri, 19 Oct 2018 03:52:55 -0400
parents 2de84fea8367
children 9c75521e4a64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
1 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
2 The purpose of this script is to create source files from different databases to be used in other tools
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"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
47 output_file = tissue +"_"+ time.strftime("%d-%m-%Y") + ".tsv"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
48 path = os.path.join(target_directory, output_file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
49 unzip(url, path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
50 print(str(os.path.isfile(path)))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
51 tmp=open(path,"r").readlines()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
52 tissue_name = tissue_name + " " + time.strftime("%d/%m/%Y")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
53 data_table_entry = dict(value = tissue, name = tissue_name, path = path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
54 _add_data_table_entry(data_manager_dict, data_table_entry, "proteinatlas")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
55
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 # 2. Peptide Atlas
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
59 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
60 def peptide_atlas_sources(data_manager_dict, tissue, target_directory):
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
61 # Define PA Human build released number (here early 2018)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
62 atlas_build_id = "472"
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
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
66 sample_category_id = tissue.split("-")[0]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
67 output_file = tissue.split("-")[1] +"_"+ time.strftime("%d-%m-%Y") + ".tsv"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
68 query = "https://db.systemsbiology.net/sbeams/cgi/PeptideAtlas/GetPeptides?atlas_build_id=" + \
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
69 atlas_build_id + "&display_options=ShowMappings&organism_id= " + \
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
70 organism_id + "&sample_category_id=" + sample_category_id + \
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
71 "&QUERY_NAME=AT_GetPeptides&output_mode=tsv&apply_action=QUERY"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
72 download = requests.get(query)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
73 decoded_content = download.content.decode('utf-8')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
74 cr = csv.reader(decoded_content.splitlines(), delimiter='\t')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
75
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
76 #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
77 uni_dict = build_dictionary(cr)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
78
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
79 tissue_id = "_".join([atlas_build_id, organism_id, sample_category_id,time.strftime("%d-%m-%Y")])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
80 tissue_value = tissue.split("-")[1]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
81 tissue = tissue.split("-")[1] + "_" +time.strftime("%d-%m-%Y")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
82 tissue_name = " ".join(tissue_value.split("_")) + " " + time.strftime("%d/%m/%Y")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
83 path = os.path.join(target_directory,output_file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
84
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
85 with open(path,"wb") as out :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
86 w = csv.writer(out,delimiter='\t')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
87 w.writerow(["Uniprot_AC","nb_obs"])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
88 w.writerows(uni_dict.items())
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
89
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
90 data_table_entry = dict(value = path, name = tissue_name, tissue = tissue)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
91 _add_data_table_entry(data_manager_dict, data_table_entry, "peptide_atlas")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
92
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
93 #function to count the number of observations by uniprot id
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
94 def build_dictionary (csv) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
95 uni_dict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
96 for line in csv :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
97 if "-" not in line[2] and check_uniprot_access(line[2]) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
98 if line[2] in uni_dict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
99 uni_dict[line[2]] += int(line[4])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
100 else :
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
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
103 return uni_dict
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
104
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
105 #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
106 def check_uniprot_access (id) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
107 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
108 if uniprot_pattern.match(id) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
109 return True
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
110 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
111 return False
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
112
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
113
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 # 3. ID mapping file
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
117 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
118 import ftplib, gzip
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
119 csv.field_size_limit(sys.maxsize) # to handle big files
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
120
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
121 def id_mapping_sources (data_manager_dict, species, target_directory) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
122
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
123 human = species == "human"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
124 species_dict = { "human" : "HUMAN_9606", "mouse" : "MOUSE_10090", "rat" : "RAT_10116" }
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
125 files=["idmapping_selected.tab.gz","idmapping.dat.gz"]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
126
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
127 #header
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
128 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"]]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
129 else : tab = [["UniProt-AC","UniProt-ID","GeneID","RefSeq","GI","PDB","GO","PIR","MIM","UniGene","Ensembl_Gene","Ensembl_Transcript","Ensembl_Protein","BioGrid","STRING","KEGG"]]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
130
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
131 #print("header ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
132
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
133 #selected.tab and keep only ids of interest
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
134 selected_tab_file=species_dict[species]+"_"+files[0]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
135 tab_path = download_from_uniprot_ftp(selected_tab_file,target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
136 with gzip.open(tab_path,"rt") as select :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
137 tab_reader = csv.reader(select,delimiter="\t")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
138 for line in tab_reader :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
139 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
140 os.remove(tab_path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
141
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
142 #print("selected_tab ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
143
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
144 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
145 Supplementary ID to get from HUMAN_9606_idmapping.dat :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
146 -NextProt,BioGrid,STRING,KEGG
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
147 """
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
148
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
149 if human : ids = ['neXtProt','BioGrid','STRING','KEGG' ] #ids to get from dat_file
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
150 else : ids = ['BioGrid','STRING','KEGG' ]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
151 unidict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
152
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
153 #keep only ids of interest in dictionaries
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
154 dat_file=species_dict[species]+"_"+files[1]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
155 dat_path = download_from_uniprot_ftp(dat_file,target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
156 with gzip.open(dat_path,"rt") as dat :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
157 dat_reader = csv.reader(dat,delimiter="\t")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
158 for line in dat_reader :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
159 uniprotID=line[0] #UniProtID as key
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
160 id_type=line[1] #ID type of corresponding id, key of sub-dictionnary
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
161 cor_id=line[2] #corresponding id
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
162 if "-" not in id_type : #we don't keep isoform
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
163 if id_type in ids and uniprotID in unidict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
164 if id_type in unidict[uniprotID] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
165 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
166 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
167 unidict[uniprotID].update({ id_type : cor_id })
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
168 elif id_type in ids :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
169 unidict[uniprotID]={id_type : cor_id}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
170 os.remove(dat_path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
171
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
172 #print("dat_file ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
173
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
174 #add ids from idmapping.dat to the final tab
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
175 for line in tab[1:] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
176 uniprotID=line[0]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
177 if human :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
178 if uniprotID in unidict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
179 nextprot = access_dictionary(unidict,uniprotID,'neXtProt')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
180 if nextprot != '' : nextprot = clean_nextprot_id(nextprot,line[0])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
181 line.extend([nextprot,access_dictionary(unidict,uniprotID,'BioGrid'),access_dictionary(unidict,uniprotID,'STRING'),
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
182 access_dictionary(unidict,uniprotID,'KEGG')])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
183 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
184 line.extend(["","","",""])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
185 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
186 if uniprotID in unidict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
187 line.extend([access_dictionary(unidict,uniprotID,'BioGrid'),access_dictionary(unidict,uniprotID,'STRING'),
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
188 access_dictionary(unidict,uniprotID,'KEGG')])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
189 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
190 line.extend(["","",""])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
191
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
192 #print ("tab ok")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
193
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
194 #add missing nextprot ID for human
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
195 if human :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
196 #build next_dict
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
197 nextprot_ids = id_list_from_nextprot_ftp("nextprot_ac_list_all.txt",target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
198 next_dict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
199 for nextid in nextprot_ids :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
200 next_dict[nextid.replace("NX_","")] = nextid
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
201 os.remove(os.path.join(target_directory,"nextprot_ac_list_all.txt"))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
202
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
203 #add missing nextprot ID
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
204 for line in tab[1:] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
205 uniprotID=line[0]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
206 nextprotID=line[13]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
207 if nextprotID == '' and uniprotID in next_dict :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
208 line[13]=next_dict[uniprotID]
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
209
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
210 output_file = species+"_id_mapping_"+ time.strftime("%d-%m-%Y") + ".tsv"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
211 path = os.path.join(target_directory,output_file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
212
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
213 with open(path,"w") as out :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
214 w = csv.writer(out,delimiter='\t')
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
215 w.writerows(tab)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
216
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
217 name_dict={"human" : "Homo sapiens", "mouse" : "Mus musculus", "rat" : "Rattus norvegicus"}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
218 name = name_dict[species]+" ("+time.strftime("%d-%m-%Y")+")"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
219
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
220 data_table_entry = dict(value = species+"_id_mapping_"+ time.strftime("%d-%m-%Y"), name = name, path = path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
221 _add_data_table_entry(data_manager_dict, data_table_entry, "id_mapping_tab")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
222
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
223 def download_from_uniprot_ftp(file,target_directory) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
224 ftp_dir = "pub/databases/uniprot/current_release/knowledgebase/idmapping/by_organism/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
225 path = os.path.join(target_directory, file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
226 ftp = ftplib.FTP("ftp.uniprot.org")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
227 ftp.login("anonymous", "anonymous")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
228 ftp.cwd(ftp_dir)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
229 ftp.retrbinary("RETR " + file, open(path, 'wb').write)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
230 ftp.quit()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
231 return (path)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
232
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
233 def id_list_from_nextprot_ftp(file,target_directory) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
234 ftp_dir = "pub/current_release/ac_lists/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
235 path = os.path.join(target_directory, file)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
236 ftp = ftplib.FTP("ftp.nextprot.org")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
237 ftp.login("anonymous", "anonymous")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
238 ftp.cwd(ftp_dir)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
239 ftp.retrbinary("RETR " + file, open(path, 'wb').write)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
240 ftp.quit()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
241 with open(path,'r') as nextprot_ids :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
242 nextprot_ids = nextprot_ids.read().splitlines()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
243 return (nextprot_ids)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
244
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
245 #return '' if there's no value in a dictionary, avoid error
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
246 def access_dictionary (dico,key1,key2) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
247 if key1 in dico :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
248 if key2 in dico[key1] :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
249 return (dico[key1][key2])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
250 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
251 return ("")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
252 #print (key2,"not in ",dico,"[",key1,"]")
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
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
256 #if there are several nextprot ID for one uniprotID, return the uniprot like ID
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
257 def clean_nextprot_id (next_id,uniprotAc) :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
258 if len(next_id.split(";")) > 1 :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
259 tmp = next_id.split(";")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
260 if "NX_"+uniprotAc in tmp :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
261 return ("NX_"+uniprotAc)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
262 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
263 return (tmp[1])
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
264 else :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
265 return (next_id)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
266
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
267
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
268 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
269 # Main function
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
270 #######################################################################################################
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
271 def main():
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
272 parser = argparse.ArgumentParser()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
273 parser.add_argument("--hpa", metavar = ("HPA_OPTION"))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
274 parser.add_argument("--peptideatlas", metavar=("SAMPLE_CATEGORY_ID"))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
275 parser.add_argument("--id_mapping", metavar = ("ID_MAPPING_SPECIES"))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
276 parser.add_argument("-o", "--output")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
277 args = parser.parse_args()
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
278
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
279 data_manager_dict = {}
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
280 # Extract json file params
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
281 filename = args.output
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
282 params = from_json_string(open(filename).read())
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
283 target_directory = params[ 'output_data' ][0]['extra_files_path']
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
284 os.mkdir(target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
285
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
286 ## Download source files from HPA
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
287 try:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
288 hpa = args.hpa
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
289 except NameError:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
290 hpa = None
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
291 if hpa is not None:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
292 #target_directory = "/projet/galaxydev/galaxy/tools/proteore/ProteoRE/tools/resources_building/test-data/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
293 hpa = hpa.split(",")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
294 for hpa_tissue in hpa:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
295 HPA_sources(data_manager_dict, hpa_tissue, target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
296
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
297 ## Download source file from Peptide Atlas query
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
298 try:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
299 peptide_atlas = args.peptideatlas
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
300 except NameError:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
301 peptide_atlas = None
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
302 if peptide_atlas is not None:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
303 #target_directory = "/projet/galaxydev/galaxy/tools/proteore/ProteoRE/tools/resources_building/test-data/"
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
304 peptide_atlas = peptide_atlas.split(",")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
305 for pa_tissue in peptide_atlas:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
306 peptide_atlas_sources(data_manager_dict, pa_tissue, target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
307
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
308 ## Download ID_mapping source file from Uniprot
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
309 try:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
310 id_mapping=args.id_mapping
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
311 except NameError:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
312 id_mapping = None
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
313 if id_mapping is not None:
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
314 id_mapping = id_mapping .split(",")
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
315 for species in id_mapping :
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
316 id_mapping_sources(data_manager_dict, species, target_directory)
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
317
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
318 #save info to json file
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
319 filename = args.output
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
320 open(filename, 'wb').write(to_json_string(data_manager_dict))
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
321
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
322 if __name__ == "__main__":
2de84fea8367 planemo upload commit d703392579d96e480c6461ce679516b12cefb3de-dirty
dchristiany
parents:
diff changeset
323 main()