Exemplo n.º 1
0
        private void MatchingKitsFrm_Load(object sender, EventArgs e)
        {
            lblKit.Text  = kit;
            lblName.Text = GGKUtilLib.queryDatabase("kit_master", new string[] { "name" }, "WHERE kit_no='" + kit + "'").Rows[0].ItemArray[0].ToString();
            DataTable dt = GGKUtilLib.QueryDB("SELECT cmp_id,kit'Kit No',name'Name',at_longest'Autosomal Longest',at_total'Autosomal Total',x_longest'X Longest',x_total'X Total',mrca'MRCA' FROM (SELECT a.cmp_id,a.kit1'kit',b.name,a.at_longest,a.at_total,a.x_longest,a.x_total,a.mrca FROM cmp_status a,kit_master b WHERE a.at_total!=0 AND a.kit1!='" + kit + "' AND a.kit2='" + kit + "' AND a.status_autosomal=1 AND b.kit_no=a.kit1 AND b.disabled=0 UNION SELECT a.cmp_id,a.kit2'kit',b.name,a.at_longest,a.at_total,a.x_longest,a.x_total,a.mrca FROM cmp_status a,kit_master b WHERE a.at_total!=0 AND a.kit2!='" + kit + "' AND a.kit1='" + kit + "' AND a.status_autosomal=1 AND b.kit_no=a.kit2 AND b.disabled=0) ORDER BY at_longest DESC,at_total DESC");

            dgvMatches.Columns.Clear();
            dgvMatches.DataSource         = dt;
            dgvMatches.Columns[0].Visible = false;

            dgvMatches.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvMatches.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvMatches.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvMatches.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvMatches.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvMatches.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvMatches.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            DataGridViewCellStyle style = new DataGridViewCellStyle();

            style.Format = "N2";

            dgvMatches.Columns[3].DefaultCellStyle = style;
            dgvMatches.Columns[4].DefaultCellStyle = style;
            dgvMatches.Columns[5].DefaultCellStyle = style;
            dgvMatches.Columns[6].DefaultCellStyle = style;
        }
Exemplo n.º 2
0
        void bWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] o      = (string[])e.Argument;
            string   cmp_id = o[0];
            string   kit2   = o[1];
            string   name2  = o[2];

            segment_dt = GGKUtilLib.QueryDB("select chromosome'Chromosome',start_position'Start Position',end_position'End Position',segment_length_cm'Segment Length (cM)',snp_count'SNP Count',segment_id from cmp_autosomal where cmp_id='" + cmp_id + "'");

            if (GGKUtilLib.isPhased(kit))
            {
                phased_kit   = kit;
                unphased_kit = kit2;
                phased       = true;
            }
            else if (GGKUtilLib.isPhased(kit2))
            {
                phased_kit   = kit2;
                unphased_kit = kit;
                phased       = true;
            }
            else
            {
                phased = false;
            }
            e.Result = new string[] { kit2, name2 };
        }
Exemplo n.º 3
0
        private void bwROH_DoWork(object sender, DoWorkEventArgs e)
        {
            DataTable dt  = GGKUtilLib.QueryDB("select kit_no,roh_status from kit_master where reference=0 and disabled=0");
            string    kit = null;
            string    roh = null;

            foreach (DataRow row in dt.Rows)
            {
                if (bwROH.CancellationPending)
                {
                    break;
                }
                kit = row.ItemArray[0].ToString();
                roh = row.ItemArray[1].ToString();
                if (roh == "0")
                {
                    bwROH.ReportProgress(dt.Rows.IndexOf(row) * 100 / dt.Rows.Count, "Runs of Homozygosity for kit #" + kit + " (" + GGKUtilLib.getKitName(kit) + ") - Processing ...");
                    GGKUtilLib.ROH(kit);
                }
                else if (roh == "1")
                {
                    bwROH.ReportProgress(dt.Rows.IndexOf(row) * 100 / dt.Rows.Count, "Runs of Homozygosity for kit #" + kit + " (" + GGKUtilLib.getKitName(kit) + ") - Already Exists. Skipping..");
                }
            }
        }
        private void bwIChkAndFix_DoWork(object sender, DoWorkEventArgs e)
        {
            GGKUtilLib.integrityCheckAndFix();
            DataTable dt = GGKUtilLib.QueryDB("select * from kit_master where reference=1");

            if (dt.Rows.Count == 0)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    admixtureToolStripMenuItem.Enabled = false;
                }));
            }
        }
Exemplo n.º 5
0
 void bWorker2_DoWork(object sender, DoWorkEventArgs e)
 {
     object[] o = (object[])e.Argument;
     dt_alleles = GGKUtilLib.QueryDB("select rsid'RSID',position'Position',kit1_genotype'" + o[0] + " (" + o[1] + ")',kit2_genotype'" + o[2] + " (" + o[3] + ")',match'Match' from cmp_mrca where segment_id='" + o[4] + "'");
 }
Exemplo n.º 6
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();
                }
            }
        }
        private void AdmixtureFrm_Load(object sender, EventArgs e)
        {
            kitLbl.Text = kit + " (" + GGKUtilLib.getKitName(kit) + ")";
            DataTable dt    = GGKUtilLib.QueryDB("select name,at_total,at_longest,x,y FROM (select b.name,a.at_total,a.at_longest,b.x,b.y from cmp_status a,kit_master b where a.kit1='" + kit + "' and a.kit2=b.kit_no and a.kit2 like 'HGDP%' and a.status_autosomal=1 and a.at_longest<3 and a.at_total!=0 UNION select b.name,a.at_total,a.at_longest,b.x,b.y from cmp_status a,kit_master b where a.kit2='" + kit + "' and a.kit1=b.kit_no and a.kit1 like 'HGDP%' and a.status_autosomal=1 and a.at_longest<3 and a.at_total!=0) ORDER BY at_total DESC");
            double    total = 0.0;

            foreach (DataRow row in dt.Rows)
            {
                total += double.Parse(row.ItemArray[1].ToString());
            }

            DataTable adx_table = new DataTable();

            adx_table.Columns.Add("Population");
            adx_table.Columns.Add("Location");
            adx_table.Columns.Add("Total Shared (cM)");
            adx_table.Columns.Add("Longest (cM)");
            adx_table.Columns.Add("Percentage");
            adx_table.Columns.Add("X");
            adx_table.Columns.Add("Y");

            string population = null;
            string location   = null;
            double at_total   = 0.0;
            string at_longest = null;
            double percentage = 0.0;

            string[] data = null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                data       = dt.Rows[i].ItemArray[0].ToString().Replace("_", " ").Split(new char[] { ',' });
                population = data[0];
                location   = data[1];
                at_total   = double.Parse(dt.Rows[i].ItemArray[1].ToString());
                at_longest = dt.Rows[i].ItemArray[2].ToString();
                percentage = (at_total * 100 / total);

                adx_table.Rows.Add(new object[] { population, location, at_total, at_longest, percentage.ToString("#0.00"), dt.Rows[i].ItemArray[3], dt.Rows[i].ItemArray[4] });

                chart1.Series[0].Points.AddXY(population + ", " + location + " (" + percentage.ToString("#0.00") + "%)", new object[] { percentage });
            }

            foreach (DataPoint p in chart1.Series[0].Points)
            {
                p.IsVisibleInLegend = false;
            }
            dgv_Admixture.Columns.Clear();
            dgv_Admixture.DataSource = adx_table;

            dgv_Admixture.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgv_Admixture.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgv_Admixture.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgv_Admixture.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgv_Admixture.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgv_Admixture.Columns[5].Visible      = false;
            dgv_Admixture.Columns[6].Visible      = false;

            int      percent = 0;
            int      x       = 0;
            int      y       = 0;
            Image    img     = pbWorldMap.Image;
            Graphics g       = Graphics.FromImage(img);

            plotted.Clear();
            foreach (DataRow row in adx_table.Rows)
            {
                percent = (int)double.Parse(row.ItemArray[4].ToString());
                x       = int.Parse(row.ItemArray[5].ToString());
                y       = int.Parse(row.ItemArray[6].ToString());
                if (!plotted.Contains(x + ":" + y))
                {
                    if (percent > 50) // plotting 100% is too big and ugly.
                    {
                        percent = 50;
                    }
                    setHeatMap(g, percent, x, y);
                    plotted.Add(x + ":" + y);
                }
            }
            g.Save();

            pbWorldMap.Image = img;
        }
Exemplo n.º 8
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();
                    }
                }
            }
        }
Exemplo n.º 9
0
        private void bwPhasing_DoWork(object sender, DoWorkEventArgs e)
        {
            if (father_kit != "Unknown" && mother_kit != "Unknown")
            {
                dt = GGKUtilLib.QueryDB("SELECT c.[rsid]'RSID',c.[chromosome]'Chromosome',c.[position]'Position',c.[genotype]\"Child\",COALESCE(f.[genotype],'--')\"Father\",COALESCE(m.[genotype],'--')\"Mother\",''\"Phased Paternal\",''\"Phased Maternal\"  FROM kit_autosomal c left outer join kit_autosomal f,kit_autosomal m on f.rsid=c.rsid AND m.rsid=c.rsid WHERE c.kit_no='" + child_kit + "' AND f.kit_no='" + father_kit + "' AND m.[kit_no]='" + mother_kit + "' ORDER BY cast(c.chromosome as integer),c.position");
            }
            else if (father_kit != "Unknown" && mother_kit == "Unknown")
            {
                dt = GGKUtilLib.QueryDB("SELECT c.[rsid]'RSID',c.[chromosome]'Chromosome',c.[position]'Position',c.[genotype]\"Child\",COALESCE(f.[genotype],'--')\"Father\",'--'\"Mother\",''\"Phased Paternal\",''\"Phased Maternal\"  FROM kit_autosomal c left outer join kit_autosomal f on f.rsid=c.rsid  WHERE c.kit_no='" + child_kit + "' AND f.kit_no='" + father_kit + "' ORDER BY cast(c.chromosome as integer),c.position");
            }
            else if (father_kit == "Unknown" && mother_kit != "Unknown")
            {
                dt = GGKUtilLib.QueryDB("SELECT c.[rsid]'RSID',c.[chromosome]'Chromosome',c.[position]'Position',c.[genotype]\"Child\",'--'\"Father\",COALESCE(m.[genotype],'--')\"Mother\" ,''\"Phased Paternal\",''\"Phased Maternal\"  FROM kit_autosomal c left outer join kit_autosomal m on m.rsid=c.rsid WHERE c.kit_no='" + child_kit + "' AND m.kit_no='" + mother_kit + "' ORDER BY cast(c.chromosome as integer),c.position");
            }

            // after phasing...
            string child           = null;
            string father          = null;
            string mother          = null;
            string phased_paternal = null;
            string phased_maternal = null;

            object[] o   = null;
            string   nc  = "N";
            bool     amb = false;

            foreach (DataRow row in dt.Rows)
            {
                o = row.ItemArray;
                phased_paternal = "";
                phased_maternal = "";
                child           = o[3].ToString();
                father          = o[4].ToString();
                mother          = o[5].ToString();

                if (child.Length == 1)
                {
                    child = child + child;
                }

                //check
                if ((father.Replace(child[0].ToString(), "").Replace(child[1].ToString(), "") == father || mother.Replace(child[0].ToString(), "").Replace(child[1].ToString(), "") == mother) && o[1].ToString() != "X" && father != "--" && mother != "--" && child != "--")
                {
                    mutatedRow.Add(dt.Rows.IndexOf(row));
                }

                amb = false;
                if (father == child && child[0] != child[1] && mother == "--")
                {
                    amb = true;
                }
                else if (mother == child && child[0] != child[1] && father == "--")
                {
                    amb = true;
                }
                else if (father == child && child[0] != child[1] && mother == child)
                {
                    amb = true;
                }

                if (amb)
                {
                    ambiguousRow.Add(dt.Rows.IndexOf(row));
                    nc = getNucleotideCode(child[0].ToString(), child[1].ToString());
                    row.SetField(6, nc);
                    row.SetField(7, nc);
                    phased_paternal = nc;
                    phased_maternal = nc;
                    continue;
                }


                if (child == "--" || child == "??")
                {
                    if (father[0] == father[1] && father == mother && o[1].ToString() != "X")
                    {
                        row.SetField(6, father[0].ToString());
                        row.SetField(7, father[0].ToString());
                        phased_paternal = father[0].ToString();
                        phased_maternal = father[0].ToString();
                        continue;
                    }
                }

                if (male && o[1].ToString() == "X")
                {
                    child = child[0].ToString();
                    if (child == "-" && mother != "--")
                    {
                        row.SetField(6, "");
                        row.SetField(7, mother[0].ToString());
                        phased_paternal = "";
                        phased_maternal = mother[0].ToString();
                        continue;
                    }
                }
                else
                {
                    if (child[0] == child[1] && child[0] != '-' && child[0] != '?')
                    {
                        row.SetField(6, child[0].ToString());
                        row.SetField(7, child[0].ToString());
                        phased_paternal = child[0].ToString();
                        phased_maternal = child[0].ToString();
                        continue;
                    }
                }

                if (o[1].ToString() != "X")
                {
                    autosomalSingleSNPPhase(child, father, mother, row, ref phased_paternal, ref phased_maternal);
                }
                else if (o[1].ToString() == "X")
                {
                    if (male)
                    {
                        row.SetField(3, child[0].ToString());
                        row.SetField(4, "");
                        row.SetField(6, "");
                        row.SetField(7, child[0].ToString());
                        phased_paternal = "";
                        phased_maternal = child[0].ToString();
                    }
                    else
                    {
                        autosomalSingleSNPPhase(child, father, mother, row, ref phased_paternal, ref phased_maternal);
                    }
                }

                if (phased_paternal == "" && phased_maternal != "")
                {
                    phased_paternal = child.Replace(phased_maternal, "");
                    if (phased_paternal.Length > 0)
                    {
                        phased_paternal = phased_paternal[0].ToString();
                    }
                    row.SetField(6, phased_paternal);
                }
                if (phased_maternal == "" && phased_paternal != "")
                {
                    phased_maternal = child.Replace(phased_paternal, "");
                    if (phased_maternal.Length > 0)
                    {
                        phased_maternal = phased_maternal[0].ToString();
                    }
                    row.SetField(7, phased_maternal);
                }
            }

            // save to kit_phased
            string rsid       = null;
            string chromosome = null;
            string position   = null;

            bwPhasing.ReportProgress(-1, "Saving Phased Kit " + child_kit + " ...");
            SQLiteConnection conn = GGKUtilLib.getDBConnection();

            SQLiteCommand cmd = new SQLiteCommand("DELETE FROM kit_phased where kit_no=@kit_no", conn);

            cmd.Parameters.AddWithValue("@kit_no", child_kit);
            cmd.ExecuteNonQuery();

            using (SQLiteTransaction trans = conn.BeginTransaction())
            {
                foreach (DataRow row in dt.Rows)
                {
                    o          = row.ItemArray;
                    rsid       = o[0].ToString();
                    chromosome = o[1].ToString();
                    position   = o[2].ToString();

                    phased_paternal = o[6].ToString();
                    phased_maternal = o[7].ToString();

                    cmd = new SQLiteCommand("INSERT OR REPLACE INTO kit_phased(kit_no,rsid,chromosome,position,paternal_genotype,maternal_genotype,paternal_kit_no,maternal_kit_no) VALUES(@kit_no,@rsid,@chromosome,@position,@paternal_genotype,@maternal_genotype,@paternal_kit_no,@maternal_kit_no)", conn);
                    cmd.Parameters.AddWithValue("@kit_no", child_kit);
                    cmd.Parameters.AddWithValue("@rsid", rsid);
                    cmd.Parameters.AddWithValue("@chromosome", chromosome);
                    cmd.Parameters.AddWithValue("@position", position);
                    cmd.Parameters.AddWithValue("@paternal_genotype", phased_paternal);
                    cmd.Parameters.AddWithValue("@maternal_genotype", phased_maternal);
                    if (father_kit == "Unknown")
                    {
                        cmd.Parameters.AddWithValue("@paternal_kit_no", "");
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@paternal_kit_no", father_kit);
                    }
                    if (mother_kit == "Unknown")
                    {
                        cmd.Parameters.AddWithValue("@maternal_kit_no", "");
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@maternal_kit_no", mother_kit);
                    }
                    cmd.ExecuteNonQuery();
                }
                trans.Commit();
            }
        }