Exemplo n.º 1
0
 public void Link(MapLinkFilter.LinkType type)
 {
     this.Map = TssRegulatoryMap.LoadMap(this.MapFileName, new MapLinkFilter {
         LinkTypeFilter = type
     });
     NullMapBuilder.WriteMap(this.Map, this.ExpressionData.Genes, this.HistoneName, this.OutputFile);
 }
Exemplo n.º 2
0
        public void BestWorst(double fraction)
        {
            this.Map = TssRegulatoryMap.LoadMap(this.MapFileName, new MapLinkFilter {
            });

            var bestLinkMap = this.Map.GetBestNeighborMap(1);

            var orderedLinks = bestLinkMap.Links.OrderBy(x => x.ConfidenceScore).ToList();

            int linkCount = (int)(orderedLinks.Count * fraction);

            var topLinks = orderedLinks
                           .Take(linkCount)
                           .OrderBy(x => x.AbsLinkLength)
                           .ToList();

            var bottomLinks = orderedLinks
                              .OrderBy(x => - x.ConfidenceScore)
                              .Take(orderedLinks.Count - linkCount)
                              .ToList();

            List <MapLink> bestLinks  = new List <MapLink>();
            List <MapLink> worstLinks = new List <MapLink>();

            foreach (var link in topLinks)
            {
                int linkIndex = int.MaxValue;
                for (int i = 0; i < bottomLinks.Count; i++)
                {
                    var bottomLink = bottomLinks[i];
                    if (Math.Sign(link.LinkLength) == Math.Sign(bottomLink.LinkLength) &&
                        link.AbsLinkLength < bottomLink.AbsLinkLength * 1.01 &&
                        link.AbsLinkLength > bottomLink.AbsLinkLength * 0.99)
                    {
                        linkIndex = i;
                        break;
                    }
                }

                if (linkIndex != int.MaxValue)
                {
                    bestLinks.Add(link);
                    worstLinks.Add(bottomLinks[linkIndex]);
                    bottomLinks.RemoveAt(linkIndex);
                }
            }

            var bottomMapFile = this.OutputFile.Replace(".bed", ".bottom.bed");

            NullMapBuilder.WriteMap(new TssRegulatoryMap(bestLinks), this.ExpressionData.Genes, this.HistoneName, this.OutputFile);
            NullMapBuilder.WriteMap(new TssRegulatoryMap(worstLinks), this.ExpressionData.Genes, this.HistoneName, bottomMapFile);
        }
        public void Convert()
        {
            var contacts = Helpers.GetFileDataLines(this.ContactFileName, true).Select(line =>
            {
                var fields = line.Split('\t');
                var pair   = fields[6];
                var ids    = pair.Split('_');

                return(new
                {
                    Pair = pair,
                    Id1 = ids[0],
                    Chr1 = fields[0],
                    Start1 = int.Parse(fields[1]),
                    End1 = int.Parse(fields[2]),
                    Id2 = ids[1],
                    Chr2 = fields[3],
                    Start2 = int.Parse(fields[4]),
                    End2 = int.Parse(fields[5]),
                    Score = double.Parse(fields[7]),
                });
            }).ToList();

            var locations = contacts.Select(x => new Location[]
            {
                new Location
                {
                    Name = x.Id1, Chromosome = x.Chr1, Start = x.Start1, End = x.End1, AlternateName = x.Id2, Score = x.Score
                },
                new Location
                {
                    Name = x.Id2, Chromosome = x.Chr2, Start = x.Start2, End = x.End2, AlternateName = x.Id1, Score = x.Score
                },
            }).SelectMany(x => x)
                            .ToList();

            var uniqueLocations = locations
                                  .ToLookup(x => x.Name)
                                  .ToDictionary(x => x.Key, x => x.First());

            var contactMap = locations
                             .ToLookup(x => x.Name, x => x)
                             .ToDictionary(x => x.Key, x => x
                                           .ToDictionary(y => y.AlternateName, y => uniqueLocations[y.AlternateName]));

            var indexedLocations = new BedFile(uniqueLocations);

            var LocusFile     = new BedFile(this.LocusFileName, BedFile.Bed3Layout);
            var annotationSet = new GtfExpressionFile(GtfExpressionFile.ExpressionType.Cage, this.AnnotationFileName);

            var LocusOverlaps = indexedLocations.Locations.ToDictionary(
                x => x.Key,
                x => TRFScorer.GetOverlaps(
                    x.Value,
                    LocusFile.ChromosomeIndexedLocations,
                    BedFile.IndexSize,
                    LocusFile.MaxLocationSize[x.Value.Chromosome]));

            var tssOverlaps = indexedLocations.Locations.ToDictionary(
                x => x.Key,
                x => TRFScorer.GetOverlaps(
                    x.Value,
                    annotationSet.ChromosomeIndexedLocations,
                    BedFile.IndexSize,
                    annotationSet.MaxLocationSize[x.Value.Chromosome],
                    Location.OverlapsDirectionalStart));

            var links = contactMap
                        .Where(x => tssOverlaps[x.Key] != null)
                        .ToDictionary(x => x.Key, x => x.Value.Where(y => LocusOverlaps[y.Key] != null))
                        .Where(x => x.Value.Any())
                        .Select(x => tssOverlaps[x.Key].Select(y => new {
                Id1       = x.Key,
                Tss       = y.Name,
                LocusList = x.Value
                            .Select(z => LocusOverlaps[z.Key]
                                    .Select(a => new { Name = a.Name, Id2 = z.Key }))
                            .SelectMany(z => z).ToList()
            })
                                .Select(y => y.LocusList.Select(z =>
            {
                var LocusLocation = LocusFile.Locations[z.Name];
                var tssStart      = annotationSet.Transcripts[y.Tss].DirectionalStart;
                var linkLength    = LocusLocation.End < tssStart ? LocusLocation.End - tssStart : LocusLocation.Start - tssStart;
                return(new MapLink
                {
                    TranscriptName = y.Tss,
                    TssName = y.Tss,
                    LocusName = z.Name,
                    ConfidenceScore = contactMap[y.Id1][z.Id2].Score,
                    LinkLength = linkLength,
                });
            }))
                                .SelectMany(y => y))
                        .SelectMany(x => x)
                        .ToList();

            var convertedMap = new TssRegulatoryMap(links);

            NullMapBuilder.WriteMap(convertedMap, annotationSet.Locations, "Contact", this.MapFileName);
        }