src/problems/mprt.lisp @ b5923704ce42

MPRT
author Steve Losh <steve@stevelosh.com>
date Thu, 08 Nov 2018 20:36:22 -0500
parents (none)
children b3598dc7ef5c
(in-package :rosalind)

(defparameter *input-mprt*
  "A2Z669
B5ZC00
P07204_TRBM_HUMAN
P20840_SAG1_YEAST")

(defparameter *output-mprt*
  "B5ZC00
85 118 142 306 395
P07204_TRBM_HUMAN
47 115 116 382 409
P20840_SAG1_YEAST
79 109 135 248 306 348 364 402 485 501 614
")

(defparameter *motif-n-glycosylation* "N{P}[ST]{P}")

(defun motif-to-regex (motif)
  "Turn a protein motif shorthand into a PPCRE scanner."
  (-<> motif
    ;; All we have to do is turn {X} into [^X] and compile.
    (ppcre:regex-replace-all "[{]" <> "[^")
    (substitute #\] #\} <>)
    ppcre:create-scanner))

(defun all-matches-dammit (regex target-string)
  "Return a list of start and end positions of all matches of `regex` on `target-string`.

  Unlike `ppcre:all-matches` this will return ALL matches, even if they're
  overlapping.  Example:

    (all-matches-dammit \"a..\" \"aabc\")
    ; =>
    ; (0 3 1 4)

    (ppcre:all-matches \"a..\" \"aabc\")
    ; =>
    ; (0 3) ; dammit

  "
  ;; cl-ppcre
  (iterate
    (with i = 0)
    (for (values start end) = (ppcre:scan regex target-string :start i))
    (while start)
    (collect start)
    (collect end)
    (setf i (1+ start))))


(define-problem mprt (data stream)
    *input-mprt*
    *output-mprt*
  (with-output-to-string (s)
    (iterate
      (with n-glycosylation = (motif-to-regex *motif-n-glycosylation*))
      (for id :in-stream data :using #'read-line)
      (for (nil . protein) = (uniprot id))
      (for matches = (all-matches-dammit n-glycosylation protein))
      (when matches
        (format s "~A~%~{~D~*~^ ~}~%" id (mapcar #'1+ matches))))))