Esempio n. 1
0
 /// <summary>
 /// Adds a link to the map if it passes the link filter criteria.
 /// </summary>
 /// <param name="tssMap">Tss map.</param>
 /// <param name="LocusMap">Locus map.</param>
 /// <param name="filter">Filter.</param>
 /// <param name="link">Link.</param>
 private static void AddLink(
     ref TssRegulatoryMap tssMap,
     ref LocusRegulatoryMap LocusMap,
     MapLinkFilter filter,
     MapLink link)
 {
     if (filter.IsValidLink(link.Strand, link.LinkLength, link.ConfidenceScore, link.Correlation))
     {
         filter.ApplyLinkFilterType(link, tssMap, LocusMap);
     }
 }
 public void AddReplaceLink(
     MapLink link,
     LocusRegulatoryMap map,
     Func <MapLink, LocusRegulatoryMap, bool> test)
 {
     this.EnsureKey(link, map);
     if (test(link, map))
     {
         map[link.LocusName].Clear();
         map[link.LocusName].Add(link.TranscriptName, link);
     }
 }
        public void EnsureKey(MapLink link, LocusRegulatoryMap LocusMap)
        {
            if (!LocusMap.ContainsKey(link.LocusName))
            {
                LocusMap.Add(link.LocusName, new Dictionary <MapLink.Tss, MapLink>());
            }

            if (LocusMap[link.LocusName].ContainsKey(link.TranscriptName))
            {
                throw new Exception(string.Format("Duplicate Locus-TSS link {0}-{1} in map", link.LocusName, link.TranscriptName));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Loads the map.
        /// </summary>
        /// <returns>The map.</returns>
        /// <param name="mapFileName">Map file name.</param>
        /// <param name="filter">Filter of map links.</param>
        public static TssRegulatoryMap LoadMap(
            string mapFileName,
            MapLinkFilter filter)
        {
            using (TextReader tr = new StreamReader(mapFileName))
            {
                string line = null;

                TssRegulatoryMap   tssMap   = new TssRegulatoryMap();
                LocusRegulatoryMap LocusMap = new LocusRegulatoryMap();

                while ((line = tr.ReadLine()) != null)
                {
                    var fields = line.Split('\t');


                    var transcriptName = fields[3];
                    var confidence     = double.Parse(fields[8]);
                    var correlation    = double.Parse(fields[7]);
                    var distance       = int.Parse(fields[9]);
                    var LocusName      = fields[6];
                    var strand         = fields[5];

                    var link = new MapLink
                    {
                        ConfidenceScore = confidence,
                        Correlation     = correlation,
                        LinkLength      = distance,
                        TranscriptName  = transcriptName,
                        TssName         = transcriptName,
                        LocusName       = LocusName,
                        Strand          = strand,
                        Chromosome      = fields[0],
                        TssPosition     = int.Parse(fields[1]),
                        HistoneName     = fields[10],
                        GeneName        = fields[11],
                    };

                    if ((filter.TranscriptSet == null || (filter.TranscriptSet != null && filter.TranscriptSet.Contains(transcriptName))) &&
                        (filter.LocusSet == null || (filter.LocusSet != null && filter.LocusSet.Contains(LocusName))))
                    {
                        AddLink(ref tssMap, ref LocusMap, filter, link);
                    }
                }

                return(filter.PostProcessLinkFilterType(tssMap, LocusMap));
            }
        }
 /// <summary>
 /// Determines whether this given link is closest gene the specified link map.
 /// </summary>
 /// <returns><c>true</c> if this instance is closest gene the specified link map; otherwise, <c>false</c>.</returns>
 /// <param name="link">Link.</param>
 /// <param name="map">Map.</param>
 private bool IsBestGene(MapLink link, LocusRegulatoryMap LocusMap)
 {
     return(!LocusMap[link.LocusName].Any(x => x.Value.ConfidenceScore < link.ConfidenceScore));
 }
 /// <summary>
 /// Determines whether this given link is closest gene the specified link map.
 /// </summary>
 /// <returns><c>true</c> if this instance is closest gene the specified link map; otherwise, <c>false</c>.</returns>
 /// <param name="link">Link.</param>
 /// <param name="map">Map.</param>
 public bool IsClosestGene(MapLink link, LocusRegulatoryMap LocusMap)
 {
     return(!LocusMap[link.LocusName].Any(x => x.Value.AbsLinkLength < link.AbsLinkLength));
 }
        public TssRegulatoryMap PostProcessLinkFilterType(TssRegulatoryMap tssMap, LocusRegulatoryMap LocusMap)
        {
            switch (this.LinkTypeFilter)
            {
            case MapLinkFilter.LinkType.NearestGene:
                return(LocusMap.Invert());

            case MapLinkFilter.LinkType.NearestBestGene:
                return(new TssRegulatoryMap(LocusMap.Links
                                            .ToLookup(x => x.TranscriptName, x => x)
                                            .Select(x => x.OrderBy(y => y.ConfidenceScore).First())));

            case MapLinkFilter.LinkType.NearestLocusOfNearestGene:
                return(new TssRegulatoryMap(LocusMap.Links
                                            .ToLookup(x => x.TranscriptName, x => x)
                                            .Select(x => x.OrderBy(y => y.AbsLinkLength).First())));

            case MapLinkFilter.LinkType.BestGeneLink:
                return(LocusMap.Invert());

            case MapLinkFilter.LinkType.NearestLocusOrGene:
                return(new TssRegulatoryMap(tssMap.Links.Concat(LocusMap.Links)));

            case MapLinkFilter.LinkType.NearestLocusAndGene:
                return(new TssRegulatoryMap(tssMap.Links.Where(LocusMap.Links.Contains)));

            case MapLinkFilter.LinkType.BestLocusNearestGene:
                return(new TssRegulatoryMap(tssMap.Links.Where(LocusMap.Links.Contains)));

            case MapLinkFilter.LinkType.BestLocusBestGene:
                return(new TssRegulatoryMap(tssMap.Links.Where(LocusMap.Links.Contains)));

            case MapLinkFilter.LinkType.NotNearestGene:
                return(new TssRegulatoryMap(tssMap.Links.Where(link => LocusMap.Links.Contains(link))));

            case MapLinkFilter.LinkType.NotNearestLocusOrGene:
                var Locus2nnmap = LocusMap.GetNearestNeighborMap(5);
                return(new TssRegulatoryMap(Locus2nnmap.Links.Where(link => !this.IsClosestLocus(link, tssMap) && !this.IsClosestGene(link, LocusMap))));

            case MapLinkFilter.LinkType.BestLocusNotNearestLocusOrGene:
                return(new TssRegulatoryMap(LocusMap.Links
                                            .Where(link => !this.IsClosestLocus(link, tssMap) &&
                                                   !this.IsClosestGene(link, LocusMap) &&
                                                   this.auxiliaryMap.Links.Contains(link))));

            case MapLinkFilter.LinkType.BestDistalLocus:
            {
                var distalLinks = tssMap.Links
                                  .Where(link => !this.IsClosestLocus(link, this.auxiliaryMap) &&
                                         !this.IsClosestGene(link, LocusMap));

                TssRegulatoryMap bestDistalMap = new TssRegulatoryMap();
                foreach (var link in distalLinks)
                {
                    this.EnsureKey(link, bestDistalMap);
                    this.AddReplaceLink(link, bestDistalMap, IsBestLocus);
                }

                return(bestDistalMap);
            }

            default:
                return(tssMap);
            }
        }
        /// <summary>
        /// Determines whether this instance is valid link type the specified link map.
        /// </summary>
        /// <returns><c>true</c> if this instance is valid link type the specified link map; otherwise, <c>false</c>.</returns>
        /// <param name="link">Link.</param>
        /// <param name="map">Map.</param>
        public void ApplyLinkFilterType(MapLink link, TssRegulatoryMap tssMap, LocusRegulatoryMap LocusMap)
        {
            this.EnsureKey(link, tssMap);

            if ((this.LinkTypeFilter == LinkType.BestLocusNotNearestLocusOrGene ||
                 this.LinkTypeFilter == LinkType.BestDistalLocus) &&
                this.auxiliaryMap == null)
            {
                this.auxiliaryMap = new TssRegulatoryMap();
            }

            switch (this.LinkTypeFilter)
            {
            case MapLinkFilter.LinkType.Any:
                tssMap[link.TranscriptName].Add(link.LocusName, link);
                break;

            case MapLinkFilter.LinkType.NearestLocus:
                this.AddReplaceLink(link, tssMap, this.IsClosestLocus);
                break;

            case MapLinkFilter.LinkType.NearestGene:
            case MapLinkFilter.LinkType.NearestLocusOfNearestGene:
                this.AddReplaceLink(link, LocusMap, this.IsClosestGene);
                break;

            case MapLinkFilter.LinkType.NearestBestGene:
                this.AddReplaceLink(link, LocusMap, this.IsClosestGene);
                break;

            case MapLinkFilter.LinkType.BestLocusNearestGene:
                this.AddReplaceLink(link, tssMap, this.IsBestLocus);
                this.AddReplaceLink(link, LocusMap, this.IsClosestGene);
                break;

            case MapLinkFilter.LinkType.BestLocusBestGene:
                this.AddReplaceLink(link, tssMap, this.IsBestLocus);
                this.AddReplaceLink(link, LocusMap, this.IsBestGene);
                break;

            case MapLinkFilter.LinkType.NearestLocusOrGene:
                this.AddReplaceLink(link, tssMap, this.IsClosestLocus);
                this.AddReplaceLink(link, LocusMap, this.IsClosestGene);
                break;

            case MapLinkFilter.LinkType.NearestLocusAndGene:
                this.AddReplaceLink(link, tssMap, this.IsClosestLocus);
                this.AddReplaceLink(link, LocusMap, this.IsClosestGene);
                break;

            case MapLinkFilter.LinkType.BestLocusLink:
                this.AddReplaceLink(link, tssMap, this.IsBestLocus);
                break;

            case MapLinkFilter.LinkType.WorstLocusLink:
                this.AddReplaceLink(link, tssMap, this.IsWorstLocus);
                break;

            case MapLinkFilter.LinkType.BestGeneLink:
                this.AddReplaceLink(link, LocusMap, this.IsBestGene);
                break;

            case MapLinkFilter.LinkType.NotNearestLocus:
                break;

            case MapLinkFilter.LinkType.NotNearestGene:
                tssMap[link.TranscriptName].Add(link.LocusName, link);
                this.AddReplaceLink(link, LocusMap, this.IsClosestGene);
                break;

            case MapLinkFilter.LinkType.NotNearestLocusOrGene:
                tssMap[link.TranscriptName].Add(link.LocusName, link);
                this.EnsureKey(link, LocusMap);
                LocusMap[link.LocusName].Add(link.TranscriptName, link);
                break;

            case MapLinkFilter.LinkType.BestLocusNotNearestLocusOrGene:
                tssMap[link.TranscriptName].Add(link.LocusName, link);
                this.EnsureKey(link, LocusMap);
                LocusMap[link.LocusName].Add(link.TranscriptName, link);
                this.EnsureKey(link, this.auxiliaryMap);
                this.AddReplaceLink(link, this.auxiliaryMap, this.IsBestLocus);
                break;

            case MapLinkFilter.LinkType.BestDistalLocus:
                tssMap[link.TranscriptName].Add(link.LocusName, link);
                this.EnsureKey(link, LocusMap);
                this.AddReplaceLink(link, LocusMap, this.IsClosestGene);
                this.EnsureKey(link, this.auxiliaryMap);
                this.AddReplaceLink(link, this.auxiliaryMap, this.IsClosestLocus);
                break;
            }
        }