forked from abi-am/omicss-25
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrosalind_strong_set.Rmd
More file actions
358 lines (271 loc) · 9.1 KB
/
Copy pathrosalind_strong_set.Rmd
File metadata and controls
358 lines (271 loc) · 9.1 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
---
title: "Rosalind Strong Set — Bioinformatics Problems in R"
author: "ABI Omics"
output:
html_document:
toc: true
toc_float: true
number_sections: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
# Overview
This set covers classical Rosalind DNA/RNA string problems. Work through them in order;
later problems reuse ideas from earlier ones.
**Strongly recommended problems**
| ID | Title |
|----|-------|
| DNA | Counting DNA Nucleotides |
| RNA | Transcribing DNA into RNA |
| REVC | Complementing a Strand of DNA |
| HAMM | Counting Point Mutations |
| GC | Computing GC Content |
| SUBS | Finding a Motif in DNA |
| CONS | Consensus and Profile |
| PROT | Translating RNA into Protein |
**Bonus (apply-family deep dive)**
| ID | Title |
|----|-------|
| SPLC | RNA Splicing |
> **Hint on style.** In several of these problems your first instinct may be a `for` loop.
> Before you reach for one, ask whether `sapply`, `lapply`, `vapply`, `mapply`, or
> `apply` can express the same idea more cleanly. The apply family is one of the
> clearest practical differences between R and Python — and it *is* useful here.
Helper ideas you may want (you do not have to use these names):
```{r helpers-sketch}
# Split a DNA/RNA string into a character vector of bases
chars <- function(s) strsplit(s, "")[[1]]
# Very small FASTA parser: returns a named character vector of sequences
# (concatenates multi-line sequence blocks)
parse_fasta <- function(text) {
lines <- strsplit(text, "\n")[[1]]
lines <- lines[nzchar(lines)]
ids <- character()
seqs <- character()
current_id <- NULL
current_seq <- character()
flush <- function() {
if (!is.null(current_id)) {
ids <<- c(ids, current_id)
seqs <<- c(seqs, paste(current_seq, collapse = ""))
}
}
for (line in lines) {
if (startsWith(line, ">")) {
flush()
current_id <- sub("^>", "", line)
current_seq <- character()
} else {
current_seq <- c(current_seq, line)
}
}
flush()
setNames(seqs, ids)
}
```
For each problem: read the sample dataset, write your solution in the code chunk,
then check against the sample output. When you are ready, grab a dataset from
[Rosalind](https://rosalind.info) and submit.
---
# DNA — Counting DNA Nucleotides
**Problem.** Given a DNA string $s$ of length at most 1000 nt, return four integers
counting the respective number of times that the symbols **A**, **C**, **G**, and **T**
occur in $s$.
**Sample dataset**
```{r dna-data}
dna <- "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"
```
**Sample output:** `20 12 17 21`
```{r dna-solution}
# Your solution here.
# Prefer something apply-/vapply-shaped over an explicit for loop counting each base.
```
---
# RNA — Transcribing DNA into RNA
**Problem.** Given a DNA string $t$ corresponding to a coding strand, return its
transcribed RNA string $u$ (replace every **T** with **U**).
**Sample dataset**
```{r rna-data}
dna <- "GATGGAACTTGACTACGTAAATT"
```
**Sample output:** `GAUGGAACUUGACUACGUAAAUU`
```{r rna-solution}
# Your solution here.
```
---
# REVC — Complementing a Strand of DNA
**Problem.** Given a DNA string, return its reverse complement.
Complement: A↔T, C↔G. Then reverse the resulting string.
**Sample dataset**
```{r revc-data}
dna <- "AAAACCCGGT"
```
**Sample output:** `ACCGGGTTTT`
```{r revc-solution}
# Your solution here.
# One clean route: chars -> complement map -> reverse -> paste.
```
---
# HAMM — Counting Point Mutations
**Problem.** Given two DNA strings $s$ and $t$ of equal length, return the Hamming
distance $d_H(s, t)$ (number of differing positions).
**Sample dataset**
```{r hamm-data}
s <- "GAGCCTACTAACGGGAT"
t <- "CATCGTAATGACGGCCT"
```
**Sample output:** `7`
```{r hamm-solution}
# Your solution here.
# Vectorized comparison (or mapply) beats a for loop that walks both strings.
```
---
# GC — Computing GC Content
**Problem.** Given a multi-FASTA of DNA strings, return the ID of the string with
the highest GC-content, followed by its GC-content as a percentage.
GC-content of a string is $100 \times \frac{\#(G) + \#(C)}{\text{length}}$.
**Sample dataset**
```{r gc-data}
gc_fasta <- ">Rosalind_6404
CCTGCGGAAGATCGGCACTAGAATAGCCAGAACCGTTTCTCTGAGGCTTCCGGCCTTCCC
TCCCACTAATAATTCTGAGG
>Rosalind_5959
CCATCGGTAGCGCATCCTTAGTCCAATTAAGTCCCTATCCAGGCGACGAAAGTTACAAG
>Rosalind_0808
CCACCCTCGTGGTATGGCTAGGCATTCAGGAACCGGAGAACGCTTCAGACCAGCCCGGAC
TGGGAACCTGCGGGCAGTAGGTGGAAT"
```
**Sample output**
```
Rosalind_0808
60.919540
```
(Any precision comparable to this is fine for practice; Rosalind accepts rounding error.)
```{r gc-solution}
# Your solution here.
# Natural apply pattern: parse FASTA -> sapply each sequence for GC% -> which.max.
```
---
# SUBS — Finding a Motif in DNA
**Problem.** Given two DNA strings $s$ and $t$ (with $t$ a motif of $s$), return all
starting locations of $t$ in $s$. Locations use **1-based** indexing.
**Sample dataset**
```{r subs-data}
s <- "GATATATGCATATACTT"
t <- "ATAT"
```
**Sample output:** `2 4 10`
```{r subs-solution}
# Your solution here.
# Sliding window: sapply over start positions, keep those where the substring matches.
```
---
# CONS — Consensus and Profile
**Problem.** Given a collection of DNA strings of equal length in FASTA format,
return:
1. A consensus string (most frequent base at each position; ties may be broken any way)
2. The profile matrix (counts of A, C, G, T at each position), labeled rows
**Sample dataset**
```{r cons-data}
cons_fasta <- ">Rosalind_1
ATCCAGCT
>Rosalind_2
GGGCAACT
>Rosalind_3
ATGGATCT
>Rosalind_4
AAGCAACC
>Rosalind_5
TTGGAACT
>Rosalind_6
ATGCCATT
>Rosalind_7
ATGGCACT"
```
**Sample output** (one valid consensus; profile is unique)
```
ATGCAACT
A: 5 1 0 0 5 5 0 0
C: 0 0 1 4 2 0 6 1
G: 1 1 6 3 0 1 0 0
T: 1 5 0 0 0 1 1 6
```
```{r cons-solution}
# Your solution here.
# Think in matrices: split each sequence into chars, rbind into a matrix,
# then apply(..., 2, ...) down each column for the profile / consensus.
```
---
# PROT — Translating RNA into Protein
**Problem.** Given an RNA string corresponding to a protein-coding ORF (length
divisible by 3), translate it into its protein string using the standard genetic
code. Stop when you hit a stop codon; do **not** include the stop symbol.
**Sample dataset**
```{r prot-data}
rna <- "AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA"
```
**Sample output:** `MAMAPRTEINSTRING`
Codon table (RNA → amino acid; `*` = stop):
```{r codon-table}
codon_table <- c(
UUU="F", UUC="F", UUA="L", UUG="L",
UCU="S", UCC="S", UCA="S", UCG="S",
UAU="Y", UAC="Y", UAA="*", UAG="*",
UGU="C", UGC="C", UGA="*", UGG="W",
CUU="L", CUC="L", CUA="L", CUG="L",
CCU="P", CCC="P", CCA="P", CCG="P",
CAU="H", CAC="H", CAA="Q", CAG="Q",
CGU="R", CGC="R", CGA="R", CGG="R",
AUU="I", AUC="I", AUA="I", AUG="M",
ACU="T", ACC="T", ACA="T", ACG="T",
AAU="N", AAC="N", AAA="K", AAG="K",
AGU="S", AGC="S", AGA="R", AGG="R",
GUU="V", GUC="V", GUA="V", GUG="V",
GCU="A", GCC="A", GCA="A", GCG="A",
GAU="D", GAC="D", GAA="E", GAG="E",
GGU="G", GGC="G", GGA="G", GGG="G"
)
translate_rna <- function(mRNA, RNA_prot_table=codon_table) {
n_codons <- length(mRNA) / 3
starts <- seq(1, by = 3, length.out = n_codons)
codons <- substring(rna, starts, starts + 2L)
aas <- codon_table[codons]
# Drop from the first stop codon onward
stop_at <- match("*", aas)
if (!is.na(stop_at)) aas <- aas[seq_len(stop_at - 1L)]
paste(aas, collapse = "")
}
```
```{r prot-solution}
# Your solution here.
# Slice the RNA into triplets, then sapply / vapply each codon through codon_table.
```
---
# Bonus: SPLC — RNA Splicing
**Problem.** Given a DNA string $s$ (coding strand) and a collection of substrings
of $s$ serving as **introns**, remove all introns from $s$, transcribe the remaining
exons to RNA, and translate to protein (as in PROT).
**Sample dataset**
```{r splc-data}
splc_fasta <- ">Rosalind_10
ATGGTCTACATAGCTGACAAACAGCACGTAGCAATCGGTCGAATCTCGAGAGGCATATGGTCACATGATCGGTCGAGCGTGTTTCAAAGTTTGCGCCTAG
>Rosalind_12
ATCGGTCGAA
>Rosalind_15
ATCGGTCGAGCGTGT"
```
**Sample output:** `MVYIADKQHVASREAYGHMFKVCA`
```{r splc-solution}
# Your solution here.
# Chain earlier pieces: remove introns (lapply/sapply), DNA→RNA, then PROT translation.
# This is a good place to reuse functions you already wrote above.
```
---
# Optional stretch
Once the sample datasets pass:
1. Download a Rosalind dataset for each problem and run your functions on it.
2. Refactor shared helpers (`chars`, `parse_fasta`, `translate_rna`, `gc_content`) into
one place at the top of the document and reuse them.
3. For DNA / GC / CONS / PROT / HAMM / SPLC specifically, try rewriting any leftover
`for` loops with apply-family calls and compare readability.