Debian Codegraph @

doc/api.rst

raw
1 ============
2 Introduction
3 ============
4
5 Pysam is a python module that makes it easy to read and manipulate
6 mapped short read sequence data stored in SAM/BAM files. It is a
7 lightweight wrapper of the htslib_ C-API.
8
9 This page provides a quick introduction in using pysam followed by the
10 API. See :ref:`usage` for more detailed usage instructions.
11
12 To use the module to read a file in BAM format, create a
13 :class:`~pysam.AlignmentFile` object::
14
15 import pysam
16 samfile = pysam.AlignmentFile("ex1.bam", "rb")
17
18 Once a file is opened you can iterate over all of the read mapping to
19 a specified region using :meth:`~pysam.AlignmentFile.fetch`. Each
20 iteration returns a :class:`~pysam.AlignedSegment` object which
21 represents a single read along with its fields and optional tags::
22
23 for read in samfile.fetch('chr1', 100, 120):
24 print(read)
25
26 samfile.close()
27
28 To give::
29
30 EAS56_57:6:190:289:82 0 99 <<<7<<<;<<<<<<<<8;;<7;4<;<;;;;;94<; 69 CTCAAGGTTGTTGCAAGGGGGTCTATGTGAACAAA 0 192 1
31 EAS56_57:6:190:289:82 0 99 <<<<<<;<<<<<<<<<<;<<;<<<<;8<6;9;;2; 137 AGGGGTGCAGAGCCGAGTCACGGGGTTGCCAGCAC 73 64 1
32 EAS51_64:3:190:727:308 0 102 <<<<<<<<<<<<<<<<<<<<<<<<<<<::<<<844 99 GGTGCAGAGCCGAGTCACGGGGTTGCCAGCACAGG 99 18 1
33 ...
34
35 You can also write to a :class:`~pysam.AlignmentFile`::
36
37 import pysam
38 samfile = pysam.AlignmentFile("ex1.bam", "rb")
39 pairedreads = pysam.AlignmentFile("allpaired.bam", "wb", template=samfile)
40 for read in samfile.fetch():
41 if read.is_paired:
42 pairedreads.write(read)
43
44 pairedreads.close()
45 samfile.close()
46
47 An alternative way of accessing the data in a SAM file is by iterating
48 over each base of a specified region using the
49 :meth:`~pysam.AlignmentFile.pileup` method. Each iteration returns a
50 :class:`~pysam.PileupColumn` which represents all the reads in the SAM
51 file that map to a single base in the reference sequence. The list of
52 reads are represented as :class:`~pysam.PileupRead` objects in the
53 :attr:`PileupColumn.pileups <pysam.PileupColumn.pileups>` property::
54
55 import pysam
56 samfile = pysam.AlignmentFile("ex1.bam", "rb" )
57 for pileupcolumn in samfile.pileup("chr1", 100, 120):
58 print("\ncoverage at base %s = %s" % (pileupcolumn.pos, pileupcolumn.n))
59 for pileupread in pileupcolumn.pileups:
60 if not pileupread.is_del and not pileupread.is_refskip:
61 # query position is None if is_del or is_refskip is set.
62 print('\tbase in read %s = %s' %
63 (pileupread.alignment.query_name,
64 pileupread.alignment.query_sequence[pileupread.query_position]))
65
66 samfile.close()
67
68 The above code outputs::
69
70 coverage at base 99 = 1
71 base in read EAS56_57:6:190:289:82 = A
72
73 coverage at base 100 = 1
74 base in read EAS56_57:6:190:289:82 = G
75
76 coverage at base 101 = 1
77 base in read EAS56_57:6:190:289:82 = G
78
79 coverage at base 102 = 2
80 base in read EAS56_57:6:190:289:82 = G
81 base in read EAS51_64:3:190:727:308 = G
82 ...
83
84 Commands available in `samtools`_ are available as simple
85 function calls. For example::
86
87 pysam.sort("-o", "output.bam", "ex1.bam")
88
89 corresponds to the command line::
90
91 samtools sort -o output.bam ex1.bam
92
93 Analogous to :class:`~pysam.AlignmentFile`, a
94 :class:`~pysam.TabixFile` allows fast random access to compressed and
95 tabix indexed tab-separated file formats with genomic data::
96
97 import pysam
98 tabixfile = pysam.TabixFile("example.gtf.gz")
99
100 for gtf in tabixfile.fetch("chr1", 1000, 2000):
101 print(gtf.contig, gtf.start, gtf.end, gtf.gene_id)
102
103 :class:`~pysam.TabixFile` implements lazy parsing in order to iterate
104 over large tables efficiently.
105
106 More detailed usage instructions is at :ref:`usage`.
107
108 .. note::
109
110 Coordinates in pysam are always 0-based (following the python
111 convention). SAM text files use 1-based coordinates.
112
113 .. note::
114
115 The above examples can be run in the :file:`tests` directory of the
116 installation directory. Type 'make' before running them.
117
118 .. seealso::
119
120 https://github.com/pysam-developers/pysam
121
122 The pysam code repository, containing source code and download
123 instructions
124
125 http://pysam.readthedocs.org/en/latest/
126
127 The pysam website containing documentation
128
129 ===
130 API
131 ===
132
133 SAM/BAM/CRAM files
134 ==================
135
136 Objects of type :class:`~pysam.AlignmentFile` allow working with
137 BAM/SAM formatted files.
138
139 .. autoclass:: pysam.AlignmentFile
140 :members:
141
142 .. autoclass:: pysam.AlignmentHeader
143 :members:
144
145 An :class:`~pysam.AlignedSegment` represents an aligned segment within
146 a SAM/BAM file.
147
148 .. autoclass:: pysam.AlignedSegment
149 :members:
150
151 .. autoclass:: pysam.PileupColumn
152 :members:
153
154 .. autoclass:: pysam.PileupRead
155 :members:
156
157 .. autoclass:: pysam.IndexedReads
158 :members:
159
160
161 Tabix files
162 ===========
163
164 :class:`~pysam.TabixFile` opens tabular files that have been
165 indexed with tabix_.
166
167 .. autoclass:: pysam.TabixFile
168 :members:
169
170 To iterate over tabix files, use :func:`~pysam.tabix_iterator`:
171
172 .. autofunction:: pysam.tabix_iterator
173
174 .. autofunction:: pysam.tabix_compress
175
176 .. autofunction:: pysam.tabix_index
177
178 .. autoclass:: pysam.asTuple
179 :members:
180
181 .. autoclass:: pysam.asVCF
182 :members:
183
184 .. autoclass:: pysam.asBed
185 :members:
186
187 .. autoclass:: pysam.asGTF
188 :members:
189
190
191 FASTA files
192 ===========
193
194 .. autoclass:: pysam.FastaFile
195 :members:
196
197 FASTQ files
198 ===========
199
200 .. autoclass:: pysam.FastxFile
201 :members:
202
203
204 .. autoclass:: pysam.FastqProxy
205 :members:
206
207
208 VCF/BCF files
209 =============
210
211 .. autoclass:: pysam.VariantFile
212 :members:
213
214 .. autoclass:: pysam.VariantHeader
215 :members:
216
217 .. autoclass:: pysam.VariantRecord
218 :members:
219
220 .. autoclass:: pysam.VariantHeaderRecord
221 :members:
222
223 HTSFile
224 =======
225
226 HTSFile is the base class for :class:`pysam.AlignmentFile` and
227 :class:`pysam.VariantFile`.
228
229 .. autoclass:: pysam.HTSFile
230 :members: