public void AddReplaceLink(
     MapLink link,
     TssRegulatoryMap map,
     Func <MapLink, TssRegulatoryMap, bool> test)
 {
     if (test(link, map))
     {
         map[link.TranscriptName].Clear();
         map[link.TranscriptName].Add(link.LocusName, link);
     }
 }
Пример #2
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 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));
            }
        }
Пример #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));
 }
 /// <summary>
 /// Determines whether this instance is worst Locus the specified link tssMap.
 /// </summary>
 /// <returns><c>true</c> if this instance is worst Locus the specified link tssMap; otherwise, <c>false</c>.</returns>
 /// <param name="link">Link.</param>
 /// <param name="tssMap">Tss map.</param>
 private bool IsWorstLocus(MapLink link, TssRegulatoryMap tssMap)
 {
     return(!tssMap[link.TranscriptName].Any(x => x.Value.ConfidenceScore > link.ConfidenceScore));
 }
 /// <summary>
 /// Determines whether this given link is closest link the specified link map.
 /// </summary>
 /// <returns><c>true</c> if this instance is closest link the specified link map; otherwise, <c>false</c>.</returns>
 /// <param name="link">Link.</param>
 /// <param name="map">Map.</param>
 public bool IsClosestLocus(MapLink link, TssRegulatoryMap tssMap)
 {
     return(!tssMap[link.TranscriptName].Any(x => x.Value.AbsLinkLength < link.AbsLinkLength));
 }
        /// <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;
            }
        }