Example #1
0
        private void HarmonyToDB(string DefaultPath)
        {
            #region Get the directory
            if (!Directory.Exists(DefaultPath)) DefaultPath = "";

            var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
            dlg1.Description = "Select the folder of your experiment.";
            dlg1.ShowNewFolderButton = true;
            dlg1.ShowEditBox = true;
            if (DefaultPath != "")
                dlg1.SelectedPath = DefaultPath;
            //dlg1.NewStyle = false;
            //dlg1.SelectedPath = txtExtractDirectory.Text;
            dlg1.ShowFullPathInEditBox = true;
            dlg1.RootFolder = System.Environment.SpecialFolder.Desktop;

            // Show the FolderBrowserDialog.
            DialogResult result = dlg1.ShowDialog();
            if (result != DialogResult.OK) return;

            string Path = dlg1.SelectedPath;

            if (Directory.Exists(Path) == false) return;
            #endregion

            #region Get the different files

            string[] ListFilesForPlates = null;
            try
            {
                ListFilesForPlates = Directory.GetFiles(Path, "Objects_Population - *.txt", SearchOption.AllDirectories);
            }
            catch (System.Exception excep)
            {
                MessageBox.Show(excep.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            if (ListFilesForPlates.Length == 0)
            {
                MessageBox.Show("The selected directory do not contain any Objects_Population files !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            string[] Sep = new string[1];
            Sep[0] = "\\";

            Dictionary<string, string> CurrentPlateDico = new Dictionary<string, string>();
          //  string[] FirstListImages = Directory.GetFiles(PlateDirectories[i], TmpPlateName + "_*.C01", SearchOption.AllDirectories);

            foreach (var item in ListFilesForPlates)
            {
                string[] Res = item.Split(Sep, StringSplitOptions.RemoveEmptyEntries);
                string CurrentName = Res[Res.Length-1];

                if (CurrentPlateDico.ContainsKey(CurrentName.Remove(CurrentName.Length - 4))) continue;

                CurrentPlateDico.Add(CurrentName.Remove(CurrentName.Length-4), item);

            }

            string[] ListTypes = CurrentPlateDico.Keys.ToArray();

            // plates selection GUI
            FormForPlateSelection FFP = new FormForPlateSelection(ListTypes,false);
            FFP.Text = "Object Types Selection";

            if (FFP.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            ListFilesForPlates = FFP.GetListPlatesSelected();

            #endregion

            if (ListFilesForPlates.Length == 0) return;

            ListFilesForPlates = Directory.GetFiles(Path, ListFilesForPlates[0] + ".txt", SearchOption.AllDirectories);

            #region And now let's analyse the files adn create the CSV

            CsvRow CurrentRow = new CsvRow();

            bool IsHeaderWritten = false;
            string OriginalHeader = "";

            Sep[0] = "\\";
            string[] TmpSplit = ListFilesForPlates[0].Split(Sep, StringSplitOptions.RemoveEmptyEntries);

           //TmpSplit[TmpSplit.Length-1].Replace(".txt",".csv");
            string TPath = Path + "\\" + TmpSplit[TmpSplit.Length - 1].Replace(".txt", ".csv");
                
            Sep[0] = ",";  // specifically for the bounding box processing

            FormForProgress MyProgressBar = new FormForProgress();

            MyProgressBar.progressBar.Maximum = ListFilesForPlates.Length;
            MyProgressBar.Show();


            for (int i = 0; i < ListFilesForPlates.Length ; i++)
            {
                MyProgressBar.richTextBoxForComment.AppendText(ListFilesForPlates[i]);
                MyProgressBar.richTextBoxForComment.Update();

                StreamWriter stream = new StreamWriter(TPath, true, Encoding.ASCII);
                
                #region process the header
                string CurrentFile = ListFilesForPlates[i];
                CsvFileReader CSVReader = new CsvFileReader(CurrentFile);
                CSVReader.Separator = '\t';
                
                CSVReader.ReadRow(CurrentRow);
                // let's take care of the header first
                while (CurrentRow[0]!="Plate Name")
                {
                    CSVReader.ReadRow(CurrentRow);
                }

                string PlateName = CurrentRow[1];

                // skip the rest of the header
                while (CurrentRow[0] != "[Data]")
                {
                    CSVReader.ReadRow(CurrentRow);
                }

                // read the columns names
                CSVReader.ReadRow(CurrentRow);
                List<string> Descs = CurrentRow;

                string TobeWritten = "Plate Name,";
                int IdxBoundingBox = -1;
                int IndexCol = -1;
                int IndexRow = -1;
                int IndexCompound = -1;
                int IndexConcentration = -1;
                int IndexCellcount = -1;
                
                int NumDesc = Descs.Count;

                for (int j = 0; j < Descs.Count; j++)
			    {
                    if (Descs[j] == "Bounding Box")
                    {
                        TobeWritten += "X_Min,Y_Min,X_Max,Y_Max,";
                        IdxBoundingBox = j;
                       // NumDesc += 3; 
                    }
                    else if (Descs[j] == "Row")
                    {
                        IndexRow = j;
                        TobeWritten += "Well Position,";
                    }
                    else if (Descs[j] == "Column")
                    {
                        IndexCol = j;
                    }
                    else if (Descs[j] == "Compound")
                    {
                        // skipped
                        IndexCompound = j;
                    }
                    else if (Descs[j] == "Concentration")
                    {
                        // skipped
                        IndexConcentration = j;
                    }
                    else if (Descs[j] == "Cell Count")
                    {
                        // skipped
                        IndexCellcount = j;
                    }
                    else
                        TobeWritten += Descs[j] + ",";
			 
			    }

                TobeWritten = TobeWritten.Remove(TobeWritten.Length - 1);

                if (IsHeaderWritten == false)
                {
                    OriginalHeader = TobeWritten;
                    stream.WriteLine(TobeWritten);
                    IsHeaderWritten = true;
                }
                else
                {
                    // inconsistency between the headers... skip the plate
                    if (TobeWritten != OriginalHeader)
                    {
                        continue;
                    }
                }

                #endregion

                // now let's process the data
                int IdxRow = 0;
                while (!CSVReader.EndOfStream)
                {
                    CSVReader.ReadRow(CurrentRow);
                    TobeWritten = PlateName+",";
                    for (int j = 0; j < Descs.Count; j++)
                    {
                        if ((IdxBoundingBox > -1) && (j == IdxBoundingBox))
                        {
                            // let's process the bounding box
                            string BB = CurrentRow[j];
                            BB = BB.Remove(BB.Length - 1);
                            BB = BB.Remove(0, 1);

                            string[] Splitted = BB.Split(Sep, StringSplitOptions.None);
                            TobeWritten += Splitted[0] + "," + Splitted[1] + "," + Splitted[2] + "," + Splitted[3] + ",";
                            // j += 3;
                        }
                        else if (j == IndexRow)
                        {
                            TobeWritten += ConvertPosition(int.Parse(CurrentRow[IndexCol]), int.Parse(CurrentRow[IndexRow]))+",";
                        }
                        else if ((j == IndexCol)||(j==IndexCellcount)||(j==IndexCompound)||(j==IndexConcentration))
                        {
                            // do nothing
                        }
                        else
                        {
                            if (CurrentRow[j] != "")
                                TobeWritten += CurrentRow[j] + ",";
                            else
                                TobeWritten += "NaN,";
                        }

                    }
                    TobeWritten = TobeWritten.Remove(TobeWritten.Length - 1);
                    stream.WriteLine(TobeWritten );
                    IdxRow++;
                }

                CSVReader.Close();
                stream.Dispose();
                MyProgressBar.progressBar.Value++;
                MyProgressBar.progressBar.Update();
            }
            MyProgressBar.Close();
            #endregion

            #region let's build the database now

            // if (CSVFeedBackWindow == null) return;
            // if (CSVFeedBackWindow.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            string DBPath = Path + "\\DB";
            Directory.CreateDirectory(DBPath);
            cConvertCSVtoDB CCTODB = CSVtoDB(TPath, ",", DBPath);
            if (CCTODB == null)
            {
                return;
            }
            else
            {
                // update image accessor
                cGlobalInfo.OptionsWindow.radioButtonImageAccessDefined.Checked = false;
                cGlobalInfo.OptionsWindow.radioButtonImageAccessHarmony35.Checked = true;
                cGlobalInfo.OptionsWindow.textBoxImageAccesImagePath.Text = Path;

                cGlobalInfoToBeExported GlobalInfoToBeExported = new cGlobalInfoToBeExported();
                cGlobalInfo.OptionsWindow.TmpOptionPath = GlobalInfoToBeExported.Save(DBPath+"\\Options.opt");

                //cGlobalInfo.OptionsWindow.sav
            }

            #endregion
        }
Example #2
0
        private void importScreenDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dlg1 = new Ionic.Utils.FolderBrowserDialogEx();
            dlg1.Description = "Select the folder containing your files";
            dlg1.ShowNewFolderButton = true;
            dlg1.ShowEditBox = true;
            dlg1.ShowFullPathInEditBox = true;
            dlg1.RootFolder = System.Environment.SpecialFolder.Desktop;

            DialogResult result = dlg1.ShowDialog();
            if (result != DialogResult.OK) return;

            string Path = dlg1.SelectedPath;
            if (Directory.Exists(Path) == false) return;

            string[] ListFiles = null;

            try
            {
                ListFiles = Directory.GetFiles(Path, "*.csv", SearchOption.AllDirectories);
            }
            catch (System.Exception excep)
            {
                MessageBox.Show(excep.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (ListFiles.Length == 0)
            {
                MessageBox.Show("No CSV files found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            FormForPlateSelection FFP = new FormForPlateSelection(ListFiles,true);
            if (FFP.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            string[] ListFilesForPlates = FFP.GetListPlatesSelected();

            ImportFiles(ListFilesForPlates);


        }