Skip to content

Instantly share code, notes, and snippets.

@meowcat

meowcat/test.R Secret

Last active January 10, 2023 08:39
Show Gist options
  • Save meowcat/9a1c64acdcbfc15298b3af8553379483 to your computer and use it in GitHub Desktop.
Save meowcat/9a1c64acdcbfc15298b3af8553379483 to your computer and use it in GitHub Desktop.
Writing spectra files with templates
NAME: meta.name
GUGUS: gaga
{%- if exists("meta.polarity") -%}POLARITY: {{ meta.polarity }}
{% endif -%}
{%- if exists("meta.antipolarity") -%}ANTIPOLARITY: {{ meta.antipolarity }}
{% endif -%}
{%- for key, value in meta.ms -%}
AC$MASS_SPECTROMETRY: {{key}} {{value}}
{% endfor -%}
Num Peaks: {{ length(peaks) }}
{%- for p in peaks -%}{{p.mz}} {{p.intensity}}{%- if not loop.is_last -%}, {%- endif -%}{%- endfor -%}
library(tidyverse)
library(jinjar)
template <- read_file("test.j")
meta <- list(
name = "Specname",
ms = list(collision = 33, ionization = "[M+H]+", sda="next"),
polarity = "positive"
)
peaks <- tribble(
~ mz, ~ intensity,
123.1234, 111,
234.2345, 222,
555.4321, 1
)
rendered <- template %>% render(meta = meta, peaks = peaks)
write_lines(rendered, "spectrum.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment