r - 如何制作具有 FASTA 格式参数的 R 函数
问题描述
这是我写的代码。我试图用 FASTA 格式的参数运行这个函数,但它不适用于这个。请帮助我更改代码中的某些内容,通过它可以轻松地使用 FASTA 格式的参数进行计算
I= 131.1736
L= 131.1736
K= 146.1882
M= 149.2124
F= 165.19
T= 119.1197
W= 204.2262
V= 117.1469
R= 174.2017
H= 155.1552
A= 89.0935
N= 132.1184
D= 133.1032
C= 121.159
E= 147.1299
Q= 146.1451
G= 75.0669
P= 115.131
S= 105.093
Y= 181.1894
ref=c(I= 131.1736, L= 131.1736, K= 146.1882, M= 149.2124, F= 165.19, T= 119.1197, W= 204.2262, V= 117.1469, R= 174.2017, H= 155.1552, A= 89.0935, N= 132.1184, D= 133.1032, C= 121.159, E= 147.1299, Q= 146.1451, G= 75.0669, P= 115.131, S= 105.093, Y= 181.1894)
#function to calculate molecular weights of amino acids, could be used by using ("",ref=ref)
mw=function(compound)
{
molecules=unlist(strsplit(compound,""))
ans=sum(ref[molecules])-((nchar(compound)-1)*18)
return(c("Molecular Weight:"=ans))
}
解决方案
使用来自链接fasta"\n"
的数据,需要删除新行。添加了一个过滤器以删除任何不在ref中的字符。
# reference lookup
ref <- c(I = 131.1736, L = 131.1736, K = 146.1882, M = 149.2124, F = 165.19,
T = 119.1197, W = 204.2262, V = 117.1469, R = 174.2017, H = 155.1552,
A = 89.0935, N = 132.1184, D = 133.1032, C = 121.159, E = 147.1299,
Q = 146.1451, G = 75.0669, P = 115.131, S = 105.093, Y = 181.1894)
mw <- function(compound){
molecules <- unlist(strsplit(compound, ""))
# keep only letters that appear in ref
molecules <- molecules[ molecules %in% names(ref) ]
ans <- sum(ref[molecules]) - ((nchar(compound)-1) * 18)
#return
paste("Molecular Weight: =", ans)
}
mw("ILK")
# [1] "Molecular Weight: = 372.5354"
mw("MNSMADTDRVNLTPIQRASEKSVQYHLKQVIGRGSYGVVYKAINKHTDQVVAIKEVVYENDEELNDIMAEISLLKNLNHNNIVKYHGFIRKSYELYILLE")
# [1] "Molecular Weight: = 11583.6644"
mw(c("ILL", "LKKS"))
# [1] "Molecular Weight: = 886.1638" "Molecular Weight: = 868.1638"
# this one has "new line \n" which will be dropped
mw("MN\nISLL")
# [1] "Molecular Weight: = 671.9446"
推荐阅读
- redux-saga - redux-saga 中的 takeSequential?
- ironpython - 如何在 spotfire 中以彩色或任何 python 脚本显示前 5 个客户 ID
- c# - 从另一个字符串c#中删除一个字符串
- ruby-on-rails - 初始化程序 rspec 测试通过,但手动测试时实际代码中断
- qt - Qt线程中exec()之后的代码没有实现
- xamarin.forms - 如何在 Xamarin Forms 中添加注册进度跟踪器?
- javascript - 使用 NodeJS 更新图像
- html - 使用 curl 命令发布文件
- material-ui - 具有特定日期格式的 Material-UI 文本字段
- reactjs - 为什么不允许带有接口的 defaultProps ?在打字稿中标记