-
Notifications
You must be signed in to change notification settings - Fork 3
/
pxd2url.py
190 lines (160 loc) · 6.87 KB
/
pxd2url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#py3.5
import requests
import json
import sys
from collections import defaultdict
pride_projects_url = 'http://wwwdev.ebi.ac.uk/pride/ws/archive/projects'
i = 0
stop_cnd = False
num_pages = sys.maxsize
projects = dict()
while not stop_cnd:
payload = {'sortDirection': 'ASC', 'pageSize': 15, 'page': i}
r = requests.get(pride_projects_url, params=payload)
data = r.json()
if r.status_code == 200 and data.get('page', {}):
#TODO failsafe with page': {'totalElements': 0,
num_pages = data['page']['totalPages']
for prd in data.get('_embedded').get('projects'):
projects[prd['accession']] = prd
i += 1
else:
stop_cnd = True
len(projects)
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(r)
pp.pprint(data)
#############
pride_projectinfo_url = 'http://wwwdev.ebi.ac.uk/pride/ws/archive/projects/{pxd}'
pxd_acc = 'PXD009996'
projectinfo = list()
r = requests.get(pride_projectinfo_url.format(pxd = pxd_acc))
data = r.json()
if r.status_code == 200:
projectinfo = data
else:
print("bah!")
pp.pprint(projectinfo)
#############
pride_projectfiles_url = 'http://wwwdev.ebi.ac.uk/pride/ws/archive/projects/{pxd}/files'
pxd_acc = 'PXD009996'
i = 0
stop_cnd = False
num_pages = sys.maxsize
projectfiles = defaultdict(list)
payload_template = {'sortDirection': 'ASC', 'pageSize': 15} # , 'filter': 'fileName=regex=mzML' #remove filter to avoid 0 result
while not stop_cnd:
payload = {'page': i}.update(payload_template)
r = requests.get(pride_projectfiles_url.format(pxd = pxd_acc), params=payload)
data = r.json()
if r.status_code == 200 and data.get('page', {}) and data.get('_embedded', {}).get('files', {}):
num_pages = data['page']['totalPages']
#TODO failsafe with page': {'totalElements': 0,
for fls in data.get('_embedded').get('files'):
projectfiles[fls['accession']] = fls # PXF00000770344 -> {info}
i += 1
if i >= num_pages:
stop_cnd = True
else:
stop_cnd = True
pp.pprint(projectfiles)
#############
pride_fileinfo_url = 'http://wwwdev.ebi.ac.uk/pride/ws/archive/msruns/{pxf}/'
pxf_acc = 'PXF00000770344'
i = 0
stop_cnd = False
fileinfo = defaultdict(list)
r = requests.get(pride_fileinfo_url.format(pxf = pxf_acc))
data = r.json()
if r.status_code == 200 and pxf_acc in projectfiles:
fileinfo[pxf_acc].update(data)
# update overwrites not none with none! params = urllib.urlencode({k: v for k, v in (('orange', orange), ('apple', apple)) if v is not None})
else:
print("bah!")
pp.pprint(fileinfo)
#############
# get organism, instrument type, experiment type and mzml or raw file
organisms = [ x.get('accession') for x in projectinfo.get('organisms')]
instruments = [ x.get('accession') for x in projectinfo.get('instruments')]
urls = list()
# publicFileLocations - list of dict get all 'value' where 'accession': 'PRIDE:0000469'
# fileCategory - dict check accession PRIDE:0000404 # 'raw'
# or check accession PRIDE:0000409 # 'peak'
for ax,fi in projectfiles.items():
if fi.get('fileCategory').get('accession') == "PRIDE:0000404":
# print(ax)
# print(fi.get('fileName'))
# print([ftp.get('value') for ftp in fi.get('publicFileLocations') if ftp.get('accession')== 'PRIDE:0000469'])
urls.extend([ftp.get('value') for ftp in fi.get('publicFileLocations') if ftp.get('accession')== 'PRIDE:0000469'])
# https://pypi.org/project/pronto/
# Combine two ontos
import pronto
ont = pronto.Ontology('path/to/file.obo')
term = ont['REF:ACCESSION']
ms = pronto.Ontology('https://raw.githubusercontent.com/HUPO-PSI/psi-ms-CV/master/psi-ms.obo')
ms.merge(nmr)
# Find ontology terms with children
import pronto
ont = pronto.Ontology('../ms-tools/oms-develop/share/OpenMS/CV/psi-ms.obo')
for term in ont:
if term.children:
print(term)
# Get all the transitive children of an ontology term
import pronto
ont = pronto.Ontology('path/to/file.obo')
print(ont['RF:XXXXXXX'].rchildren())
############
# pxd based queries in precedence: organism, instrument, #raw
pride_projectfiles_url = 'http://wwwdev.ebi.ac.uk/pride/ws/archive/projects/{pxd}/files'
pride_projectinfo_url = 'http://wwwdev.ebi.ac.uk/pride/ws/archive/projects/{pxd}'
for pxd_acc in sorted(projects.keys())[::-1]:
projectinfo = list()
r = requests.get(pride_projectinfo_url.format(pxd = pxd_acc))
data = r.json()
if r.status_code == 200:
projectinfo = data
else:
print("bah, {p}!".format(p=pxd_acc))
continue
organisms = [ x.get('accession') for x in projectinfo.get('organisms')] # Human (accesion number 9609 )
instruments = [ x.get('accession') for x in projectinfo.get('instruments')] # Q-Exactive (accession number MS:1001911 )
if '9606' not in organisms or 'MS:1001911' not in instruments:
continue
i = 0
stop_cnd = False
num_pages = sys.maxsize
projectfiles = defaultdict(list)
payload_template = {'sortDirection': 'ASC', 'pageSize': 15} # , 'filter': 'fileName=regex=mzML' #remove filter to avoid 0 result
while not stop_cnd:
payload = {'page': i}.update(payload_template)
r = requests.get(pride_projectfiles_url.format(pxd = pxd_acc), params=payload)
data = r.json()
if r.status_code == 200 and data.get('page', {}) and data.get('_embedded', {}).get('files', {}):
num_pages = data['page']['totalPages']
#TODO failsafe with page': {'totalElements': 0,
for fls in data.get('_embedded').get('files'):
projectfiles[fls['accession']] = fls # PXF00000770344 -> {info}
i += 1
if i >= num_pages:
stop_cnd = True
else:
stop_cnd = True
urls = list()
# publicFileLocations - list of dict get all 'value' where 'accession': 'PRIDE:0000469'
# fileCategory - dict check accession PRIDE:0000404 # 'raw'
# or check accession PRIDE:0000409 # 'peak'
for ax,fi in projectfiles.items():
if fi.get('fileCategory').get('accession') == "PRIDE:0000404":
# print(ax)
# print(fi.get('fileName'))
# print([ftp.get('value') for ftp in fi.get('publicFileLocations') if ftp.get('accession')== 'PRIDE:0000469'])
urls.extend([ftp.get('value') for ftp in fi.get('publicFileLocations') if ftp.get('accession')== 'PRIDE:0000469'])
if len(urls) < 6 and len(urls) > 3:
pp.pprint(pxd_acc)
pp.pprint(projectfiles)
break
# >>> pxd_acc
# 'PXD011124'
# >>> urls
# ['ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2018/09/PXD011124/Service_ElAffar_300816_15.raw', 'ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2018/09/PXD011124/Service_ElAffar_300816_14.raw', 'ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2018/09/PXD011124/Service_ElAffar_300816_13.raw', 'ftp://ftp.pride.ebi.ac.uk/pride/data/archive/2018/09/PXD011124/Service_ElAffar_300816_16.raw']