Beispiel #1
0
 public Exon(Exon e)
 {
     chromosome = e.chromosome;
     Id         = e.Id;
     Range      = e.Range;
     Strand     = e.Strand;
     Name       = e.Name;
 }
Beispiel #2
0
        public static void TestNullModell4(String nameNull)
        {
            Chromosome chrOrig = new Chromosome("4");
            Chromosome chrNull = new Chromosome(nameNull);
            int        idOrig  = chrOrig.Id;
            int        idNull  = chrNull.Id;

            Range rangeOrig = chrOrig.GetQ();



            System.Data.Common.DbDataReader res =
                App.DB.Query("SELECT * FROM exon WHERE chromosomeId=" + idOrig + " AND start >= " +
                             rangeOrig.Start + " AND end <= " + rangeOrig.End + " ORDER BY start asc");

            /*
             *     App.DB.Query("SELECT * FROM exon WHERE chromosomeId=" + idOrig + " AND start >= " +
             *      rangeOrig.Start + " AND end <= " + rangeOrig.End + " ORDER BY RANDOM() LIMIT 50");
             * */

            int i = 0;

            while (res.Read())
            {
                String name     = res["name"].ToString();
                Exon   exonOrig = new Exon(res, chrOrig);

                System.Data.Common.DbDataReader resNull = App.DB.QueryRead("SELECT * FROM exon WHERE name='" +
                                                                           name + "' AND chromosomeId=" + idNull);

                if (!resNull.HasRows)
                {
                    App.Log(name + " not found in " + nameNull);
                    continue;
                }
                Exon exonNull = new Exon(resNull, chrNull);

                String seqOrig = exonOrig.Read();
                String seqNull = exonNull.Read();

                if (seqOrig == seqNull)
                {
                    //  if (exonNull.Strand != exonOrig.Strand) App.Log("Strand switcher! :");
                    App.Log("#" + i + " " + name + " OK");
                }
                else
                {
                    App.Log("#" + i + " " + name + " FFFFFUUUUUUU....");
                }
                i++;
            }
        }
        private void LoadExons()
        {
            Exons = new Exon[0];

            List <Exon> list = new List <Exon>();

            App.Status("Loading exons. This may take a while...");

            System.Data.Common.DbDataReader res =
                App.DB.Query("SELECT * FROM exon WHERE chromosomeId=" + Id + " ORDER BY start ASC");

            App.Status("Saving exons.");
            while (res.Read())
            {
                Exon exon = new Exon(res, this);
                list.Add(exon);
            }

            Exons = list.ToArray();


            App.Status("Indexing exons.");
            int numIndizes = (int)(Length / ExonRangeSize);

            ExonStarts = new int[numIndizes];

            int exonStart = 0;

            for (int i = 0; i < numIndizes; i++)
            {
                int start = i * ExonRangeSize;
                for (int j = exonStart; j < Exons.Length; j++)
                {
                    if (Exons[j].Start >= start)
                    {
                        ExonStarts[i] = j;
                        exonStart     = j;
                        break;
                    }
                }
            }

            App.Status();

            LoadedExons = true;
        }
        private void CreateChromosome()
        {
            App.DB.OpenFast();

            SQLiteCommand     command     = App.DB.CreateCommand();
            SQLiteTransaction transaction = App.DB.BeginTransaction();

            command.Transaction = transaction;

            String sql = "INSERT INTO exon (exonId, name, geneId, start, end, strand, isVirtual, chromosomeId) " +
                         "VALUES(@exonId, @name, @geneId, @start, @end, @strand, 1, " + NewChromosomeId + ")";

            command.CommandText = sql;



            int posOld  = SourceRange.Start;
            int posNew  = 1;
            int curGene = 0;

            byte[] buffer;

            FileStream fNew = File.Create(NewFilePath);

            do
            {
                App.Status("Writing Gene " + (curGene + 1) + " of " + Genes.Count);

                Gene geneOld = null;
                Gene geneNew = null;

                // WriteNonCodingSequence
                int nonCodingLength = 0;

                if (curGene < Genes.Count)
                {
                    geneOld = Genes[curGene];
                    geneNew = Genes[PositionsShuffled[curGene]];

                    nonCodingLength = geneOld.Start - posOld;
                    if (nonCodingLength < 0)
                    {
                        /* App.Log("Overlapping Gene: " + geneOld.Name + " - skipping");
                         * curGene++;
                         * continue;*/

                        int diff = posOld - geneOld.Start;
                        App.Log("Overlapping Gene: " + geneOld.Name + " - " + diff + "bp");
                        posOld          = geneOld.Start;
                        nonCodingLength = 0;
                    }
                }
                else
                {
                    nonCodingLength = SourceRange.End - posOld;
                }

                if (nonCodingLength > 0)
                {
                    Source.ReadBuffer(posOld, nonCodingLength, out buffer);
                    // App.Log("Reading Non Coding Sequence from pos " + posOld + ", length " + nonCodingLength);
                    fNew.Write(buffer, 0, buffer.Length);
                }
                posOld += nonCodingLength;
                posNew += nonCodingLength;

                // App.Log("# " + curGene + " NC Length " + nonCodingLength);


                if (curGene >= Genes.Count)
                {
                    break;
                }

                bool switchStrand = App.Random.NextBool();
                // switchStrand = false;

                // WriteNewGene
                Source.ReadBuffer(geneNew.Start, geneNew.Length, out buffer);
                //App.Log("Reading Gene New from pos " + geneNew.Start + ", length " + geneNew.Length);

                Gene geneNewNull = new Gene(geneNew);
                geneNewNull.Range = new Range(posNew, posNew + geneNew.Length);
                if (switchStrand)
                {
                    Sequencer.InvCompl(ref buffer);
                    if (geneNewNull.Strand == 1)
                    {
                        geneNewNull.Strand = -1;
                    }
                    else
                    {
                        geneNewNull.Strand = 1;
                    }
                    // App.Log("Switched Strands");
                }


                fNew.Write(buffer, 0, buffer.Length);


                // Exons
                System.Data.Common.DbDataReader res =
                    App.DB.Query("SELECT * FROM exon WHERE geneId=" + geneNew.Id + " and chromosomeId = " + Source.Id + " ORDER BY start ASC");

                while (res.Read())
                {
                    Exon exonOld = new Exon(res, Source);
                    Exon exonNew = new Exon(exonOld);

                    int bpFromGeneStart = exonNew.Start - geneNew.Start;

                    int newStart = geneNewNull.Start + bpFromGeneStart;
                    int newEnd   = newStart + exonOld.Length;

                    if (switchStrand)
                    {
                        newStart = geneNewNull.End - bpFromGeneStart - exonOld.Length;
                        newEnd   = geneNewNull.End - bpFromGeneStart;
                    }

                    exonNew.Range  = new Range(newStart, newEnd);
                    exonNew.Strand = geneNewNull.Strand;

                    command.Parameters.AddWithValue("@exonId", exonNew.Id);
                    command.Parameters.AddWithValue("@geneId", geneNewNull.Id);
                    command.Parameters.AddWithValue("@start", newStart);
                    command.Parameters.AddWithValue("@end", newEnd);
                    command.Parameters.AddWithValue("@strand", exonNew.Strand);
                    command.Parameters.AddWithValue("@name", exonNew.Name);
                    command.ExecuteNonQuery();
                }

                App.Log("# " + curGene + " " + geneNewNull.Name + "(ID " + geneNewNull.Id + ") -&gt; " + geneOld.Name + " - Pos: " + geneNewNull.Start + " Strand: " + geneOld.Strand + (switchStrand ? " (reversed)" : ""));

                //  App.Log("# " + curGene + " New Gene Length " + geneNew.Length);

                //  App.Log("# " + curGene + " posNew " + posNew + " - posOld "+posOld);

                //  App.Log("# " + curGene + " Old Gene Distance " + (Genes[curGene+1].Start-geneOld.Start));
                // Next Gene

                long filePos = fNew.Position;

                posNew += geneNewNull.Length;
                posOld += geneOld.Length;

                App.Assert(filePos == (posNew - 1));

                curGene++;

                App.LogScroll();
                //     if (curGene > 40) break;
            }while (posOld <= SourceRange.End);

            fNew.Close();

            transaction.Commit();
            command.Dispose();

            App.DB.Exec("UPDATE chromosome SET length=" + posNew + " WHERE id=" + NewChromosomeId);

            App.DB.Close();

            App.Status("");
        }
Beispiel #5
0
        protected void OnFusionEvent(Range fragment1, Range fragment2)
        {
            Exon ex1 = Source.WithinExon(fragment1.End);
            Exon ex2 = Source.WithinExon(fragment2.Start);

            //  10371808
            // 103718617
            if ((ex1 != null) && (ex2 != null))
            {
                // Both sides of the cut are within exons
                if (ex1.Strand == ex2.Strand)
                {
                    // Both exons are on same strand

                    int bpExon1      = (int)(fragment1.End - ex1.Start);
                    int bpExon2      = (int)(ex2.End - fragment2.Start);
                    int percentExon1 = (int)(((double)bpExon1 / ex1.Length) * 100);
                    int percentExon2 = (int)(((double)bpExon2 / ex2.Length) * 100);

                    if ((bpExon1 > 100) && (bpExon2 > 100))
                    {
                        String seqFusion, seq1, seq2;

                        Range rangeLeft  = new Range(ex1.Start, fragment1.End);
                        Range rangeRight = new Range(fragment2.Start, ex2.End);

                        seq1      = Source.Read(rangeLeft.Start, (int)rangeLeft.Length);
                        seq2      = Source.Read(rangeRight.Start, (int)rangeRight.Length);
                        seqFusion = seq1 + seq2;

                        Dictionary <String, String> orfs = new Dictionary <string, string>();
                        orfs.Add("Forward 0", Sequencer.GetORF(seqFusion, 0));
                        orfs.Add("Forward 1", Sequencer.GetORF(seqFusion, 1));
                        orfs.Add("Forward 2", Sequencer.GetORF(seqFusion, 2));

                        String seqInv = Sequencer.Invert(Sequencer.Complement(seqFusion));
                        orfs.Add("Reverse 0", Sequencer.GetORF(seqInv, 0));
                        orfs.Add("Reverse 1", Sequencer.GetORF(seqInv, 1));
                        orfs.Add("Reverse 2", Sequencer.GetORF(seqInv, 2));

                        Dictionary <String, String> peptides = new Dictionary <string, string>();

                        bool bSignificantPeptides = false;

                        foreach (String key in orfs.Keys)
                        {
                            if (orfs[key].Length >= 120)
                            {
                                String pep = Sequencer.Translate(orfs[key]);

                                peptides.Add(key, pep);
                                bSignificantPeptides = true;
                            }
                        }

                        if (bSignificantPeptides)
                        {
                            NumGenes++;

                            String htmlEx1 = "<a href='http://www.ensembl.org/Homo_sapiens/Search/Details?species=Homo_sapiens;idx=Transcript;end=1;q=" + ex1.Name +
                                             "' target='_blank'>" + ex1.Name + "</a> " + bpExon1.ToString() + "bp (" + percentExon1.ToString() + "%)";

                            String htmlEx2 = "<a href='http://www.ensembl.org/Homo_sapiens/Search/Details?species=Homo_sapiens;idx=Transcript;end=1;q=" + ex2.Name +
                                             "' target='_blank'>" + ex2.Name + "</a> " + bpExon2.ToString() + "bp (" + percentExon2.ToString() + "%)";

                            if (ExtendedOutput)
                            {
                                App.Log("<b>Found Fusion Gene #" + NumGenes + ": </b>" + htmlEx1 + " <b>&</b> " + htmlEx2 + "");
                            }
                            // App.Log("Fusion Sequence: " + seqFusion);

                            foreach (String key in peptides.Keys)
                            {
                                String pep = peptides[key];

                                if (ExtendedOutput)
                                {
                                    App.Log(key + ": " + pep);
                                }

                                FusionSequence fs = new FusionSequence();
                                fs.SequenceId = NumOrf.ToString();
                                fs.IsHit      = false;
                                fs.Exon1      = ex1;
                                fs.Exon2      = ex2;
                                fs.OrfName    = key;
                                fs.Range1     = rangeLeft;
                                fs.Range2     = rangeRight;
                                fs.Sequence   = pep;
                                FusionSequences.Insert(NumOrf, fs);

                                Hmmer.AddSequence(pep, fs.SequenceId);

                                //String sequenceName = "#" + NumGenes+" "+ex1.Name + " " + percentExon1.ToString() + "% & " + ex2.Name + " " + percentExon2.ToString() + "% ORF " + key;
                                NumOrf++;
                            }
                        } // if (bSignificantPeptides)
                    }     //   if ((bpExon1 > 100) && (bpExon2 > 100))
                }         //  if (ex1.Strand == ex2.Strand)
            }             //  if ((ex1 != null) && (ex2 != null))
            else
            {
                //App.Log("No Fusion Gene :(");
            }
        }