-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcm_to_pfm.py
85 lines (67 loc) · 2.45 KB
/
pcm_to_pfm.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
'''
Copyright © 2018 Anton Tsukanov. Contacts: [email protected]
License: http://www.gnu.org/licenses/gpl.txt
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 3 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.
'''
import math
import sys
import random
import itertools
import argparse
def read_matrix(path):
with open(path, 'r') as file:
header = file.readline()
header = header.strip()
header = header.strip('>')
matrix = {'A': [], 'C': [], 'G': [], 'T': []}
for line in file:
line = line.strip().split()
for letter, value in zip(matrix.keys(), line):
matrix[letter].append(float(value))
file.close()
return(matrix, header)
def make_pfm_from_pcm(pcm):
number_of_sites = [0] * len(pcm['A'])
for key in pcm.keys():
for i in range(len(pcm[key])):
number_of_sites[i] += pcm[key][i]
pfm = dict()
mono_nucleotides = itertools.product('ACGT', repeat=1)
for i in mono_nucleotides:
pfm[i[0]] = []
first_key = list(pcm.keys())[0]
nuc_pseudo = 1/len(pcm.keys())
for i in range(len(pcm[first_key])):
for nuc in pcm.keys():
pfm[nuc].append((pcm[nuc][i] + nuc_pseudo) / (number_of_sites[i] + 1))
return(pfm)
def write_pfm(output, header, pfm):
with open(output, 'w') as file:
file.write('>{0}\n'.format(header))
for i in zip(pfm['A'], pfm['C'], pfm['G'], pfm['T']):
file.write('{0}\t{1}\t{2}\t{3}\n'.format(i[0], i[1], i[2], i[3]))
pass
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('input', action='store',
help='path to PCM file (HOMER/HOCOMOCO like format)')
parser.add_argument('output', action='store',
help='path to write PFM')
return(parser.parse_args())
def main():
args = parse_args()
matrix_path = args.input
output_path = args.output
pcm, header = read_matrix(matrix_path)
pfm = make_pfm_from_pcm(pcm)
write_pfm(output_path, header, pfm)
pass
if __name__ == '__main__':
main()