Spaces:
Sleeping
Sleeping
File size: 9,324 Bytes
42f26af |
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
""" parse_utils.py """
from pathlib import Path
from typing import Tuple, List, Optional
from itertools import groupby
from tqdm import tqdm
import numpy as np
import pandas as pd
def parse_spectra(spectra_file: str) -> Tuple[dict, List[Tuple[str, np.ndarray]]]:
"""parse_spectra.
Parses spectra in the SIRIUS format and returns
Args:
spectra_file (str): Name of spectra file to parse
Return:
Tuple[dict, List[Tuple[str, np.ndarray]]]: metadata and list of spectra
tuples containing name and array
"""
lines = [i.strip() for i in open(spectra_file, "r").readlines()]
group_num = 0
metadata = {}
spectras = []
my_iterator = groupby(
lines, lambda line: line.startswith(">") or line.startswith("#")
)
for index, (start_line, lines) in enumerate(my_iterator):
group_lines = list(lines)
subject_lines = list(next(my_iterator)[1])
# Get spectra
if group_num > 0:
spectra_header = group_lines[0].split(">")[1]
peak_data = [
[float(x) for x in peak.split()[:2]]
for peak in subject_lines
if peak.strip()
]
# Check if spectra is empty
if len(peak_data):
peak_data = np.vstack(peak_data)
# Add new tuple
spectras.append((spectra_header, peak_data))
# Get meta data
else:
entries = {}
for i in group_lines:
if " " not in i:
continue
elif i.startswith("#INSTRUMENT TYPE"):
key = "#INSTRUMENT TYPE"
val = i.split(key)[1].strip()
entries[key[1:]] = val
else:
start, end = i.split(" ", 1)
start = start[1:]
while start in entries:
start = f"{start}'"
entries[start] = end
metadata.update(entries)
group_num += 1
metadata["_FILE_PATH"] = spectra_file
metadata["_FILE"] = Path(spectra_file).stem
return metadata, spectras
def spec_to_ms_str(
spec: List[Tuple[str, np.ndarray]], essential_keys: dict, comments: dict = {}
) -> str:
"""spec_to_ms_str.
Turn spec ars and info dicts into str for output file
Args:
spec (List[Tuple[str, np.ndarray]]): spec
essential_keys (dict): essential_keys
comments (dict): comments
Returns:
str:
"""
def pair_rows(rows):
return "\n".join([f"{i} {j}" for i, j in rows])
header = "\n".join(f">{k} {v}" for k, v in essential_keys.items())
comments = "\n".join(f"#{k} {v}" for k, v in essential_keys.items())
spec_strs = [f">{name}\n{pair_rows(ar)}" for name, ar in spec]
spec_str = "\n\n".join(spec_strs)
output = f"{header}\n{comments}\n\n{spec_str}"
return output
def build_mgf_str(
meta_spec_list: List[Tuple[dict, List[Tuple[str, np.ndarray]]]],
merge_charges=True,
parent_mass_keys=["PEPMASS", "parentmass", "PRECURSOR_MZ"],
) -> str:
"""build_mgf_str.
Args:
meta_spec_list (List[Tuple[dict, List[Tuple[str, np.ndarray]]]]): meta_spec_list
Returns:
str:
"""
entries = []
for meta, spec in tqdm(meta_spec_list):
str_rows = ["BEGIN IONS"]
# Try to add precusor mass
for i in parent_mass_keys:
if i in meta:
pep_mass = float(meta.get(i, -100))
str_rows.append(f"PEPMASS={pep_mass}")
break
for k, v in meta.items():
str_rows.append(f"{k.upper().replace(' ', '_')}={v}")
if merge_charges:
spec_ar = np.vstack([i[1] for i in spec])
spec_ar = np.vstack([i for i in sorted(spec_ar, key=lambda x: x[0])])
else:
raise NotImplementedError()
str_rows.extend([f"{i} {j}" for i, j in spec_ar])
str_rows.append("END IONS")
str_out = "\n".join(str_rows)
entries.append(str_out)
full_out = "\n\n".join(entries)
return full_out
def parse_spectra_msp(
mgf_file: str, max_num: Optional[int] = None
) -> List[Tuple[dict, List[Tuple[str, np.ndarray]]]]:
"""parse_spectr_msp.
Parses spectra in the MSP file format
Args:
mgf_file (str) : str
max_num (Optional[int]): If set, only parse this many
Return:
List[Tuple[dict, List[Tuple[str, np.ndarray]]]]: metadata and list of spectra
tuples containing name and array
"""
key = lambda x: x.strip().startswith("PEPMASS")
parsed_spectra = []
with open(mgf_file, "r", encoding="utf-8") as fp:
for (is_header, group) in tqdm(groupby(fp, key)):
if is_header:
continue
meta = dict()
spectra = []
# Note: Sometimes we have multiple scans
# This mgf has them collapsed
cur_spectra_name = "spec"
cur_spectra = []
group = list(group)
for line in group:
line = line.strip()
if not line:
pass
elif ":" in line:
k, v = [i.strip() for i in line.split(":", 1)]
meta[k] = v
else:
mz, intens = line.split()
cur_spectra.append((float(mz), float(intens)))
if len(cur_spectra) > 0:
cur_spectra = np.vstack(cur_spectra)
spectra.append((cur_spectra_name, cur_spectra))
parsed_spectra.append((meta, spectra))
else:
pass
# print("no spectra found for group: ", "".join(group))
if max_num is not None and len(parsed_spectra) > max_num:
# print("Breaking")
break
return parsed_spectra
def parse_spectra_mgf(
mgf_file: str, max_num: Optional[int] = None
) -> List[Tuple[dict, List[Tuple[str, np.ndarray]]]]:
"""parse_spectr_mgf.
Parses spectra in the MGF file formate, with
Args:
mgf_file (str) : str
max_num (Optional[int]): If set, only parse this many
Return:
List[Tuple[dict, List[Tuple[str, np.ndarray]]]]: metadata and list of spectra
tuples containing name and array
"""
key = lambda x: x.strip() == "BEGIN IONS"
parsed_spectra = []
with open(mgf_file, "r") as fp:
for (is_header, group) in tqdm(groupby(fp, key)):
if is_header:
continue
meta = dict()
spectra = []
# Note: Sometimes we have multiple scans
# This mgf has them collapsed
cur_spectra_name = "spec"
cur_spectra = []
group = list(group)
for line in group:
line = line.strip()
if not line:
pass
elif line == "END IONS" or line == "BEGIN IONS":
pass
elif "=" in line:
k, v = [i.strip() for i in line.split("=", 1)]
meta[k] = v
else:
mz, intens = line.split()
cur_spectra.append((float(mz), float(intens)))
if len(cur_spectra) > 0:
cur_spectra = np.vstack(cur_spectra)
spectra.append((cur_spectra_name, cur_spectra))
parsed_spectra.append((meta, spectra))
else:
pass
# print("no spectra found for group: ", "".join(group))
if max_num is not None and len(parsed_spectra) > max_num:
# print("Breaking")
break
return parsed_spectra
def parse_tsv_spectra(spectra_file: str) -> List[Tuple[str, np.ndarray]]:
"""parse_tsv_spectra.
Parses spectra returned from sirius fragmentation tree
Args:
spectra_file (str): Name of spectra tsv file to parse
Return:
List[Tuple[str, np.ndarray]]]: list of spectra
tuples containing name and array. This is used to maintain
consistency with the parse_spectra output
"""
output_spec = []
with open(spectra_file, "r") as fp:
for index, line in enumerate(fp):
if index == 0:
continue
line = line.strip().split("\t")
intensity = float(line[1])
exact_mass = float(line[3])
output_spec.append([exact_mass, intensity])
output_spec = np.array(output_spec)
return_obj = [("sirius_spec", output_spec)]
return return_obj
# YZC parse msgym-like formatted data
def parse_spectra_msgym(df):
parsed_spectra = []
for _, row in df.iterrows():
mzs = [float(m) for m in row['mzs'].split(',')]
intensities = [float(i) for i in row['intensities'].split(',')]
cur_spectra = [(m, i) for m, i in zip(mzs, intensities)]
cur_spectra = np.vstack(cur_spectra)
cur_spectra_name = row['spec']
meta = {'ID': cur_spectra_name,
'parentmass': row['parent_mass']}
parsed_spectra.append((meta, [(cur_spectra_name, cur_spectra)]))
return parsed_spectra
|