Skip to content

Instantly share code, notes, and snippets.

@pewerner
Created October 14, 2014 17:18
Show Gist options
  • Save pewerner/fab844f8bf7d0ba321e6 to your computer and use it in GitHub Desktop.
Save pewerner/fab844f8bf7d0ba321e6 to your computer and use it in GitHub Desktop.
Class for optimizing input files based on volume
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
namespace InputFileOptimizer
{
public class Optimizer
{
public string input_file_path;
public string output_directory;
public int st_volume_limit;
public int lt_volume_limit;
public int xlt_volume_limit;
public int xlt_subtract_volume;
public bool sort_by_column = true;
public int well_id_column;
public int volume_column;
public bool sss = true;
public void optimizeFile()
{
string[] hit_list = File.ReadAllLines(input_file_path);
//--Optimize the list if necessary
if (sort_by_column )
{
hit_list = sortTheLIst(hit_list, well_id_column);
}
List<string> STHitList = new List<string>();
STHitList.Add(hit_list[0]);
List<string> LTHitList = new List<string>();
LTHitList.Add(hit_list[0]);
List<string> XLTHitList = new List<string>();
XLTHitList.Add(hit_list[0]);
for (int i = 1; i < hit_list.Length; ++i)
{
string[] line = hit_list[i].Split(',');
float volume = float.Parse(line[volume_column]);
//--Add Lines to an array for the ST Hit List
if (volume <= st_volume_limit)
{
STHitList.Add(ConvertStringArrayToString(line));
}
//--Add Lines to an array for the LT Hit List
if (volume > st_volume_limit && volume <= lt_volume_limit)
{
LTHitList.Add(ConvertStringArrayToString(line));
}
//--Add Lines to an array for the XLT Hit List
if (volume > lt_volume_limit)
{
//--Split the Transfer into two if the volume exceeds the xlt Threshold
if (volume > xlt_volume_limit)
{
float smallerVolume = volume - 100;
line[4] = smallerVolume.ToString();
XLTHitList.Add(ConvertStringArrayToString(line));
string[] new_line = line;
new_line[4] = xlt_subtract_volume.ToString();
XLTHitList.Add(ConvertStringArrayToString(line));
}
else
{
XLTHitList.Add(ConvertStringArrayToString(line));
}
}
}
TextWriter twST = new StreamWriter(output_directory + "\\sthitlist.csv");
foreach (String s in STHitList)
{
twST.WriteLine(s);
}
twST.Close();
TextWriter twLT = new StreamWriter(output_directory + "\\lthitlist.csv");
foreach (String s in LTHitList)
{
twLT.WriteLine(s);
}
twLT.Close();
TextWriter twxLT = new StreamWriter(output_directory + "\\xlthitlist.csv");
foreach (String s in XLTHitList)
{
twxLT.WriteLine(s);
}
twxLT.Close();
}
static string ConvertStringArrayToString(string[] array)
{
string result = string.Join(",", array);
return result;
}
//--Reorder the Array Of lines so that transfers are optimized for a 96 well plate.
static string[] sortTheLIst(string[] itemsToReorder, int wellIdIDX) {
string[] arrayOfLetters ={"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"};
List<string> arrayOfWellIDs = new List<string>();
for (int i = 0; i < 24; ++i) {
for (int j = 0; j < arrayOfLetters.Count(); ++j) {
string letter = arrayOfLetters[j];
string wellID = letter + (i + 1);
arrayOfWellIDs.Add(wellID);
}
}
string[] reorderedArrayOfIDS = new string[384];
for (var i = 1; i < itemsToReorder.Count(); ++i)
{
//--Gets new Position
string foundWellID = itemsToReorder[i].Split(',')[wellIdIDX];
int pos = arrayOfWellIDs.IndexOf(foundWellID);
reorderedArrayOfIDS[pos] = itemsToReorder[i];
}
//-- Add Header back to file
//reorderedArrayOfIDS[0] = itemsToReorder[0];
//--Remove Empty indexs
reorderedArrayOfIDS = reorderedArrayOfIDS.Where(x => !string.IsNullOrEmpty(x)).ToArray();
for (var i = 1; i < itemsToReorder.Count(); ++i)
{
itemsToReorder[i] = reorderedArrayOfIDS[i-1];
}
return itemsToReorder;
}
}
}
import clr
clr.AddReferenceToFileAndPath('C:/Users/pwerner/Documents/Visual Studio 2010/Projects/InputFileOptimizer/InputFileOptimizer/bin/Debug/InputFileOptimizer.dll')
from InputFileOptimizer import Optimizer
op = Optimizer()
#Set the Input File Path
op.input_file_path = 'C:/Users/Encore User/Desktop/test.csv'
# Directory where new input files will be generated (sthitlist.csv, lthitlist.csv, xlthitlist.csv)
op.output_directory = 'C:/Users/Encore User/Desktop/Encore'
# Max volume for ST Tip Adapater inputfile
op.st_volume_limit = 10
# Max volume for LT Tip Adapter Input File
op.lt_volume_limit = 250
# Max volume for XLT Tip Adapter Input File. If the Transfer Volume Exceeds this number we will apply
# the xlt_subtract volume and do the transfer in two steps.
op.xlt_volume_limit= 900
# if applied this will create an additional transfer with this volume in order to transfer the requested volume in the input file
# without exceeding the xlt_volume_limit
op.xlt_subtract_volume = 100
# If set to True, this will sort all input files to optimize for column wise dispensing
op.sort_by_column = True
# Column in the input file that has the Source Well ID (Zero Based)
op.well_id_column = 1
# Column in the input file that has the Transfer Volume (Zero Based)
op.volume_column = 4
op.optimizeFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment