Esempio n. 1
0
        private void bwPhaseVisualizer_DoWork(object sender, DoWorkEventArgs e)
        {
            DataTable dt_existing = GGKUtilLib.QueryDB("select segment_image,segment_xml from cmp_phased where phased_kit='" + phased_kit + "' and match_kit='" + unphased_kit + "' and chromosome='" + chromosome + "' and start_position=" + start_position + " and end_position=" + end_position);

            if (dt_existing.Rows.Count > 0)
            {
                object[] o   = dt_existing.Rows[0].ItemArray;
                string   xml = o[1].ToString();
                dt = new DataTable();
                MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(xml));
                dt.ReadXml(ms);


                this.Invoke(new MethodInvoker(delegate
                {
                    dgvSegment.DataSource = dt;

                    dgvSegment.Columns[0].HeaderText = "Position";
                    dgvSegment.Columns[1].HeaderText = GGKUtilLib.sqlSafe(GGKUtilLib.getKitName(unphased_kit));
                    dgvSegment.Columns[2].HeaderText = GGKUtilLib.sqlSafe(GGKUtilLib.getKitName(phased_kit)) + " (Paternal)";
                    dgvSegment.Columns[3].HeaderText = GGKUtilLib.sqlSafe(GGKUtilLib.getKitName(phased_kit)) + " (Maternal)";

                    dgvSegment.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    dgvSegment.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    dgvSegment.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    dgvSegment.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                    dgvSegment.Columns[0].ReadOnly = true;
                    dgvSegment.Columns[1].ReadOnly = true;
                    dgvSegment.Columns[2].ReadOnly = true;
                    dgvSegment.Columns[3].ReadOnly = true;
                }));

                byte[] image_array = (byte[])o[0];
                Image  img         = GGKUtilLib.byteArrayToImage(image_array);
                this.Invoke(new MethodInvoker(delegate
                {
                    original        = new Bitmap(img, 600, 150);
                    pbSegment.Image = original;
                }));
            }
            else
            {
                dt = GGKUtilLib.QueryDB("select a.position,a.genotype,p.paternal_genotype,p.maternal_genotype from kit_autosomal a,kit_phased p where a.kit_no='" + unphased_kit + "' and a.position>" + start_position + " and a.position<" + end_position + " and a.chromosome='" + chromosome + "' and p.rsid=a.rsid and p.kit_no='" + phased_kit + "' order by a.position");
                if (dt.Rows.Count > 0)
                {
                    if (this.IsHandleCreated)
                    {
                        this.Invoke(new MethodInvoker(delegate
                        {
                            dgvSegment.DataSource = dt;

                            dgvSegment.Columns[0].HeaderText = "Position";
                            dgvSegment.Columns[1].HeaderText = GGKUtilLib.sqlSafe(GGKUtilLib.getKitName(unphased_kit));
                            dgvSegment.Columns[2].HeaderText = GGKUtilLib.sqlSafe(GGKUtilLib.getKitName(phased_kit)) + " (Paternal)";
                            dgvSegment.Columns[3].HeaderText = GGKUtilLib.sqlSafe(GGKUtilLib.getKitName(phased_kit)) + " (Maternal)";

                            dgvSegment.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                            dgvSegment.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                            dgvSegment.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                            dgvSegment.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

                            dgvSegment.Columns[0].ReadOnly = true;
                            dgvSegment.Columns[1].ReadOnly = true;
                            dgvSegment.Columns[2].ReadOnly = true;
                            dgvSegment.Columns[3].ReadOnly = true;
                        }));
                        Image img = GGKUtilLib.getPhasedSegmentImage(dt, chromosome);
                        this.Invoke(new MethodInvoker(delegate
                        {
                            original        = img;
                            pbSegment.Image = img;
                        }));
                    }

                    dt.TableName = "cmp_phased";
                    StringBuilder sb = new StringBuilder();
                    StringWriter  w  = new StringWriter(sb);
                    dt.WriteXml(w, XmlWriteMode.WriteSchema);
                    string segment_xml = sb.ToString();

                    SQLiteConnection conn = GGKUtilLib.getDBConnection();
                    SQLiteCommand    cmd  = new SQLiteCommand("INSERT INTO cmp_phased(phased_kit,match_kit,chromosome,start_position,end_position,segment_image,segment_xml) VALUES (@phased_kit,@match_kit,@chromosome,@start_position,@end_position,@segment_image,@segment_xml)", conn);
                    cmd.Parameters.AddWithValue("@phased_kit", phased_kit);
                    cmd.Parameters.AddWithValue("@match_kit", unphased_kit);
                    cmd.Parameters.AddWithValue("@chromosome", chromosome);
                    cmd.Parameters.AddWithValue("@start_position", start_position);
                    cmd.Parameters.AddWithValue("@end_position", end_position);
                    byte[] image_bytes = GGKUtilLib.imageToByteArray(original);
                    cmd.Parameters.Add("@segment_image", DbType.Binary, image_bytes.Length).Value = image_bytes;
                    cmd.Parameters.AddWithValue("@segment_xml", segment_xml);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        }
Esempio n. 2
0
        private void bwPhaseVisualizer_DoWork(object sender, DoWorkEventArgs e)
        {
            DataTable dt2            = GGKUtilLib.QueryDB("select distinct kit_no from kit_phased");
            string    phased_kit     = null;
            string    unphased_kit   = null;
            string    chromosome     = null;
            string    start_position = null;
            string    end_position   = null;
            int       percent        = 0;

            foreach (DataRow row in dt2.Rows)
            {
                if (bwPhaseVisualizer.CancellationPending)
                {
                    break;
                }
                phased_kit = row.ItemArray[0].ToString();
                percent    = dt2.Rows.IndexOf(row) * 100 / dt2.Rows.Count;
                bwPhaseVisualizer.ReportProgress(percent, "Phased Segments for kit #" + phased_kit + " (" + GGKUtilLib.getKitName(phased_kit) + ") - Processing ...");

                DataTable dt3 = GGKUtilLib.QueryDB("select unphased_kit,chromosome,start_position,end_position FROM (select kit1'unphased_kit',chromosome,start_position,end_position from cmp_autosomal where kit2='" + phased_kit + "' UNION select kit2'unphased_kit',chromosome,start_position,end_position from cmp_autosomal where kit1='" + phased_kit + "') order by cast(chromosome as integer),start_position");

                foreach (DataRow row3 in dt3.Rows)
                {
                    if (bwPhaseVisualizer.CancellationPending)
                    {
                        break;
                    }
                    unphased_kit   = row3.ItemArray[0].ToString();
                    chromosome     = row3.ItemArray[1].ToString();
                    start_position = row3.ItemArray[2].ToString();
                    end_position   = row3.ItemArray[3].ToString();

                    DataTable exists = GGKUtilLib.QueryDB("select * from cmp_phased where phased_kit='" + phased_kit + "' and match_kit='" + unphased_kit + "' and chromosome='" + chromosome + "' and start_position=" + start_position + " and end_position=" + end_position);

                    if (exists.Rows.Count > 0)
                    {
                        //already exists...
                        if (!redo_visual)
                        {
                            bwPhaseVisualizer.ReportProgress(percent, "Segment [" + GGKUtilLib.getKitName(phased_kit) + ":" + GGKUtilLib.getKitName(unphased_kit) + "] Chr " + chromosome + ": " + start_position + "-" + end_position + ", Already Processed. Skipping ...");
                            continue;
                        }
                        else
                        {
                            GGKUtilLib.UpdateDB("DELETE from cmp_phased where phased_kit='" + phased_kit + "'");
                        }
                    }

                    bwPhaseVisualizer.ReportProgress(percent, "Segment [" + GGKUtilLib.getKitName(phased_kit) + ":" + GGKUtilLib.getKitName(unphased_kit) + "] Chr " + chromosome + ": " + start_position + "-" + end_position + ", Processing ...");


                    DataTable dt = GGKUtilLib.QueryDB("select a.position,a.genotype,p.paternal_genotype,p.maternal_genotype from kit_autosomal a,kit_phased p where a.kit_no='" + unphased_kit + "' and a.position>" + start_position + " and a.position<" + end_position + " and a.chromosome='" + chromosome + "' and p.rsid=a.rsid and p.kit_no='" + phased_kit + "' order by a.position");
                    if (dt.Rows.Count > 0)
                    {
                        if (bwPhaseVisualizer.CancellationPending)
                        {
                            break;
                        }

                        Image img = GGKUtilLib.getPhasedSegmentImage(dt, chromosome);

                        dt.TableName = "cmp_phased";
                        StringBuilder sb = new StringBuilder();
                        StringWriter  w  = new StringWriter(sb);
                        dt.WriteXml(w, XmlWriteMode.WriteSchema);
                        string segment_xml = sb.ToString();

                        SQLiteConnection conn = GGKUtilLib.getDBConnection();
                        SQLiteCommand    cmd  = new SQLiteCommand("INSERT INTO cmp_phased(phased_kit,match_kit,chromosome,start_position,end_position,segment_image,segment_xml) VALUES (@phased_kit,@match_kit,@chromosome,@start_position,@end_position,@segment_image,@segment_xml)", conn);
                        cmd.Parameters.AddWithValue("@phased_kit", phased_kit);
                        cmd.Parameters.AddWithValue("@match_kit", unphased_kit);
                        cmd.Parameters.AddWithValue("@chromosome", chromosome);
                        cmd.Parameters.AddWithValue("@start_position", start_position);
                        cmd.Parameters.AddWithValue("@end_position", end_position);
                        byte[] image_bytes = GGKUtilLib.imageToByteArray(img);
                        cmd.Parameters.Add("@segment_image", DbType.Binary, image_bytes.Length).Value = image_bytes;
                        cmd.Parameters.AddWithValue("@segment_xml", segment_xml);
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
            }
        }