| 1 |
|
| 2 |
""" |
| 3 |
Additional functions for converting file etc. |
| 4 |
|
| 5 |
@author M. Kösters |
| 6 |
""" |
| 7 |
|
| 8 |
|
| 9 |
n |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
from pymzml.utils.GSGW import GSGW |
| 31 |
import pymzml.regex_patterns as regex_patterns |
| 32 |
import re |
| 33 |
import gzip |
| 34 |
|
| 35 |
|
| 36 |
def index_gzip(pathIn, pathOut, max_idx=10000, idx_len=8, verbose=False, comp_str=-1): |
| 37 |
""" |
| 38 |
Convert an mzml file (can be gzipped) into an indexed, gzipped mzML file. |
| 39 |
|
| 40 |
Arguments: |
| 41 |
pathIn (str): path to an mzML input File. |
| 42 |
pathOut (str): path were the index gzip will be created. |
| 43 |
|
| 44 |
Keyword Arguments: |
| 45 |
max_idx (int): number of indexes which can be saved. |
| 46 |
idx_len (int): character len of on key |
| 47 |
verbose (boolean): print progress while parsing input. |
| 48 |
comp_str(int): compression strength of zlib compression, |
| 49 |
needs to be 1 <= x <= 9 |
| 50 |
""" |
| 51 |
if pathIn.endswith("gz"): |
| 52 |
fileOpen = gzip.open |
| 53 |
elif pathIn.lower().endswith("mzml"): |
| 54 |
fileOpen = open |
| 55 |
with GSGW( |
| 56 |
output_path=pathOut, |
| 57 |
max_idx=max_idx, |
| 58 |
max_idx_len=idx_len, |
| 59 |
max_offset_len=idx_len, |
| 60 |
comp_str=comp_str, |
| 61 |
) as Writer: |
| 62 |
with fileOpen(pathIn, "rt") as Reader: |
| 63 |
data = "" |
| 64 |
for line in Reader: |
| 65 |
if line.strip().startswith("</spectrum>"): |
| 66 |
data += line |
| 67 |
Writer.add_data(data, nativeID) |
| 68 |
if verbose: |
| 69 |
print("NativeID : {0}".format(nativeID), end="\r") |
| 70 |
data = "" |
| 71 |
elif line.strip().startswith("<spectrum "): |
| 72 |
data += line |
| 73 |
lineID = re.search(regex_patterns.SPECTRUM_TAG_PATTERN, line).group( |
| 74 |
"index" |
| 75 |
) |
| 76 |
nativeID = int( |
| 77 |
regex_patterns.SPECTRUM_ID_PATTERN.search(lineID).group(1) |
| 78 |
) |
| 79 |
|
| 80 |
elif line.strip().startswith("<chromatogram "): |
| 81 |
data += line |
| 82 |
nativeID = re.search( |
| 83 |
regex_patterns.CHROMATOGRAM_ID_PATTERN, line |
| 84 |
).group(1) |
| 85 |
print("found chromatogram") |
| 86 |
elif line.strip().startswith("<spectrumL"): |
| 87 |
data += line |
| 88 |
Writer.add_data(data, "Head") |
| 89 |
if verbose: |
| 90 |
print("NativeID :", "Head") |
| 91 |
data = "" |
| 92 |
elif line.strip().startswith("<chromatogramL"): |
| 93 |
data += line |
| 94 |
Writer.add_data(data, "junk") |
| 95 |
if verbose: |
| 96 |
print("NativeID :", "junk") |
| 97 |
data = "" |
| 98 |
elif line.strip().startswith("</chromatogram>"): |
| 99 |
data += line |
| 100 |
Writer.add_data(data, nativeID) |
| 101 |
if verbose: |
| 102 |
print("found chromatogram") |
| 103 |
print("NativeID: {0}".format(nativeID)) |
| 104 |
data = "" |
| 105 |
else: |
| 106 |
data += line |
| 107 |
if data: |
| 108 |
Writer.add_data(data, "tail") |
| 109 |
if verbose: |
| 110 |
print("NativeID :", "tail") |
| 111 |
|
| 112 |
Writer.write_index() |
| 113 |
return |
| 114 |
|
| 115 |
|
| 116 |
def index(pathIn, pathOut, max_idx=10000, idx_len=8, verbose=False, comp_str=-1): |
| 117 |
""" |
| 118 |
Convert an mzml file (can be gzipped) into an indexed, gzipped mzML file. |
| 119 |
|
| 120 |
Arguments: |
| 121 |
pathIn (str): path to input File. |
| 122 |
pathOut (str): path were output should be created. |
| 123 |
|
| 124 |
Keyword Arguments: |
| 125 |
max_idx (int): number of indexes which can be saved. |
| 126 |
idx_len (int): character len of on key |
| 127 |
verbose (boolean): print progress while parsing input. |
| 128 |
comp_str(int): compression strength of zlib compression, |
| 129 |
needs to be 1 <= x <= 9 |
| 130 |
""" |
| 131 |
import gzip |
| 132 |
|
| 133 |
with GSGW( |
| 134 |
output_path=pathOut, |
| 135 |
max_idx_len=idx_len, |
| 136 |
max_offset_len=idx_len, |
| 137 |
comp_str=comp_str, |
| 138 |
) as Writer: |
| 139 |
with gzip.open(pathIn, "rt") as Reader: |
| 140 |
data = "" |
| 141 |
for line in Reader: |
| 142 |
if line.strip().startswith("</spectrum>"): |
| 143 |
data += line |
| 144 |
Writer.add_data(data, nativeID) |
| 145 |
if verbose: |
| 146 |
pass |
| 147 |
data = "" |
| 148 |
elif line.strip().startswith("<spectrum "): |
| 149 |
data += line |
| 150 |
lineID = re.search(regex_patterns.SPECTRUM_TAG_PATTERN, line).group( |
| 151 |
"index" |
| 152 |
) |
| 153 |
nativeID = int( |
| 154 |
regex_patterns.SPECTRUM_ID_PATTERN.search(lineID).group(0) |
| 155 |
) |
| 156 |
elif line.strip().startswith("<chromatogram "): |
| 157 |
data += line |
| 158 |
nativeID = re.search( |
| 159 |
regex_patterns.CHROMATOGRAM_ID_PATTERN, line |
| 160 |
).group(1) |
| 161 |
elif line.strip().startswith("<spectrumL"): |
| 162 |
data += line |
| 163 |
Writer.add_data(data, "Head") |
| 164 |
if verbose: |
| 165 |
print("NativeID :", "Head") |
| 166 |
data = "" |
| 167 |
elif line.strip().startswith("<chromatogramL"): |
| 168 |
data += line |
| 169 |
Writer.add_data(data, "junk") |
| 170 |
if verbose: |
| 171 |
print("NativeID :", "junk") |
| 172 |
data = "" |
| 173 |
elif line.strip().startswith("</chromatogram>"): |
| 174 |
data += line |
| 175 |
Writer.add_data(data, nativeID) |
| 176 |
if verbose: |
| 177 |
print("found chromo") |
| 178 |
print("NativeID :", nativeID, end="\r") |
| 179 |
data = "" |
| 180 |
else: |
| 181 |
data += line |
| 182 |
if data: |
| 183 |
Writer.add_data(data, "tail") |
| 184 |
if verbose: |
| 185 |
print("NativeID :", "tail") |
| 186 |
Writer.write_index() |
| 187 |
|
| 188 |
|
| 189 |
def make_obo_mapping(obo, reversed=False): |
| 190 |
|
| 191 |
mapping = {} |
| 192 |
with open(obo) as obo_file: |
| 193 |
for line in obo_file: |
| 194 |
if line.startswith("id: "): |
| 195 |
id = line.split()[-1] |
| 196 |
elif line.startswith("name: "): |
| 197 |
mapping[id] = " ".join(line.split()[1:]) |
| 198 |
if reversed: |
| 199 |
mapping = {y: x for x, y in mapping.items()} |
| 200 |
return mapping |
| 201 |
|
| 202 |
|
| 203 |
if __name__ == "__main__": |
| 204 |
print(__doc__) |