我在R.工作.我有一个数据框,其中包含染色体上的起始位置和结束位置(其中整数表示染色体上的坐标)例如:
start end
1 5
3 7
4 10
12 7 (inverted is also allowed)
8 15
Run Code Online (Sandbox Code Playgroud)
我想要的是计算所有这些范围内坐标的出现次数.因此,对于上面的示例,输出将是这样的:
position count
1 1
2 1
3 2
4 3
5 3
6 2
7 3
8 3
9 3
10 3
11 2
12 2
13 1
14 1
15 1
Run Code Online (Sandbox Code Playgroud)
我有62000多个这样的范围,每个范围至少有1000个位置.我知道如何进行这种转换,但我不知道如何有效地做到这一点,即几秒钟.
当前(效率低下的代码)
positions <- c()
for(i in seq(nrow(a))){
positions <- c(positions, seq(a[i,3], a[i,4]))
}
table(positions)
Run Code Online (Sandbox Code Playgroud)
"a"是我的数据框,开始和结束坐标分别在第三和第四列.
数据框中的一列包含字符,因此对于使用apply我要么需要创建一个新的数据框(消耗额外的空间),要么需要在apply函数内转换为整数(额外的时间).对不起,因为之前没有告知此事.
我试图使用if-else表达式,如果if条件失败,它应该打破循环,但是会invalid syntax出错.
示例代码:
a = 5
while True:
print(a) if a > 0 else break
a-=1
Run Code Online (Sandbox Code Playgroud)
当然,如果我用传统方式书写(不使用一个班轮),它就有效.
请让我知道break在else关键字后使用命令有什么问题.
是否可以知道当前matplotlib样式的名称?我知道我可以使用来获取所有可用样式的列表plt.style.available,但是我想要的是获取当前正在使用的样式名称的可能性。我正在使用spyder的ipython控制台python 3.5,但是如果对此的答案取决于我的工作环境,我会感到惊讶:)
我想将lambda表达式与cython一起使用,但不适用于cpdef。该错误表明尚不支持该错误,但是cython changeleg则表明支持lambda表达式。
%%cython
cimport numpy as np
import numpy as np
cdef foo():
a = np.random.randint(1,10,10)
b = sorted(a, key = lambda x: x%np.pi) #Compiles
return(b)
cpdef goo():
a = np.random.randint(1,10,10)
b = sorted(a) #Compiles
return(b)
cpdef hoo():
a = np.random.randint(1,10,10)
b = sorted(a, key = lambda x: x%np.pi) #Compile time error
return(b)
Run Code Online (Sandbox Code Playgroud)
Error compiling Cython file:
------------------------------------------------------------
...
cpdef goo():
a = np.random.randint(1,10,10)
b = sorted(a)
return(b)
cpdef hoo():
^
------------------------------------------------------------
/********/.cache/ipython/cython/_cython_magic_63378538fa4250ed3135e0289d6af7a0.pyx:14:6: closures inside cpdef functions not …Run Code Online (Sandbox Code Playgroud) 我有一个表达式集矩阵,其行名是我认为格式为 GENCODE ID 的格式,例如“ENSG00000000003.14”“ENSG00000000457.13”“ENSG00000000005.5”等。我想将它们转换为gene_symbol,但我不确定最好的方法,特别是因为我认为是版本“.14”或“.13”。我应该先修剪点后面的所有 ID,然后使用 biomaRt 进行转换吗?如果是这样,最有效的方法是什么?有没有更好的方法来获取gene_symbol?
非常感谢你的帮助
当我运行以下代码时,我希望一旦foo()执行,它使用的内存(主要是 create m)就会被释放。然而,事实并非如此。要释放此内存,我需要重新启动 IPython 控制台。
%%cython
# distutils: language = c++
import numpy as np
from libcpp.map cimport map as cpp_map
cdef foo():
cdef:
cpp_map[int,int] m
int i
for i in range(50000000):
m[i] = i
foo()
Run Code Online (Sandbox Code Playgroud)
如果有人能告诉我为什么会出现这种情况以及如何在不重新启动 shell 的情况下释放此内存,那就太好了。提前致谢。
我需要计算一组给定范围内的唯一元素的数量.我的输入是这些范围的起点和终点坐标,我执行以下操作.
>>>coordinates
[[7960383, 7961255],
[15688414, 15689284],
[19247797, 19248148],
[21786109, 21813057],
[21822367, 21840682],
[21815951, 21822369],
[21776839, 21783355],
[21779693, 21786111],
[21813097, 21815959],
[21776839, 21786111],
[21813097, 21819613],
[21813097, 21822369]]
[21813097, 21822369]]
>>>len(set(chain(*[range(i[0],i[1]+1) for i in coordinates]))) #here chain is from itertools
Run Code Online (Sandbox Code Playgroud)
问题是它不够快.这需要在我的机器上花费3.5ms(使用%timeit)(购买新计算机不是一种选择),因为我需要在数百万套上执行此操作,所以速度并不快.
有什么建议可以证明这一点吗?
编辑:行数可以变化.在这种情况下,有12行.但我不能给它任何上限.
python ×5
cython ×2
r ×2
annotations ×1
biomart ×1
break ×1
containers ×1
dataframe ×1
if-statement ×1
lambda ×1
loops ×1
matplotlib ×1
memory ×1
memory-leaks ×1
numpy ×1
performance ×1
plot ×1
python-3.x ×1
range ×1
set ×1