new file: makefile
[GalaxyCodeBases.git] / python / vcf / str1.py
blob6675e222ca0ce2bcadb3d928ea0ccacf6390ad5a
1 #!/usr/bin/env python3
3 import sys
4 import os
5 from pathlib import Path
6 from pysam import VariantFile
8 import pprint
10 def readVcfDat(fname):
11 vcf_in = VariantFile(fname)
12 for rec in vcf_in.fetch():
13 pprint.pprint(rec)
14 print ([rec.chrom,rec.pos,rec.id,rec.alleles])
15 if rec.id.startswith('rs'):
16 # SNP
17 else:
18 # STR
19 for key,value in rec.samples.iteritems():
20 print((key, value['GT']))
23 def main():
24 try:
25 p = Path('unphased_all_vcf')
26 for f in p.iterdir():
27 if f.is_file():
28 if '.vcf' in f.suffixes:
29 print((f,f.suffixes))
30 readVcfDat(f)
32 print ('[!]Done', file=sys.stderr)
33 sys.stdout.flush()
34 except BrokenPipeError: # https://stackoverflow.com/a/58517082
35 # https://docs.python.org/3/library/signal.html#note-on-sigpipe
36 # Python flushes standard streams on exit; redirect remaining output
37 # to devnull to avoid another BrokenPipeError at shutdown
38 devnull = os.open(os.devnull, os.O_WRONLY)
39 os.dup2(devnull, sys.stdout.fileno())
40 sys.exit(1) # Python exits with error code 1 on EPIPE
42 if __name__ == '__main__':
43 main()