How to make exception to a missing attribute in <class, list>?

174 Views Asked by At

After reading my data, I use to following script:

for sample in record.samples:
    print(type(sample))

I get:

<class 'vcf.model._Call'>

With this:

    print(sample)

I get:

Call(sample=MA605, CallData(GT=0/0, AD=[13, 0], DP=13, GQ=0, PB=None, PC=None, PG=0/0, PI=None, PL=[0, 0, 228], PW=0/0))
Call(sample=MA611, CallData(GT=0/0, AD=[57, 0], DP=57, GQ=0, PB=None, PC=None, PG=0/0, PI=None, PL=[0, 0, 1369], PW=0/0))
Call(sample=MA622, CallData(GT=0/1, AD=[67, 14], DP=81, GQ=99, PB=None, PC=None, PG=0/1, PI=None, PL=[229, 0, 2535], PW=0/1))
Call(sample=MA625, CallData(GT=0/0, AD=[58, 0], DP=58, GQ=0, PB=None, PC=None, PG=0/0, PI=None, PL=[0, 0, 590], PW=0/0))
Call(sample=MA629, CallData(GT=1|0, AD=[24, 5], DP=29, GQ=67, PB=.,., PC=1.0, PG=1|0, PI=5060, PL=[67, 0, 952], PW=1|0))
Call(sample=Ncm8, CallData(GT=0/0, AD=[7, 0], DP=7, GQ=0, PB=None, PC=None, PG=0/0, PI=None, PL=[0, 0, 147

I am trying to write the output to a file using:

output.write("{}\t{}\t{}\t{}\t{}"
             .format(contig1, pos1, ref_allele1, all_alleles1, all_freq1))
    for sample in record.samples:
        output.write("\t{}\t{}".format(sample['GT'], sample['PI']))

Everything is working well, but I have some line that have PI field missing - which means PI is totally missing and is not (PI = None). When my script hists that line I get AttributeError:

Traceback (most recent call last): File "/home/everestial007/PycharmProjects/stitcher/pHASE-Stitcher-Markov/pHASE-Stitcher_stage01-4_nonInteractive-MarkovModel.py", line 221, in <module> phase_block_index = record.genotype('2ms02g')['PI'] File "/home/everestial007/anaconda3/lib/python3.5/site-packages/vcf/model.py", line 104, in __getitem__ return getattr(self.data, key) AttributeError: 'CallData' object has no attribute 'PI'

How, can I set the PI value to period (.) when it is not in my data (record.sample)?

I tried several method. One of method I though would work is:

output.write("\t{}\t{}".format(sample['GT'] or None, sample['PI'] or None))

But, still failing.

Any, suggestions.

Thanks,

1

There are 1 best solutions below

0
On

You can try sample.get('PI') or '.'.

Get returns by default None, when a key is not defined.