Created
February 16, 2018 14:01
-
-
Save dinovski/2323847086d9fcaf980fd313f49bd8c5 to your computer and use it in GitHub Desktop.
convert 96 well name to numeric position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv | |
import string | |
import datetime | |
import random | |
import json | |
import re | |
import sys | |
import os | |
def well96_to_number(well_name): | |
""" | |
Given a Well-Name of a 96-well plate (e.g. "A12" or "F08") | |
returns the well number: | |
A01 => 1 | |
A1 => 1 | |
A12 => 89 | |
B01 => 2 | |
B1 => 2 | |
B12 => 90 | |
""" | |
if len(well_name)<2 or len(well_name)>3: | |
raise ValueError("invalid well name '%s' (expecting 2 or 3 charracters)" % ( well_name ) ) | |
row_text = well_name[:1].upper(); | |
if row_text < 'A' or row_text > 'H': | |
raise ValueError("invalid row '%s' in well name '%s' (expecting letter 'A' to 'H')" % ( row_text, well_name ) ) | |
row = ord(row_text) - ord("A") + 1 ; | |
col_text = well_name[1:] | |
try: | |
col = int(col_text) | |
except ValueError: | |
raise ValueError("invalid column '%s' in well name '%s' (expecting a numberic value)" % ( col_text, well_name ) ) | |
if col < 1 or col > 12: | |
raise ValueError("invalid column '%s' in well name '%s' (expecting a number between 1 and 12)" % ( col_text, well_name ) ) | |
well_num = (col-1)*8 + row | |
return well_num | |
def convert_number(): | |
if len(sys.argv)<2: | |
sys.stderr.write("Usage: %s [WELL-NAME]\n" % ( sys.argv[0] )) | |
sys.exit(1) | |
well_num = well96_to_number(sys.argv[1]) | |
print well_num | |
def csv96_wellname_to_number(filename): | |
""" | |
Given a filename of a CSV containing well-names in the second and fourth columns, | |
convert the well-names (e.g. "A11") to well-number (e.g. "81"). | |
The CSV is expected to have 6 fields: | |
source plate, | |
source well name (e.g. "A11") | |
dest plate, | |
dest well name (e.g. "B09") | |
[other optional columns] | |
""" | |
line_number = 0 | |
try: | |
for line_number,row in enumerate(csv.reader(open(filename))): | |
if line_number==0: # skip header line | |
print ",".join(row) | |
continue | |
if len(row) < 4: | |
raise Exception("expecting at least 4 fields") | |
row = [ x.strip() for x in row ] | |
row[1] = str( well96_to_number( row[1] ) ) | |
row[3] = str ( well96_to_number( row[3] ) ) | |
print ",".join(row) | |
except IOError as e: | |
raise Exception("Error loading CSV file (%s): %s" % ( filename , str(e))) | |
except ValueError as e: | |
raise Exception("Invalid input in CSV file '%s' line %d: %s" % ( filename, line_number+1, str(e) ) ) | |
def convert_csvfile(): | |
if len(sys.argv)<2: | |
sys.stderr.write("Usage: %s [CSV-FILE]\n" % ( sys.argv[0] )) | |
sys.exit(1) | |
csv96_wellname_to_number(sys.argv[1]) | |
if __name__ == "__main__": | |
convert_csvfile() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment