-
Notifications
You must be signed in to change notification settings - Fork 4
/
inc-seq.py
executable file
·160 lines (146 loc) · 7.36 KB
/
inc-seq.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
"""The INC-Seq pipeline
"""
import os
import sys
import argparse
import subprocess
from datetime import datetime
from Bio import SeqIO
from utils import findUnit, buildConsensus
def get_tmp(program):
program_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S.%f')
tmp_folder = "/dev/shm/" if os.path.exists("/dev/shm") else "/tmp/"
tmp_folder += program + program_time + "/"
return tmp_folder
def callBuildConsensus(aligner, record, aln, copy_num_thre, len_diff_thre, tmp_folder, seg_cov, iterative):
if aligner == "blastn":
consensus = buildConsensus.consensus_blastn(record, aln, copy_num_thre,
len_diff_thre, tmp_folder,
seg_cov, iterative)
elif aligner == "graphmap":
consensus = buildConsensus.consensus_graphmap(record, aln, copy_num_thre,
len_diff_thre, tmp_folder,
seg_cov, iterative)
elif aligner == "poa":
consensus = buildConsensus.consensus_poa(record, aln, copy_num_thre,
len_diff_thre, tmp_folder)
elif aligner == "marginAlign":
consensus = buildConsensus.consensus_marginAlign(record, aln, copy_num_thre,
len_diff_thre, tmp_folder,
seg_cov, iterative)
return consensus
def main(arguments):
#### parsing arguments
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-i', "--input",
required="True",
help="Input file in fasta format",
dest="inFasta")
parser.add_argument('-o', '--outfile',
help="Output file",
dest = "outFile",
default=sys.stdout, type=argparse.FileType('w'))
parser.add_argument("-a", "--aligner",
default='blastn',
dest="aligner",
help="The aligner used (blastn, graphmap, poa) [Default: blastn]")
parser.add_argument("-m", "--minReadLength",
default=2000,
dest="minRL",
type=int,
help="The reads shorter than this will be discarded [Default:2000]")
##find unit specific
parser.add_argument("--anchor_seg_step",
default=500,
dest="anchor_seg_step",
type=int,
help="Step of sliding window used as anchors [Default: 500] (eg. -s 500 : start at 0, 500, 1000, ...)")
parser.add_argument("--anchor_length",
default=500,
dest="anchor_len",
type=int,
help="The length of the anchor, should be smaller than the unit length [Default: 500]")
parser.add_argument("--anchor_cov",
default=0.8,
dest="anchor_cov",
type=float,
help="Anchor coverage required [Default: 0.8]")
parser.add_argument("--anchor_seq",
dest="anchor_seq",
type=str,
help="A single file containing the sequences used as the anchor [Default: Use subsequences as anchors]")
##consensus building specific
parser.add_argument("--iterative",
action = "store_true",
dest="iterative",
help="Iteratively run pbdagcon on consensus [Default: False]")
parser.add_argument("--segments_only",
action = "store_true",
dest="segments_only",
help="Extract segments only without constucting consensus [Default: False]")
parser.add_argument("--seg_cov",
default=0.8,
dest="seg_cov",
type=float,
help="Segment coverage required [Default: 0.8]")
parser.add_argument("--copy_num_thre",
dest="copy_num_thre",
default = 6,
type = int,
help="Minimal copy number required [Default: 6]")
parser.add_argument("--length_difference_threshold",
dest="len_diff_thre",
default = 0.05,
type = float,
help="Segment length deviation from the median to be considered as concordant [Default: 0.05]")
args = parser.parse_args(arguments)
#### parse input reads
seqs = SeqIO.parse(args.inFasta, "fasta")
#### create temp folder
tmp_folder = get_tmp('incseq_' + args.inFasta.split("/")[-1] + '_')
os.makedirs(tmp_folder)
counter = 0
if args.segments_only:
outH = open("inc_seq.segments.fa", "w")
for record in seqs:
seqlen = len(record.seq)
sys.stderr.write("---------- Processing read %i ----------\n" % (counter + 1))
counter += 1
if seqlen < args.minRL:
#### length filter
sys.stderr.write("Failed to pass length filter!\n")
else:
#### find units
if args.aligner == "blastn" or args.aligner == "graphmap" or args.aligner =="poa" or args.aligner == "marginAlign": ## FIXME graphmap implementation
if args.anchor_seq:
## anchor sequence provided, run with INC-Seq2 mode
aln = findUnit.find_unit_blastn(record, args.anchor_seq, tmp_folder, seqlen,
args.anchor_seg_step,
args.anchor_len,
args.anchor_cov)
else:
## use subsequences as anchors (INC-Seq mode)
aln = findUnit.find_unit_blastn(record, None, tmp_folder, seqlen,
args.anchor_seg_step,
args.anchor_len,
args.anchor_cov)
#### build consensus
if args.segments_only:
tmp = buildConsensus.segmentize(record, aln, args.copy_num_thre, args.len_diff_thre,
outH)
sys.stderr.write("Consensus construction skipped, check \"inc_seq.segments.fa\" for extracted segments!\n")
continue #skip consensus building
consensus = callBuildConsensus(args.aligner, record, aln, args.copy_num_thre,
args.len_diff_thre, tmp_folder,
args.seg_cov, args.iterative)
if consensus:
sys.stderr.write("Consensus called\t%s\tNumber of segments\t%d\n" %(record.id, consensus[1]))
args.outFile.write(consensus[0])
else:
sys.stderr.write("Consensus construction failed!\n")
if args.segments_only:
outH.close()
os.rmdir(tmp_folder)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))