def calculate_memory_for_sort():
"""Calculate available memory for ``samtools sort`` function.
If there is enough memory, no temp files are created. **Enough**
is defined as at least 1G per CPU.
Returns
-------
sort_memory: str or None
String to use directly with *-m* option in sort, or None.
"""
avail_memory = None
if HAVE_PSUTIL == True:
mem = virtual_memory()
avail_memory = mem.total
else:
try:
avail_memory = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') # e.g. 4015976448
except ValueError:
logging.error("If you're in Mac OS you need to have the psutil Python library.")
raise SystemExit
avail_cpu = multiprocessing.cpu_count()
sort_memory = avail_memory / avail_cpu / 1024 ** 2
# samtools default from documentation is 768M, to be conservative
# only use -m option when there is more than 1G per CPU.
if sort_memory < 1024:
sort_memory = None
else:
sort_memory = "%sG" % (int(floor(sort_memory / 1024)))
return sort_memory
# --------------------------------------------------------------------------------------------------
评论列表
文章目录