25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

127 satır
3.7 KiB

  1. from typing import (
  2. Callable,
  3. List,
  4. Optional,
  5. Tuple,
  6. Iterable,
  7. Union,
  8. )
  9. from pysam.libcutils import _pysam_dispatch
  10. class SamtoolsError(Exception):
  11. '''exception raised in case of an error incurred in the samtools
  12. library.'''
  13. def __init__(self, value):
  14. self.value = value
  15. def __str__(self):
  16. return repr(self.value)
  17. class PysamDispatcher(object):
  18. '''The dispatcher emulates the samtools/bctools command line.
  19. Captures stdout and stderr.
  20. Raises a :class:`pysam.SamtoolsError` exception in case samtools
  21. exits with an error code other than 0.
  22. Some command line options are associated with parsers. For
  23. example, the samtools command "pileup -c" creates a tab-separated
  24. table on standard output. In order to associate parsers with
  25. options, an optional list of parsers can be supplied. The list
  26. will be processed in order checking for the presence of each
  27. option.
  28. If no parser is given or no appropriate parser is found, the
  29. stdout output of samtools/bcftools commands will be returned.
  30. '''
  31. dispatch = None
  32. parsers = None
  33. collection = None
  34. def __init__(
  35. self,
  36. collection: str,
  37. dispatch: str,
  38. parsers: Optional[Iterable[Tuple[str, Callable[[Union[str, List[str]]], Union[str, List[str]]]]]] = None,
  39. ):
  40. self.collection = collection
  41. self.dispatch = dispatch
  42. self.parsers = parsers
  43. self.stderr = []
  44. def __call__(self, *args: str, **kwargs) -> Union[str, List[str]]:
  45. '''
  46. execute a samtools command.
  47. Keyword arguments:
  48. catch_stdout -- redirect stdout from the samtools command and
  49. return as variable (default True)
  50. save_stdout -- redirect stdout to a filename.
  51. raw -- ignore any parsers associated with this samtools command.
  52. split_lines -- return stdout (if catch_stdout is True and stderr
  53. as a list of strings.
  54. '''
  55. retval, stderr, stdout = _pysam_dispatch(
  56. self.collection,
  57. self.dispatch,
  58. args,
  59. catch_stdout=kwargs.get("catch_stdout", True),
  60. save_stdout=kwargs.get("save_stdout", None))
  61. if kwargs.get("split_lines", False):
  62. stdout = stdout.splitlines()
  63. if stderr:
  64. stderr = stderr.splitlines()
  65. if retval:
  66. raise SamtoolsError(
  67. "%s returned with error %i: "
  68. "stdout=%s, stderr=%s" %
  69. (self.collection,
  70. retval,
  71. stdout,
  72. stderr))
  73. self.stderr = stderr
  74. # call parser for stdout:
  75. if not kwargs.get("raw") and stdout and self.parsers:
  76. for options, parser in self.parsers:
  77. for option in options:
  78. if option not in args:
  79. break
  80. else:
  81. return parser(stdout)
  82. return stdout
  83. def get_messages(self):
  84. return self.stderr
  85. def usage(self):
  86. '''return the samtools usage information for this command'''
  87. retval, stderr, stdout = _pysam_dispatch(
  88. self.collection,
  89. self.dispatch,
  90. is_usage=True,
  91. catch_stdout=True)
  92. # some tools write usage to stderr, such as mpileup
  93. if stderr:
  94. return stderr
  95. else:
  96. return stdout
  97. class unquoted_str(str):
  98. '''Tag a value as an unquoted string. Meta-information in the VCF
  99. header takes the form of key=value pairs. By default, pysam will
  100. enclose the value in quotation marks. Tagging that value with
  101. unquoted_str will prevent this quoting.'''