@@ -24,18 +24,20 @@ public class IncludeBedFile
2424 /// <summary>
2525 /// The IntervalTree from this bed file.
2626 /// </summary>
27- [ NotNull ] public readonly GenomeIntervalTree < IContigAndInterval > IntervalTree ;
27+ [ NotNull ]
28+ public GenomeIntervalTree < IContigAndInterval > IntervalTree => _intervalTree . Value ;
29+ [ NotNull ] private readonly Lazy < GenomeIntervalTree < IContigAndInterval > > _intervalTree ;
2830
2931 /// <summary>
3032 /// The bed file associated with this instance. If created from <see cref="CreateFromContigIntervals"/>, this will write out a bed file.
3133 /// </summary>
3234 [ NotNull ] public FileInfo BedFile => _fileSource . Value ;
3335 private readonly Lazy < FileInfo > _fileSource ;
3436
35- private IncludeBedFile ( [ NotNull ] GenomeIntervalTree < IContigAndInterval > tree ,
37+ private IncludeBedFile ( [ NotNull ] Lazy < GenomeIntervalTree < IContigAndInterval > > tree ,
3638 [ NotNull ] Lazy < FileInfo > fileSource )
3739 {
38- IntervalTree = tree ;
40+ _intervalTree = tree ;
3941 _fileSource = fileSource ;
4042 }
4143
@@ -56,7 +58,8 @@ public static IncludeBedFile CreateFromContigIntervals(
5658 {
5759 var tree = contigIntervals as GenomeIntervalTree < IContigAndInterval > ??
5860 CreateGenomeIntervalTree ( contigIntervals ) ;
59- return new IncludeBedFile ( tree , CreateBedFileLazy ( tree ) ) ;
61+ return new IncludeBedFile ( new Lazy < GenomeIntervalTree < IContigAndInterval > > ( tree ) ,
62+ CreateBedFileLazy ( tree ) ) ;
6063
6164 Lazy < FileInfo > CreateBedFileLazy (
6265 IEnumerable < IContigAndInterval > thisTree )
@@ -93,19 +96,29 @@ private static GenomeIntervalTree<IContigAndInterval> CreateGenomeIntervalTree(
9396 var listOrder = new List < IContigInfo > ( ) ;
9497 foreach ( var contigInterval in contigIntervals )
9598 {
96- if ( ! dictionary . TryGetValue ( contigInterval . Contig , out var tree ) )
99+ var contig = contigInterval . Contig ;
100+ if ( ! dictionary . TryGetValue ( contig , out var tree ) )
97101 {
98102 tree = MergedIntervalTree < uint > . Create ( null ) ;
99- listOrder . Add ( contigInterval . Contig ) ;
100- dictionary . Add ( contigInterval . Contig , tree ) ;
103+ listOrder . Add ( contig ) ;
104+ dictionary . Add ( contig , tree ) ;
101105 }
102106 tree . Add ( contigInterval ) ;
103107 }
104108
105109 var ret = GenomeIntervalTree < IContigAndInterval > . Create ( ) ;
106110 foreach ( var contig in listOrder )
111+ {
107112 ret . AddRange ( dictionary [ contig ]
108- . Select ( i => i as IContigAndInterval ?? ContigAndInterval . Create ( contig , i . Start , i . Stop ) ) ) ;
113+ . Select ( i => i as IContigAndInterval
114+ ?? ContigAndInterval . Create ( contig , i . Start , i . Stop ) ) ) ;
115+ var other = contig . ToUcscStyle ( ) ;
116+ if ( other . Name == contig . Name )
117+ other = contig . ToGrchStyle ( ) ;
118+ if ( other . Name != contig . Name )
119+ ret . AddRange ( dictionary [ contig ]
120+ . Select ( i => ContigAndInterval . Create ( other , i . Start , i . Stop ) ) ) ;
121+ }
109122
110123 return ret ;
111124 }
@@ -118,7 +131,10 @@ private static GenomeIntervalTree<IContigAndInterval> CreateGenomeIntervalTree(
118131 [ NotNull ]
119132 [ Pure ]
120133 public static IncludeBedFile CreateFromBedFile ( [ NotNull ] FileInfo bedFile )
121- => CreateFromBedReader ( BedReader . Create ( bedFile ) ) ;
134+ => bedFile . ExistsNow ( )
135+ ? CreateFromBedReader ( BedReader . Create ( bedFile ) )
136+ : TypeCache < string , IncludeBedFile > . GetOrAdd ( bedFile . FullName ,
137+ ( ) => CreateFromBedReader ( BedReader . Create ( bedFile ) ) ) ;
122138
123139 /// <summary>
124140 /// Creates a new instance of <see cref="IncludeBedFile"/> from a <see cref="BedReader"/>.
@@ -128,7 +144,8 @@ public static IncludeBedFile CreateFromBedFile([NotNull] FileInfo bedFile)
128144 [ Pure ]
129145 public static IncludeBedFile CreateFromBedReader ( [ NotNull ] BedReader bedReader )
130146 => TypeCache < string , IncludeBedFile > . GetOrAdd ( bedReader . FileSource . GetCompleteRealPath ( ) . FullName , ( ) =>
131- new IncludeBedFile ( CreateGenomeIntervalTree ( bedReader ) ,
147+ new IncludeBedFile ( new Lazy < GenomeIntervalTree < IContigAndInterval > > (
148+ ( ) => CreateGenomeIntervalTree ( bedReader ) ) ,
132149 new Lazy < FileInfo > ( ( ) => bedReader . FileSource ) ) ) ;
133150
134151 /// <inheritdoc/>
0 commit comments