//-------------------------------------------------------------------------------- // // Copyright © The University of Queensland, 2012-2014. All rights reserved. // // License: //-------------------------------------------------------------------------------- namespace Genomics { using System; using System.Collections.Generic; using System.IO; using System.Linq; using Shared; using Tools; using System.Text.RegularExpressions; /// /// A single link in a regulatory mmap /// public class MapLink { private Location locusLocation; private Location transcriptLocation; /// /// Initializes a new instance of the class. /// public MapLink() { } /// /// Gets or sets the confidence score. /// /// The confidence score. public double ConfidenceScore { get; set; } /// /// Gets or sets the correlation. /// /// The correlation. public double Correlation { get; set; } /// /// Gets or sets the length of the link. /// /// The length of the link. public int LinkLength { get; set; } /// /// Gets or sets the name of the transcript. /// /// The name of the transcript. public Tss TranscriptName { get; set; } /// /// Gets or sets the chromosome. /// /// The chromosome. public string Chromosome { get; set; } /// /// Gets or sets the tss position. /// /// The tss position. public int TssPosition { get; set; } /// /// Gets the tss location. /// /// The tss location. public Location TssLocation { get { return Helpers.CheckInit( ref this.transcriptLocation, () => new Location { Chromosome = this.Chromosome, Name = this.TranscriptName, AlternateName = this.TssName, Start = this.TssPosition, End = this.TssPosition, Strand = this.Strand, }); } } /// /// Gets or sets the size of the locus. /// /// The size of the locus. public int LocusSize { get { return this.LocusEnd - this.LocusStart; } } /// /// Gets the locus start. /// /// The locus start. public int LocusStart { get { return this.LocusName.Start; } } /// /// Gets or sets the locus end. /// /// The locus end. public int LocusEnd { get { return this.LocusName.End; } } /// /// Gets or sets the histone namw. /// /// The histone namw. public string HistoneName { get; set; } /// /// Gets or sets the name of the gene. /// /// The name of the gene. public string GeneName { get; set; } /// /// Gets or sets the name of the tss. /// /// The name of the tss. public string TssName { get; set; } /// /// Gets or sets the name of the locus. /// /// The name of the locus. public Locus LocusName { get; set; } /// /// Gets or sets the strand of the target gene/transcript/tss. /// /// The strand. public string Strand { get; set; } /// /// Gets the locus position. /// /// The locus position. public int LocusPosition { get { if (this.Strand == "+") { return this.LinkLength; } else if (this.Strand == "-") { return -this.LinkLength; } else { throw new Exception("Missing strand in link data"); } } } public Location LocusLocation { get { return Helpers.CheckInit( ref this.locusLocation, () => new Location { Chromosome = this.LocusName.Chr, Name = this.LocusName, Start = this.LocusStart, End = this.LocusEnd, Strand = this.Strand, }); } } /// /// Gets the length of the abs link. /// /// The length of the abs link. public int AbsLinkLength { get { return Math.Abs(this.LinkLength); } } /// /// Serves as a hash function for a object. /// /// A hash code for this instance that is suitable for use in hashing algorithms and data structures such as /// a hash table. public override int GetHashCode() { return (this.TssName + "_" + this.LocusName).GetHashCode(); } /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// true if the specified is equal to the current /// ; otherwise, false. public override bool Equals(object obj) { if (!(obj is MapLink)) { return false; } var other = (MapLink)obj; return this.TssName == other.TssName && this.LocusName == other.LocusName; } public static bool operator ==(MapLink l1, MapLink l2) { return l1.Equals(l2); } public static bool operator !=(MapLink l1, MapLink l2) { return !l1.Equals(l2); } /// /// Test if two links have the same transcript and locus. /// public class SameTranscriptAndLocus : IEqualityComparer { /// /// Equals the specified a and b. /// /// true if the link has the same trancript and locus endpoints /// Link a. /// Link b. public bool Equals(MapLink a, MapLink b) { return a.Equals(b); } /// The object for which the hash code is to be returned. /// Returns a hash code for the specified object. /// A hash code for the specified object. /// /// Gets the hash code. /// /// The alpha component. public int GetHashCode(MapLink a) { return a.GetHashCode(); } } public class Locus : EqualityComparer { private readonly string locusName; private string chr; private int start = -1; private int end = -1; private Regex nameFormat = new Regex("chr..?:[0-9]+-[0-9]+"); public Locus(string s) { this.locusName = s; } public static implicit operator string(Locus s) { return s.locusName; } public static implicit operator Locus(string s) { return new Locus(s); } public override bool Equals(object obj) { return obj is Locus && ((Locus)obj).locusName == this.locusName; } public override int GetHashCode() { return this.locusName.GetHashCode(); } public override string ToString() { return this.locusName; } public override bool Equals(Locus x, Locus y) { return x.Equals(y); } public override int GetHashCode(Locus obj) { return obj.GetHashCode(); } public static bool operator ==(Locus c1, Locus c2) { return c1.locusName.Equals(c2.locusName); } public static bool operator !=(Locus c1, Locus c2) { return !c1.locusName.Equals(c2.locusName); } public string Chr { get { return Helpers.CheckInit( ref this.chr, () => this.locusName.Split(':')[0] ); } } public int Start { get { if (this.start == -1) { this.start = int.Parse(this.locusName.Split(':')[1].Split('-')[0]); } return this.start; } } public int End { get { if (this.end == -1) { this.end = int.Parse(this.locusName.Split(':')[1].Split('-')[1]); } return this.end; } } } public class Tss : EqualityComparer { private readonly string tssName; public Tss(string s) { this.tssName = s; } public static implicit operator string(Tss s) { return s.tssName; } public static implicit operator Tss(string s) { return new Tss(s); } public override bool Equals(object obj) { return obj is Tss && ((Tss)obj).tssName == this.tssName; } public override int GetHashCode() { return this.tssName.GetHashCode(); } public override string ToString() { return this.tssName; } public override bool Equals(Tss x, Tss y) { return x.Equals(y); } public override int GetHashCode(Tss obj) { return obj.GetHashCode(); } public static bool operator ==(Tss t1, Tss t2) { return t1.tssName.Equals(t2.tssName); } public static bool operator !=(Tss t1, Tss t2) { return !t1.tssName.Equals(t2.tssName); } } } }