| 1 |
.. code-block:: python |
| 2 |
|
| 3 |
#!/usr/bin/env python |
| 4 |
|
| 5 |
""" |
| 6 |
|
| 7 |
Check for presence of example files and download them. |
| 8 |
Just call the function open_example() |
| 9 |
|
| 10 |
For using the correct output directory just state get_example_file.OUTPUT_DIR |
| 11 |
|
| 12 |
""" |
| 13 |
import hashlib |
| 14 |
import os.path |
| 15 |
import sys |
| 16 |
|
| 17 |
|
| 18 |
SHA256_DICT = { |
| 19 |
"deconvolution.mzML.gz": ( |
| 20 |
"19399e4f87d8937cf7be32bab7fd8889700e119cbc2ede215270cbfc9913b394", |
| 21 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 22 |
), |
| 23 |
"dta_example.mzML": ( |
| 24 |
"2cd7207d5106ee5fb3ab7867961a6c1ad02f0ef72da19532e7ebb41c8d010b1a", |
| 25 |
"http://dev.thep.lu.se/fp6-prodac/export/80/trunk/mzML/", |
| 26 |
), |
| 27 |
"neutral_loss_example_1.1.0.mzML": ( |
| 28 |
"5911fa5d91a435aedd7be5a2ea09272f437d44f350948179a96146705a177a16", |
| 29 |
"http://dev.thep.lu.se/fp6-prodac/export/80/trunk/mzML/scans/", |
| 30 |
), |
| 31 |
"precursor_spectrum_example_1.1.0.mzML": ( |
| 32 |
"783187ec7eb2ee2713aecd9f6032508caad3439afa0b4a20232480b33472c17b", |
| 33 |
"http://dev.thep.lu.se/fp6-prodac/export/80/trunk/mzML/scans/", |
| 34 |
), |
| 35 |
"profile-mass-spectrum.mzml": ( |
| 36 |
"2e952de2a25db421f3e30d8c659acc6c3f5e862e4d0fd32ec24c7c606c9a62fd", |
| 37 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 38 |
), |
| 39 |
"small-1.0.0.mzml": ( |
| 40 |
"9e15d6d2eba26932e4d5c61f92df7771da0bbb8589b7237d05bd915017dbf82f", |
| 41 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 42 |
), |
| 43 |
"BSA1.mzML": ( |
| 44 |
"d4bde93c77ec9e948cc62f4c022b8d54591073fd1170e264b69a79dc8d259830", |
| 45 |
"http://open-ms.svn.sourceforge.net/viewvc/open-ms/OpenMS/share/OpenMS/examples/BSA/", |
| 46 |
), |
| 47 |
"proteomeDiscoverer.mzML.gz": ( |
| 48 |
"7fdd45bfcb6d82b2a75abd63fdc0d8cc1f14c1bd717ba2f3d4eec87ec201f3be", |
| 49 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 50 |
), |
| 51 |
"32-bit.mzML.gz": ( |
| 52 |
"199d2611c9894f2a07fb734fbe4db2226cd2a43e806d4e9662b66b979f7df264", |
| 53 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 54 |
), |
| 55 |
"32-bit-compressed.mzML.gz": ( |
| 56 |
"30fdde9ebf3bb145a60ea589f466cb0db8b3b2c451646156db3e56ffe1bf0e59", |
| 57 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 58 |
), |
| 59 |
"64-bit.mzML.gz": ( |
| 60 |
"0d56a2295096e7499a8a90509661d22d4a6a097ad58f3c03310804c9698113aa", |
| 61 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 62 |
), |
| 63 |
"small.pwiz.1.1.mzML": ( |
| 64 |
"87ddde776c9933857edb22f404116f23e85ca8bddadc84d10308eb8853a58768", |
| 65 |
"http://proteowizard.sourceforge.net/example_data/", |
| 66 |
), |
| 67 |
"MRM_example_1.1.0.mzML": ( |
| 68 |
"8140d77921be6fe49e73d71536b211ad39ae48f0272d4a465da2dd202185bdd9", |
| 69 |
"http://dev.thep.lu.se/fp6-prodac/export/80/trunk/mzML/scans/", |
| 70 |
), |
| 71 |
"emptyScan.mzML.gz": ( |
| 72 |
"4d82ea02c1f938e8fbe97b907371aad0eceee074bbee37f7513374a1bb7c3f3b", |
| 73 |
"http://www.uni-muenster.de/hippler/pymzml/example_mzML_files/", |
| 74 |
), |
| 75 |
} |
| 76 |
|
| 77 |
EXAMPLE_DIR = os.path.join(os.path.dirname(__file__), "mzML_example_files") |
| 78 |
OUTPUT_DIR = os.path.join(os.path.dirname(__file__), "output") |
| 79 |
|
| 80 |
try: |
| 81 |
os.makedirs(EXAMPLE_DIR) |
| 82 |
except OSError: |
| 83 |
pass |
| 84 |
try: |
| 85 |
os.makedirs(OUTPUT_DIR) |
| 86 |
except OSError: |
| 87 |
pass |
| 88 |
|
| 89 |
|
| 90 |
def open_example(filename): |
| 91 |
""" |
| 92 |
filename = filename of a file in the example directory |
| 93 |
If the file "filename" is not present in the example directory, the file is downloaded. |
| 94 |
Path with filename is returned. |
| 95 |
""" |
| 96 |
try: |
| 97 |
SHA256_DICT[filename] |
| 98 |
except KeyError: |
| 99 |
print('File "{0}" not available.'.format(filename), file=sys.stderr) |
| 100 |
exit(1) |
| 101 |
|
| 102 |
filename_with_path = os.path.join(EXAMPLE_DIR, filename) |
| 103 |
# check file presence |
| 104 |
checked_file = check_file(filename_with_path, filename) |
| 105 |
if checked_file: |
| 106 |
return filename_with_path |
| 107 |
|
| 108 |
# file is not present or corrupt |
| 109 |
server_url = SHA256_DICT[filename][1] |
| 110 |
url = server_url + filename |
| 111 |
if download(filename_with_path, url): |
| 112 |
checked_file = check_file(filename_with_path, filename) |
| 113 |
if checked_file: |
| 114 |
return filename_with_path |
| 115 |
else: |
| 116 |
os.remove(filename_with_path) |
| 117 |
print("Downloaded file is corrupt. Please try again.", file=sys.stderr) |
| 118 |
exit(1) |
| 119 |
|
| 120 |
exit(1) |
| 121 |
|
| 122 |
|
| 123 |
def check_file(filename_with_path, filename): |
| 124 |
# check file presence |
| 125 |
if os.path.isfile(filename_with_path): |
| 126 |
# file exists, check SHA224 sum |
| 127 |
file = open(filename_with_path, "rb") |
| 128 |
if check_sha256(file, filename): |
| 129 |
return True |
| 130 |
|
| 131 |
|
| 132 |
def check_sha256(file, filename): |
| 133 |
sha256_sum = SHA256_DICT[filename][0] |
| 134 |
|
| 135 |
if hashlib.sha256(file.read()).hexdigest() == sha256_sum: |
| 136 |
return True |
| 137 |
else: |
| 138 |
return False |
| 139 |
|
| 140 |
|
| 141 |
def calc_sha256(file): |
| 142 |
file = open(file, "rb") |
| 143 |
return hashlib.sha256(file.read()).hexdigest() |
| 144 |
|
| 145 |
|
| 146 |
def download(filename_with_path, url): |
| 147 |
""" |
| 148 |
filename_with_path = path and filename, where to save downloaded file |
| 149 |
url = url to download |
| 150 |
Path with filename is returned. |
| 151 |
""" |
| 152 |
|
| 153 |
if sys.version_info[0] < 3: |
| 154 |
from urllib2 import Request, urlopen, URLError, HTTPError |
| 155 |
else: |
| 156 |
from urllib.request import Request, urlopen, URLError, HTTPError |
| 157 |
|
| 158 |
print("Downloading file ...", end="\r", file=sys.stderr) |
| 159 |
req = Request(url) |
| 160 |
print("Downloading file completed.", file=sys.stderr) |
| 161 |
try: |
| 162 |
response = urlopen(req) |
| 163 |
with open(filename_with_path, "wb") as f: |
| 164 |
f.write(response.read()) |
| 165 |
return filename_with_path |
| 166 |
except HTTPError: |
| 167 |
print( |
| 168 |
"HTTP Error: The server could not fulfill the request.", |
| 169 |
"Possibly, the file is not found on the server.", |
| 170 |
file=sys.stderr, |
| 171 |
) |
| 172 |
exit(1) |
| 173 |
except URLError: |
| 174 |
print( |
| 175 |
"URL Error: Server not available. Please check your internet connection.", |
| 176 |
file=sys.stderr, |
| 177 |
) |
| 178 |
exit(1) |
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__": |
| 182 |
print(__doc__) |