From 988a1c2f0c66d2264995c16703f98740b760a9a2 Mon Sep 17 00:00:00 2001 From: Adrian Nievergelt Date: Thu, 5 Dec 2019 18:14:16 +0100 Subject: [PATCH] added MOTL multiplyer scripts for general use --- subtomogramAveraging/csv_2Xshifts_apn.py | 45 +++++++++++++++++ subtomogramAveraging/multiplyMOTL.py | 62 ++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100755 subtomogramAveraging/csv_2Xshifts_apn.py create mode 100755 subtomogramAveraging/multiplyMOTL.py diff --git a/subtomogramAveraging/csv_2Xshifts_apn.py b/subtomogramAveraging/csv_2Xshifts_apn.py new file mode 100755 index 0000000..17e7ac8 --- /dev/null +++ b/subtomogramAveraging/csv_2Xshifts_apn.py @@ -0,0 +1,45 @@ +''' +31-07-2017 + +Pigino Lab, MPI-CBG + +Edits the csv file, doubling the x,y,z values, +deletes last semicolon from each line, and then replaces +all the semicolons with comma +''' + +import fileinput +import sys +import codecs +import os + +with fileinput.input(inplace=True) as f: + with fileinput.input(inplace=True) as f1: + + + #Skips the first line in the csv file + next(f) + + print("CCC,reserved,reserved,pIndex,wedgeWT,NA,NA,NA,NA,NA,xOffset,yOffset,zOffset,NA,NA,reserved,EulerZ(1),EulerZ(3),EulerX(2),reserved,CREATED WITH PEET Version 1.9.1 04/01/2014") + for line in f: + + #removes ; from the end of all lines where it appears + #leaves all other lines unchanged + line = line.rstrip('\n'); + #if line.endswith(';'): + #line = line[:-1] + + #array that stores the index value of all the semicolons in the line + sc_pos_array = [i for i,x in enumerate(line) if x == ','] + + #Multiplies and replaces x,y,z values by 2* the value + line = line[0:sc_pos_array[9]+1] + str(2 * float(line[sc_pos_array[9]+1 : sc_pos_array[10]])) + "," + str(2 * float(line[sc_pos_array[10]+1 : sc_pos_array[11]])) + "," + str(2 * float(line[sc_pos_array[11]+1 : sc_pos_array[12]])) + line[sc_pos_array[12]:] + + #finally, replace all the semicolons with comma + #line = line.replace(';',',') + + + print(line) + + + diff --git a/subtomogramAveraging/multiplyMOTL.py b/subtomogramAveraging/multiplyMOTL.py new file mode 100755 index 0000000..0c8b77f --- /dev/null +++ b/subtomogramAveraging/multiplyMOTL.py @@ -0,0 +1,62 @@ +#!/usr/bin/python + +# APN - 2019-05-06 + +import os,sys +import argparse +import matplotlib.pyplot as plt +import csv + +parser = argparse.ArgumentParser(description = "Takes a PEET motif list (MOTL) file and duplicates the motif descriptor lines a specified number of times.") + +# parser.add_argument("-f", "--file", help="create overlay for a single file", +parser.add_argument('-i', '--input', nargs='?', default='.', help='Input file to process') +parser.add_argument('-o', '--output', nargs='?', default='.', help='Output file to write to. If no argument is provided, a _mult suffix will be added to the original file name') +parser.add_argument('-n', '--number', nargs='?', type=int, default='4', help='How many times to duplicate data lines') +parser.add_argument('-H', '--headerLines', nargs='?', type=int, default='1', help='Number of header lines in file') +parser.add_argument('-it', '--ignoreTail', nargs='?', type=int, default=0, help='Ignores the given number of columns at the end of the file when parsing') +parser.add_argument('-g', '--graph', action='store_true', help='Store a graph of the corrected and uncorrected line intensities for debug purposes.') + + +args = parser.parse_args() + + +def row_count(filename): + with open(filename) as in_file: + return sum(1 for _ in in_file) + +if args.input == '.': + print("Missing input file"); + print(parser.print_help()) + sys.exit() + +if args.output == '.': + inPath = os.path.splitext(args.input) + args.output = inPath[0] + "_mult" + inPath[1] + +if args.number < 1: + print("Data line duplication argument must be at least 1.") + sys.exit() + +args.ignoreTail = abs(args.ignoreTail) + + +with open(args.input, 'r') as csvInFile, open(args.output, 'w') as csvOutFile: + csvReader = csv.reader(csvInFile, delimiter=',', quotechar='#') + csvWriter = csv.writer(csvOutFile, delimiter=',', quotechar='#') + + lastLineNumber = row_count(args.input) + + for row in csvReader: + + if csvReader.line_num <= args.headerLines or csvReader.line_num == lastLineNumber: + # PEET column descriptor line or last line, don't duplicate + if csvReader.line_num <= args.headerLines: + csvWriter.writerow(row) + else: + csvWriter.writerow(row[:len(row)-args.ignoreTail]) + else: + # Data line, write as many times as requested + for i in range(args.number): + csvWriter.writerow(row[:len(row)-args.ignoreTail]) + -- GitLab