@@ -10,12 +10,14 @@ import is.hail.expr.ir.{
1010import is .hail .expr .ir .IRParser .parseType
1111import is .hail .expr .ir .defs .{EncodedLiteral , GetFieldByIdx }
1212import is .hail .expr .ir .functions .IRFunctionRegistry
13+ import is .hail .io .reference .{IndexedFastaSequenceFile , LiftOver }
1314import is .hail .linalg .RowMatrix
1415import is .hail .types .physical .PStruct
1516import is .hail .types .virtual .{TArray , TInterval }
1617import is .hail .utils .{defaultJSONFormats , log , toRichIterable , FastSeq , HailException , Interval }
1718import is .hail .variant .ReferenceGenome
1819
20+ import scala .collection .mutable
1921import scala .jdk .CollectionConverters .{
2022 asScalaBufferConverter , mapAsScalaMapConverter , seqAsJavaListConverter ,
2123}
@@ -29,7 +31,10 @@ import org.json4s.Formats
2931import org .json4s .jackson .{JsonMethods , Serialization }
3032import sourcecode .Enclosing
3133
32- trait Py4JBackendExtensions { this : Backend =>
34+ trait Py4JBackendExtensions {
35+ def backend : Backend
36+ def references : mutable.Map [String , ReferenceGenome ]
37+ def persistedIR : mutable.Map [Int , BaseIR ]
3338 def flags : HailFeatureFlags
3439 def longLifeTempFileManager : TempFileManager
3540
@@ -59,7 +64,9 @@ trait Py4JBackendExtensions { this: Backend =>
5964 persistedIR.remove(id)
6065
6166 def pyAddSequence (name : String , fastaFile : String , indexFile : String ): Unit =
62- withExecuteContext(ctx => references(name).addSequence(ctx, fastaFile, indexFile))
67+ backend.withExecuteContext { ctx =>
68+ references(name).addSequence(IndexedFastaSequenceFile (ctx.fs, fastaFile, indexFile))
69+ }
6370
6471 def pyRemoveSequence (name : String ): Unit =
6572 references(name).removeSequence()
@@ -74,7 +81,7 @@ trait Py4JBackendExtensions { this: Backend =>
7481 partitionSize : java.lang.Integer ,
7582 entries : String ,
7683 ): Unit =
77- withExecuteContext { ctx =>
84+ backend. withExecuteContext { ctx =>
7885 val rm = RowMatrix .readBlockMatrix(ctx.fs, pathIn, partitionSize)
7986 entries match {
8087 case " full" =>
@@ -112,7 +119,7 @@ trait Py4JBackendExtensions { this: Backend =>
112119 returnType : String ,
113120 bodyStr : String ,
114121 ): Unit = {
115- withExecuteContext { ctx =>
122+ backend. withExecuteContext { ctx =>
116123 IRFunctionRegistry .registerIR(
117124 ctx,
118125 name,
@@ -126,10 +133,10 @@ trait Py4JBackendExtensions { this: Backend =>
126133 }
127134
128135 def pyExecuteLiteral (irStr : String ): Int =
129- withExecuteContext { ctx =>
136+ backend. withExecuteContext { ctx =>
130137 val ir = IRParser .parse_value_ir(irStr, IRParserEnvironment (ctx, persistedIR.toMap))
131138 assert(ir.typ.isRealizable)
132- execute(ctx, ir) match {
139+ backend. execute(ctx, ir) match {
133140 case Left (_) => throw new HailException (" Can't create literal" )
134141 case Right ((pt, addr)) =>
135142 val field = GetFieldByIdx (EncodedLiteral .fromPTypeAndAddress(pt, addr, ctx), 0 )
@@ -158,13 +165,13 @@ trait Py4JBackendExtensions { this: Backend =>
158165 }
159166
160167 def pyToDF (s : String ): DataFrame =
161- withExecuteContext { ctx =>
168+ backend. withExecuteContext { ctx =>
162169 val tir = IRParser .parse_table_ir(s, IRParserEnvironment (ctx, irMap = persistedIR.toMap))
163170 Interpret (tir, ctx).toDF()
164171 }
165172
166173 def pyReadMultipleMatrixTables (jsonQuery : String ): util.List [MatrixIR ] =
167- withExecuteContext { ctx =>
174+ backend. withExecuteContext { ctx =>
168175 log.info(" pyReadMultipleMatrixTables: got query" )
169176 val kvs = JsonMethods .parse(jsonQuery) match {
170177 case json4s.JObject (values) => values.toMap
@@ -193,19 +200,24 @@ trait Py4JBackendExtensions { this: Backend =>
193200 addReference(ReferenceGenome .fromJSON(jsonConfig))
194201
195202 def pyRemoveReference (name : String ): Unit =
196- references.remove (name)
203+ removeReference (name)
197204
198205 def pyAddLiftover (name : String , chainFile : String , destRGName : String ): Unit =
199- withExecuteContext(ctx => references(name).addLiftover(ctx, chainFile, destRGName))
206+ backend.withExecuteContext { ctx =>
207+ references(name).addLiftover(references(destRGName), LiftOver (ctx.fs, chainFile))
208+ }
200209
201210 def pyRemoveLiftover (name : String , destRGName : String ): Unit =
202211 references(name).removeLiftover(destRGName)
203212
204213 private [this ] def addReference (rg : ReferenceGenome ): Unit =
205214 ReferenceGenome .addFatalOnCollision(references, FastSeq (rg))
206215
216+ private [this ] def removeReference (name : String ): Unit =
217+ references -= name
218+
207219 def parse_value_ir (s : String , refMap : java.util.Map [String , String ]): IR =
208- withExecuteContext { ctx =>
220+ backend. withExecuteContext { ctx =>
209221 IRParser .parse_value_ir(
210222 s,
211223 IRParserEnvironment (ctx, irMap = persistedIR.toMap),
@@ -231,7 +243,7 @@ trait Py4JBackendExtensions { this: Backend =>
231243 }
232244
233245 def loadReferencesFromDataset (path : String ): Array [Byte ] =
234- withExecuteContext { ctx =>
246+ backend. withExecuteContext { ctx =>
235247 val rgs = ReferenceGenome .fromHailDataset(ctx.fs, path)
236248 ReferenceGenome .addFatalOnCollision(references, rgs)
237249
@@ -245,7 +257,7 @@ trait Py4JBackendExtensions { this: Backend =>
245257 f : ExecuteContext => T
246258 )(implicit E : Enclosing
247259 ): T =
248- withExecuteContext { ctx =>
260+ backend. withExecuteContext { ctx =>
249261 val tempFileManager = longLifeTempFileManager
250262 if (selfContainedExecution && tempFileManager != null ) f(ctx)
251263 else ctx.local(tempFileManager = NonOwningTempFileManager (tempFileManager))(f)
0 commit comments