Last active
January 26, 2016 19:27
-
-
Save Swarchal/27bcccd5271db755e242 to your computer and use it in GitHub Desktop.
puzzle club 3: Calculating GC content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Computing GC content\n", | |
"---------------------\n", | |
"\n", | |
"Return string with the greatest proportion of GC" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Rosalind_0409\n", | |
"52.4144869215292" | |
] | |
} | |
], | |
"source": [ | |
"data <- seqinr::read.fasta(\"~/Downloads/rosalind_gc.txt\")\n", | |
"\n", | |
"cg_content <- function(x){\n", | |
" out <- table(x)\n", | |
" c <- out[[2]]\n", | |
" g <- out[[3]]\n", | |
" content <- ((c + g) / length(x)) * 100\n", | |
"}\n", | |
"\n", | |
"gc_list <- lapply(data, cg_content)\n", | |
"out <- which.max(lapply(data, cg_content))\n", | |
"cat(paste(names(out), max(unlist(gc_list)), sep = '\\n'))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"-------------------------------------\n", | |
"\n", | |
"## Without using `table()`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Rosalind_0409\n", | |
"52.4144869215292" | |
] | |
} | |
], | |
"source": [ | |
"data <- seqinr::read.fasta(\"~/Downloads/rosalind_gc.txt\")\n", | |
"\n", | |
"col_data <- lapply(data, function(x){paste(x, collapse = \"\")})\n", | |
"\n", | |
"cg_content <- function(x){\n", | |
" chars <- strsplit(x, NULL)[[1]]\n", | |
" c <- summary(factor(chars))[[2]]\n", | |
" g <- summary(factor(chars))[[3]]\n", | |
" out <- ((c + g) / length(chars)) * 100\n", | |
"}\n", | |
"\n", | |
"out <- lapply(col_data, cg_content)\n", | |
"cat(paste(names(which.max(out)), max(unlist(out)), sep = '\\n'))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "R", | |
"language": "R", | |
"name": "ir" | |
}, | |
"language_info": { | |
"codemirror_mode": "r", | |
"file_extension": ".r", | |
"mimetype": "text/x-r-source", | |
"name": "R", | |
"pygments_lexer": "r", | |
"version": "3.2.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment