Commit f0891c5f authored by Adrian Nievergelt's avatar Adrian Nievergelt

initial version of multi-file capable MOTL list rescaler for unbinning

parent 8e0e0910
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Created on Wed May 15 09:22:40 2019
@author: adrian
import configparser
import os, sys
import re
from pprint import pprint
import argparse
import mrcfile
import csv
parser = argparse.ArgumentParser(description = "Rescales shifts in motif lists (MOTLs) generated in PEET with either with a specified scale factor or between two supplied mrc files.")
# parser.add_argument("-f", "--file", help="create overlay for a single file",
parser.add_argument('-o', '--oldMrc', nargs='?', default='.', help='Old tomogram mrc that the MOTLs have been calculated with.')
parser.add_argument('-n', '--newMrc', nargs='?', default='.', help='New tomogram mrc file that the MOTLs should be scaled to.')
parser.add_argument('-sf', '--scaleFactor', nargs='?', type=float, help='Manual scale factor, default 2 if nothing specified.')
parser.add_argument('-s', '--suffix', nargs='?', type=str, default='rescaled', help='Suffix to append to rescaled MOTLs. Default is "_rescaled".')
parser.add_argument('-f', '--force', action='store_true', help='Force overwriting output files.')
parser.add_argument('-hl', '--numHeaderLines', nargs='?', type=int, default=1, help='Number of header lines in MOTL, default 1.')
parser.add_argument('file', type=str, nargs='+')
args = parser.parse_args()
if args.scaleFactor == None:
if not args.oldMrc == '.' and not args.newMrc == '.':
mrc1 = mrcfile.mmap('/home/nieverge/Adrian_PEET/an9_g5_t1_mc2_ipp_icon_flip_b4.rec', mode='r+')
mrc2 = mrcfile.mmap('/home/nieverge/Adrian_PEET/an9_g5_t1_mc2_ipp_icon_flip_b2.rec', mode='r+')
# print(mrc1.header)
# scale factor is the ratio at which the pixel/nm get scaled between the first and the second mrc.
scaleFactor = mrc1.header.cella.x / / (mrc2.header.cella.x /
print("Scaling shifts by a factor {:.2g}.".format(scaleFactor))
scaleFactor = 2
print("No input and output mrcs specified. Using scale factor %g." % (scaleFactor));
for file in args.file:
basePath = os.path.splitext(file)[0]
outMotlPath = "{}_{}.csv".format(basePath, args.suffix)
if os.path.exists(outMotlPath) and not args.force:
print("Output file {} exists, skipping. Use -f to overwrite.".format(file))
outMotl = open(outMotlPath, 'w')
writer = csv.writer(outMotl, delimiter=',', quoting=csv.QUOTE_NONE)
with open(file, 'r') as motlFile:
# rescale all the shifts according to rescaling ratio
reader = csv.reader(motlFile, delimiter=',', quoting=csv.QUOTE_NONE)
for row in reader:
if reader.line_num <= args.numHeaderLines:
for rescaleCol in range(10,13):
row[rescaleCol]= scaleFactor*float(row[rescaleCol])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment