Example #1
0
        public async void ReadDataGenotype(string path)
        {
            Cursor.Current = Cursors.WaitCursor;
            var rawData = await Task.Run(() => readFileGenData(path));

            Cursor.Current = Cursors.Default;
            List <Dictionary <int, string> > filteredData = new List <Dictionary <int, string> >();

            // filter out the locus that are not in the both files
            //iterate over all the raw data and filter only the ones that are both
            if (rawData == null)
            {
                MessageBox.Show("Genotype file is not in correct format.\nExample:\nmarkerName\t010101010", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            List <Locus> LocusList = new List <Locus>();

            db = DatabaseProvider.GetDatabase();
            lock (db)
            {
                //in filterd data we removed the markers that are not found in the map
                foreach (Dictionary <int, string> dic in rawData)
                {
                    var locus = findLocus(db.GenomeOrganization.Chromosome, dic[0]);
                    if (locus != null)
                    {
                        filteredData.Add(dic);
                        LocusList.Add(locus);
                    }
                }
                if (filteredData.Count == 0)
                {
                    MessageBox.Show("Cannot find any Genotypes in the Genetic Map,\nPlease reload the Genetic map with a relevant ogranism.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    errorMessage = ErrorMessage.BadGeneticMap;
                    return;
                }
                var population = Regex.Split(filteredData[0][1], string.Empty);
                if (population[0] == "")
                {
                    population = population.Skip(1).ToArray();
                }
                if (population[population.Length - 1] == "")
                {
                    Array.Resize(ref population, population.Length - 1);
                }

                if (db.SubData.Count == 0)
                {
                    for (int i = 0; i < population.Length; i++)
                    {
                        //create classes at first use
                        DataIndividualsAndTraits individualsAndTraits = new DataIndividualsAndTraits();
                        db.SubData.Add(individualsAndTraits);
                        db.SubData[i].Genotype   = new int[1, filteredData.Count];
                        db.SubData[i].GenotypeOk = new bool[1, filteredData.Count];
                        db.SubData[i].Locus      = LocusList;
                    }
                }
                else
                {
                    //already created just need to define the size
                    for (int i = 0; i < population.Length; i++)
                    {
                        db.SubData[i].Genotype   = new int[1, filteredData.Count];
                        db.SubData[i].GenotypeOk = new bool[1, filteredData.Count];
                        db.SubData[i].Locus      = LocusList;
                    }
                }

                int subIndex = 0;
                foreach (Dictionary <int, string> dic in filteredData)
                {
                    var pop = Regex.Split(dic[1], string.Empty);
                    if (pop[0] == "")
                    {
                        pop = pop.Skip(1).ToArray();
                    }
                    if (pop[pop.Length - 1] == "")
                    {
                        Array.Resize(ref pop, pop.Length - 1);
                    }


                    int offSetIndex = 0;

                    foreach (DataIndividualsAndTraits indiv in db.SubData)
                    {
                        if (pop[offSetIndex].Equals("-"))
                        {
                            //missing data - need to add another state to go with 1 is true ,0 false , -1 missing
                            indiv.Genotype[0, subIndex]   = -1;
                            indiv.GenotypeOk[0, subIndex] = false;
                        }
                        else if (pop[offSetIndex].Equals("1"))
                        {
                            // data is 1
                            indiv.Genotype[0, subIndex]   = 1;
                            indiv.GenotypeOk[0, subIndex] = true;
                        }
                        else if (pop[offSetIndex].Equals("0"))
                        {
                            //data is 0
                            indiv.Genotype[0, subIndex]   = 0;
                            indiv.GenotypeOk[0, subIndex] = false;
                        }
                        offSetIndex = (offSetIndex + 1) % pop.Length;
                    }

                    subIndex = (subIndex + 1) % filteredData.Count;
                }
            }
        }
Example #2
0
        public async void ReadDataPhenotype(string path)
        {
            Cursor.Current = Cursors.WaitCursor;
            var rawData = await Task.Run(() => readPhenData(path));

            Cursor.Current = Cursors.Default;
            if (rawData == null)
            {
                MessageBox.Show("Phenotype file is not in correct format.\nExample:tc111\t0.25\t0.21...", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //create list of traits
            List <Trait> tempListTraits = new List <Trait>();

            //get full names of the traits
            for (int i = 0; i < rawData.Count; i++)
            {
                string traitName;
                rawData[i].TryGetValue(0, out traitName);
                Trait t = new Trait();
                t.Id       = i;
                t.NameFull = traitName;
                tempListTraits.Add(t);
            }
            string tempValue = null;

            for (int i = 0; i < rawData.Count; i++)
            {
                //iterate and remove all the trait names since we dont need them anymore
                rawData[i].Remove(0);
                rawData[i].TryGetValue(rawData[i].Count, out tempValue);
                if (tempValue.Equals(""))
                {
                    rawData[i].Remove(rawData[i].Count);
                }
            }
            db = DatabaseProvider.GetDatabase();
            lock (db)
            {
                if (db.SubData.Count == 0)
                {
                    for (int i = 0; i < rawData[0].Count; i++)
                    {
                        //assign all the individuals the same traits from file
                        DataIndividualsAndTraits individualsAndTraits = new DataIndividualsAndTraits();
                        individualsAndTraits.Trait = tempListTraits;
                        db.SubData.Add(individualsAndTraits);
                    }
                }
                else
                {
                    for (int i = 0; i < rawData[0].Count; i++)
                    {
                        //data already exists thus need to add it the traits
                        db.SubData[i].Trait = tempListTraits;
                    }
                }
                //init all arrays
                for (int j = 0; j < rawData[0].Count; j++)
                {
                    db.SubData[j].TraitValueOk = new bool[1, rawData.Count];
                    db.SubData[j].TraitValue   = new float[1, rawData.Count];
                }
                int traitCounter = 0;
                int dicCounter   = 1;
                foreach (Dictionary <int, string> dic in rawData)
                {
                    for (int i = 0; i < db.SubData.Count; i++)
                    {
                        string tempVal = dic[dicCounter];
                        if (tempVal.Equals("$") || tempVal.Equals(""))
                        {
                            //missing data
                            db.SubData[i].TraitValueOk[0, traitCounter] = false;
                            db.SubData[i].TraitValue[0, traitCounter]   = 0;
                        }
                        else
                        {
                            db.SubData[i].TraitValueOk[0, traitCounter] = true;
                            float val = float.Parse(tempVal);
                            db.SubData[i].TraitValue[0, traitCounter] = val;
                        }
                        dicCounter++;
                        dicCounter = dicCounter % 117;
                        if (dicCounter == 0)
                        {
                            dicCounter = 1;
                        }
                    }
                    traitCounter++;
                }
            }
        }