예제 #1
0
        /// <summary>
        /// Find the last position before 'pos' within an exon
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        private long LastExonPositionBefore(long pos)
        {
            long last = -1;

            foreach (Exon ex in Exons.OrderBy(x => x.OneBasedStart))
            {
                if (pos < ex.OneBasedStart)
                {
                    // Nothing found?
                    if (last < 0)
                    {
                        Console.WriteLine("WARNING: Cannot find last exonic position before " + pos + " for transcript '" + ID + "'");
                        return(-1);
                    }
                    return(last);
                }
                else if (pos <= ex.OneBasedEnd)
                {
                    return(pos);
                }
                last = ex.OneBasedEnd;
            }

            if (last < 0)
            {
                Console.WriteLine("WARNING: Cannot find last exonic position before " + pos + " for transcript '" + ID + "'");
            }
            return(pos);
        }
예제 #2
0
        /// <summary>
        /// Create UTR regions for this transcript
        /// </summary>
        public List <UTR> CreateUTRs()
        {
            if (CodingDomainSequences.Count == 0)
            {
                return(UTRs);
            }

            List <Interval> missing = Exons.OfType <Interval>().ToList();

            foreach (Interval interval in UTRs.Concat(CodingDomainSequences.OfType <Interval>().ToList()))
            {
                missing = missing.SelectMany(i => i.Minus(interval)).ToList();
            }

            long codingMin = CodingDomainSequences.Select(c => c.OneBasedStart).Min();
            long codingMax = CodingDomainSequences.Select(c => c.OneBasedEnd).Max();

            foreach (Interval interval in missing)
            {
                Exon x = FindExon(interval);
                if (x == null)
                {
                    throw new ArgumentException("Cannot find exon for UTR: " + interval.ToString());
                }

                UTR toAdd = null;
                if (IsStrandPlus())
                {
                    if (interval.OneBasedEnd <= codingMin)
                    {
                        toAdd = new UTR5Prime(x, x.ChromosomeID, x.Source, x.Strand, interval.OneBasedStart, interval.OneBasedEnd);
                    }
                    else if (interval.OneBasedStart >= codingMax)
                    {
                        toAdd = new UTR3Prime(x, x.ChromosomeID, x.Source, x.Strand, interval.OneBasedStart, interval.OneBasedEnd);
                    }
                }
                else
                {
                    if (interval.OneBasedStart >= codingMax)
                    {
                        toAdd = new UTR5Prime(x, x.ChromosomeID, x.Source, x.Strand, interval.OneBasedStart, interval.OneBasedEnd);
                    }
                    else if (interval.OneBasedEnd <= codingMin)
                    {
                        toAdd = new UTR3Prime(x, x.ChromosomeID, x.Source, x.Strand, interval.OneBasedStart, interval.OneBasedEnd);
                    }
                }

                // OK?
                if (toAdd != null)
                {
                    UTRs.Add(toAdd);
                }
            }
            return(UTRs);
        }
예제 #3
0
        /// <summary>
        /// Find the first position after 'pos' within an exon
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        private long FirstExonPositionAfter(long pos)
        {
            foreach (Exon ex in Exons.OrderBy(x => x.OneBasedStart))
            {
                if (pos <= ex.OneBasedStart)
                {
                    return(ex.OneBasedStart);
                }
                if (pos <= ex.OneBasedEnd)
                {
                    return(pos);
                }
            }

            Console.WriteLine("WARNING: Cannot find first exonic position after " + pos + " for transcript '" + ID + "'");
            return(-1);
        }
예제 #4
0
        public List <MetadataListItem <List <string> > > GetFeatures()
        {
            var features     = new List <MetadataListItem <List <string> > >();
            var geneMetadata = GetGtfFeatureMetadata();

            features.Add(geneMetadata);
            List <Interval> exonsAndCds = Exons.OfType <Interval>().Concat(CodingDomainSequences).OrderBy(t => t.OneBasedStart).ToList(); // exons before cds; exons should come up first after stable sorting
            Exon            currentExon = null;

            foreach (Interval xc in exonsAndCds)
            {
                Exon x = xc as Exon;
                if (x != null)
                {
                    currentExon = x;
                    features.Add(x.GetGtfFeatureMetadata());
                }
                else
                {
                    features.Add(CDSFeatureMetadata(xc as CDS, currentExon));
                }
            }
            return(features);
        }
예제 #5
0
        /// <summary>
        /// Creates coding domains based on another annotated transcript
        /// </summary>
        /// <param name="withCDS"></param>
        /// <returns>true if this transcript was annotated; false if the transcript with CDS did not lead to an annotation</returns>
        public bool CreateCDSFromAnnotatedStartCodons(Transcript withCDS)
        {
            // Nothing to do if null input
            if (withCDS == null)
            {
                return(false);
            }

            // Figure out the start position
            CDS  firstCds        = withCDS.CdsSortedStrand.First();
            long cdsStartInChrom = IsStrandPlus() ? firstCds.OneBasedStart : firstCds.OneBasedEnd;
            long cdsStartInMrna  = BaseNumber2MRnaPos(cdsStartInChrom);

            if (cdsStartInMrna < 0)
            {
                return(false);
            }                                         // the coding start wasn't within any of the exons of this transcript

            // Figure out the stop codon from translation
            ISequence spliced         = SplicedRNA();
            ISequence translateThis   = spliced.GetSubSequence(cdsStartInMrna, spliced.Count - cdsStartInMrna);
            ISequence proteinSequence = Translation.OneFrameTranslation(translateThis, Gene.Chromosome.Mitochondrial);
            int       stopIdx         = proteinSequence.Select(x => x).ToList().IndexOf(Alphabets.Protein.Ter);

            if (stopIdx < 0)
            {
                return(false);
            }                                                                              // no stop codon in sight
            long endInMrna    = cdsStartInMrna + (stopIdx + 1) * GeneModel.CODON_SIZE - 1; // include the stop codon in CDS
            long lengthInMrna = endInMrna - cdsStartInMrna + 1;

            // Figure out the stop index on the chromosome
            long     utr5ishstart = IsStrandPlus() ? Exons.Min(x => x.OneBasedStart) : cdsStartInChrom + 1;
            long     utr5ishend   = IsStrandPlus() ? cdsStartInChrom - 1 : Exons.Max(x => x.OneBasedEnd);
            Interval utr5ish      = new Interval(null, "", Source, Strand, utr5ishstart, utr5ishend);
            var      intervals    = SortedStrand(Exons.SelectMany(x => x.Minus(utr5ish)).ToList());
            long     lengthSoFar  = 0;

            foreach (Interval y in intervals)
            {
                long lengthSum = lengthSoFar + y.Length();
                if (lengthSum <= lengthInMrna) // add this whole interval
                {
                    var toAdd = new CDS(this, ChromosomeID, Source, Strand, y.OneBasedStart, y.OneBasedEnd, 0);
                    CodingDomainSequences.Add(toAdd);
                    lengthSoFar += toAdd.Length();
                }
                else if (lengthSoFar < lengthInMrna) // chop off part of this interval
                {
                    long chopLength = lengthSum - lengthInMrna;
                    long start      = IsStrandPlus() ?
                                      y.OneBasedStart :
                                      y.OneBasedStart + chopLength;
                    long end = IsStrandPlus() ?
                               y.OneBasedEnd - chopLength :
                               y.OneBasedEnd;
                    var toAdd = new CDS(this, ChromosomeID, Source, Strand, start, end, 0);
                    CodingDomainSequences.Add(toAdd);
                    lengthSoFar += toAdd.Length();
                }
            }

            SetRegions(this);
            return(true);
        }
예제 #6
0
 /// <summary>
 /// Return an exon intersecting 'marker' (first exon found)
 /// </summary>
 /// <param name="marker"></param>
 /// <returns></returns>
 public Exon FindExon(Interval marker)
 {
     return(Exons.FirstOrDefault(x => x.Intersects(marker)));
 }
예제 #7
0
 /// <summary>
 /// Return the an exon that intersects 'pos'
 /// </summary>
 /// <param name="pos"></param>
 /// <returns></returns>
 public Exon FindExon(int pos)
 {
     return(Exons.FirstOrDefault(x => x.Includes(pos)));
 }