[英]How to make a R function which has an argument in FASTA format
這是我寫的代碼。 我試圖用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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.