-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcentro.py
65 lines (54 loc) · 1.85 KB
/
centro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import click as ck
import math
import os
import numpy as np
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import json
class Align(object):
def __init__(self, chr_id, chr_len, chr_start, chr_end,
ctg_id, ctg_len, ctg_start, ctg_end, is_reverse=False):
self.chr_id = chr_id
self.chr_len = chr_len
self.chr_start = chr_start
self.chr_end = chr_end
self.ctg_id = ctg_id
self.ctg_len = ctg_len
self.ctg_start = ctg_start
self.ctg_end = ctg_end
self.is_reverse = is_reverse
def toarray(self):
if self.is_reverse:
return [f'-{self.ctg_id}', self.ctg_start, self.ctg_end]
else:
return [self.ctg_id, self.ctg_start, self.ctg_end]
@ck.command()
@ck.option('--assembly', '-a', default='', help='Assembly contigs')
@ck.option('--config', '-c', default='', help='JSON file with configuration')
@ck.option('--output', '-o', default='', help='Output fasta file')
def main(assembly, config, output):
chroms = [f'chr{i}' for i in range(1, 23)]
chroms.append('chrX')
chroms.append('chrY')
chroms.append('chrM')
with open(config) as f:
json_text = f.read()
contigs = json.loads(json_text)
used = set()
for chrom, vals in contigs.items():
for contig_id, s, e in vals:
if contig_id.startswith('-'):
contig_id = contig_id[1:]
used.add(contig_id)
records = {}
with open(assembly) as f:
sequences = SeqIO.parse(f, 'fasta')
for record in sequences:
if record.id not in used:
records[record.id] = record
with open(output, 'w') as f:
for rec_id, rec in records.items():
SeqIO.write(rec, f, 'fasta')
if __name__ == '__main__':
main()