-
Notifications
You must be signed in to change notification settings - Fork 2
/
KmerTable2KoverMatrix.py
executable file
·58 lines (53 loc) · 2.04 KB
/
KmerTable2KoverMatrix.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# KmerTable2KoverMatrix.py
#
# Copyright 2016 Huan Fan <hfan22@wisc.edu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
from AAF import smartopen, present
from optparse import OptionParser
import sys
Usage = "%prog [options] shared_kmer_table"
version = '%prog 20170118.1'
parser = OptionParser(Usage, version = version)
parser.add_option("-n", dest = "filter", type = int, default = 1,
help = "k-mer filtering threshold, default = 1")
(options, args) = parser.parse_args()
kmer_table = smartopen(sys.argv[1])
outfile = open(sys.argv[1].split('.')[0]+'_kmerMatrix.tsv','w')
n = options.filter
line1 = kmer_table.readline()
if line1.startswith('#') #with header
outfile.write('kmers')
for line in kmer_table:
if line.startswith('#sample'):
outfile.write('\t'+line.split(":")[1].strip()+'\n')
else:
outfile.write(line.split()[0]+'\t')
outfile.write('\t'.join([present(i,n) for i in line.split()[1:]])+'\n')
else:
outfile.write(line1.split()[0]+'\t')
outfile.write('\t'.join([present(i,n) for i in line1.split()[1:]])+'\n')
for line in kmer_table:
outfile.write(line.split()[0]+'\t')
outfile.write('\t'.join([present(i,n) for i in line.split()[1:]])+'\n')
#Remove the final new line
#outfile.truncate(outfile.tell()-1)
kmer_table.close()
outfile.close()