Imported Upstream version 0.2
[pysam.git] / pysam / Pileup.py
1 '''Tools for working with files in the samtools pileup -c format.'''
2 import collections
3 import pysam
4
5 PileupSubstitution = collections.namedtuple( "PileupSubstitution",
6                                     " ".join( (\
7             "chromosome", 
8             "position", 
9             "reference_base", 
10             "consensus_base",
11             "consensus_quality",
12             "snp_quality",
13             "rms_mapping_quality",
14             "coverage",
15             "read_bases",
16             "base_qualities" ) ) )
17
18 PileupIndel = collections.namedtuple( "PileupIndel",
19                                       " ".join( (\
20             "chromosome", 
21             "position", 
22             "reference_base", 
23             "genotype",
24             "consensus_quality",
25             "snp_quality",
26             "rms_mapping_quality",
27             "coverage",
28             "first_allelle",
29             "second_allele",
30             "reads_first",
31             "reads_second",
32             "reads_diff" ) ) )
33
34 def iterate( infile ):
35     '''iterate over ``samtools pileup -c`` formatted file.
36
37     *infile* can be any iterator over a lines.
38
39     The function yields named tuples of the type :class:`pysam.Pileup.PileupSubstitution`
40     or :class:`pysam.Pileup.PileupIndel`.
41
42     .. note:: 
43        The parser converts to 0-based coordinates
44     '''
45     
46     conv_subst = (str,lambda x: int(x)-1,str,str,int,int,int,int,str,str)
47     conv_indel = (str,lambda x: int(x)-1,str,str,int,int,int,int,str,str,int,int,int)
48
49     for line in infile:
50         d = line[:-1].split()
51         if d[2] == "*":
52             try:
53                 yield PileupIndel( *[x(y) for x,y in zip(conv_indel,d) ] )
54             except TypeError:
55                 raise pysam.SamtoolsError( "parsing error in line: `%s`" % line)
56         else:
57             try:
58                 yield PileupSubstitution( *[x(y) for x,y in zip(conv_subst,d) ] )
59             except TypeError:
60                 raise pysam.SamtoolsError( "parsing error in line: `%s`" % line)