Add cwl and docker files
[lightning.git] / cwl / preprocess / gvcf / src / cleanvcf.py
1 # Copyright (C) The Lightning Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 #!/usr/bin/env python
6
7 from __future__ import print_function
8 import sys
9
10 def is_header(line):
11     """Check if a line is header."""
12
13     return line.startswith('#')
14
15 # FIELD index
16 # CHROM 0, POS 1, REF 3
17
18 def main():
19     previous_CHROM = ""
20     previous_end_POS = 0
21
22     for line in sys.stdin:
23         if not is_header(line):
24             fields = line.split('\t')
25             CHROM = fields[0]
26             POS = int(fields[1])
27             REF = fields[3]
28             if CHROM == previous_CHROM:
29                 if POS > previous_end_POS:
30                     print(line, end='')
31                     previous_end_POS = max(previous_end_POS, POS + len(REF) - 1)
32             else:
33                 print(line, end='')
34                 previous_end_POS = POS + len(REF) - 1
35             previous_CHROM = CHROM
36         else:
37             print(line, end='')
38
39 if __name__ == '__main__':
40     main()