Skip to content

Commit

Permalink
add script to convert pwm to bamm
Browse files Browse the repository at this point in the history
  • Loading branch information
wge11 committed Nov 29, 2017
1 parent 470a10c commit f51ad93
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,4 @@ cmake-build-debug/
# python
##############
*/*/__pycache__/
*/__pycache__/
38 changes: 38 additions & 0 deletions py/pwm2bamm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
This is a script to convert PWMs from a MEME-formated file to BaMM-formated files.
The only input is a MEME-formated file (version 4)
Written by Wanwan Ge
'''

import argparse
import os

from utils import parse_meme, write_bamm

def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument('meme_file')

return parser

def main():
parser = create_parser()
args = parser.parse_args()

ipath = args.meme_file
dir = os.path.dirname(ipath)
basename = os.path.splitext(os.path.basename(ipath))[0]
motifset = parse_meme(ipath)
models = motifset['models']

for num in range(len(models)):
filepath_v = dir + '/' + basename + "_motif_" + str(num+1) + ".ihbcp"
filepath_p = dir + '/' + basename + "_motif_" + str(num+1) + ".ihbp"
write_bamm(models[num]['pwm'], filepath_v )
write_bamm(models[num]['pwm'], filepath_p )


if __name__ == '__main__':
main()


7 changes: 7 additions & 0 deletions py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,5 +279,12 @@ def write_meme(dataset, meme_output_file):
print(" ".join(['{:.4f}'.format(x) for x in line]), file=fh)
print(file=fh)

def write_bamm(pwm, ofile):
eps = 1e-16
with open(ofile, "w") as fh:
for i in range(len(pwm)):
print(' '.join(['{:.4e}'.format(x+eps) for x in pwm[i]]) + ' \n', file=fh)


class MalformattedMemeError(ValueError):
pass

0 comments on commit f51ad93

Please sign in to comment.