Database Reference
In-Depth Information
6.
Finally, we create a wrapper function that passes our parser to parse-ez and
conigures the parsing engine the way we need it:
(defn parse-fasta [input]
(parse multi-fasta input
:eof false :auto-trim false))
Now, we can use this function to parse the example record, provided at the beginning of
this recipe:
user=> parse-fasta test-data)
{:defline
"gi|5524211|gb|AAD44166.1| cytochrome b [Elephas maximus maximus]",
:gene-seq
"LCLYTHIGRNIYYGSYLYSETWNTGIMLLLITMATAFMGYVLPWGQMSFWGATVITNLFSAIPYIGTN
LVEWIWGGFSVDKATLNRFFAFHFILPFTMVALAGVHLTFLHETGSNNPLGLTSDSDKIPFHPYYTI
KDFLGLLILILLLLLLALLSPDMLGDPDNHMPADPLNTPLHIKPEWYFLFAYAILRSVPNKLGGVLALF
LSIVILGLMPFLHTSKHRSMMLRPLSQALFWTLTMDLLTLTWIGSQPVEYPYTIIGQMASILYFSII
LAFLPIAGXIENY"}
How it works…
At the most abstract level, parsers are functions. They take a string as input and return a
data structure. More complex, advanced parsers are built by combining simpler elements.
The <| function is a good example of this. It does not parse anything by itself. However, this
function makes it possible to combine two other parsers in a useful way: it parses both parts
and throws away the results of the second.
The acid-code function is an example of how to create a parser from a basic component.
It matches any of the characters in the set.
acid-code-line then combines the acid-code parser. It has to match one or more
acid-code characters, optionally followed by a newline . It uses the <| combinator to
throw away the newline and return the sequence of acid-codes .
This entire parser is built up in this way, by composing complex structures from simple parts.
While this is a very basic parser, it's possible to create quite complex parsers in this way,
leveraging the full power of Clojure while keeping the code readable and maintainable.
Validating data with Valip
Validating data happens so often that it's good to have an EDSL to express the validation rules
that our data has to pass. This makes the rules easier to create, understand, and maintain.
 
Search WWH ::




Custom Search