Skip to content

Commit ce7228b

Browse files
committed
Add write typed functions
1 parent 4dda1b5 commit ce7228b

File tree

5 files changed

+285
-136
lines changed

5 files changed

+285
-136
lines changed

api/api/api.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"bytes"
45
"context"
56
"embed"
67
"encoding/json"
@@ -17,6 +18,9 @@ import (
1718
luajson "github.com/koeng101/dnadesign/api/api/json"
1819
"github.com/koeng101/dnadesign/api/gen"
1920
"github.com/koeng101/dnadesign/lib/bio"
21+
"github.com/koeng101/dnadesign/lib/bio/fasta"
22+
"github.com/koeng101/dnadesign/lib/bio/fastq"
23+
"github.com/koeng101/dnadesign/lib/bio/slow5"
2024
"github.com/koeng101/dnadesign/lib/primers/pcr"
2125
"github.com/koeng101/dnadesign/lib/synthesis/codon"
2226
"github.com/koeng101/dnadesign/lib/synthesis/fix"
@@ -209,16 +213,24 @@ func (app *App) ExecuteLua(data string, attachments []gen.Attachment) (string, s
209213
L.SetGlobal("fasta_parse", L.NewFunction(app.LuaIoFastaParse))
210214
L.SetGlobal("genbank_parse", L.NewFunction(app.LuaIoGenbankParse))
211215

212-
// Add CDS design functions
216+
// Add CDS functions
213217
L.SetGlobal("fix", L.NewFunction(app.LuaCdsFix))
214218
L.SetGlobal("optimize", L.NewFunction(app.LuaCdsOptimize))
215219
L.SetGlobal("translate", L.NewFunction(app.LuaCdsTranslate))
216220

217-
// Add simulate functions
218-
L.SetGlobal("fragment", L.NewFunction(app.LuaCloningFragment))
221+
// Add PCR functions
219222
L.SetGlobal("complex_pcr", L.NewFunction(app.LuaPcrComplexPcr))
220223
L.SetGlobal("pcr", L.NewFunction(app.LuaPcrSimplePcr))
221224

225+
// Add Cloning functions
226+
L.SetGlobal("fragment", L.NewFunction(app.LuaCloningFragment))
227+
228+
// Add Folding functions
229+
// Add Seqhash functions
230+
// Add CodonTable functions
231+
// Add Utils functions
232+
// Add Random functions
233+
222234
// Execute the Lua script
223235
if err := L.DoString(data); err != nil {
224236
return "", "", err
@@ -315,7 +327,12 @@ func (app *App) LuaIoFastaParse(L *lua.LState) int {
315327
}
316328

317329
func (app *App) PostIoFastaWrite(ctx context.Context, request gen.PostIoFastaWriteRequestObject) (gen.PostIoFastaWriteResponseObject, error) {
318-
return nil, nil
330+
var w bytes.Buffer
331+
for _, fastaRecord := range *request.Body {
332+
fastaStruct := fasta.Record(fastaRecord)
333+
_, _ = fastaStruct.WriteTo(&w) // other than memory problems, there are no circumstances where bytes.Buffer errors
334+
}
335+
return gen.PostIoFastaWrite200TextResponse(w.String()), nil
319336
}
320337
func (app *App) LuaIoFastaWrite(L *lua.LState) int { return 0 }
321338

@@ -357,7 +374,12 @@ func (app *App) PostIoFastqParse(ctx context.Context, request gen.PostIoFastqPar
357374
func (app *App) LuaIoFastqParse(L *lua.LState) int { return 0 }
358375

359376
func (app *App) PostIoFastqWrite(ctx context.Context, request gen.PostIoFastqWriteRequestObject) (gen.PostIoFastqWriteResponseObject, error) {
360-
return nil, nil
377+
var w bytes.Buffer
378+
for _, fastqRead := range *request.Body {
379+
fastqStruct := fastq.Read{Identifier: fastqRead.Identifier, Sequence: fastqRead.Sequence, Quality: fastqRead.Quality, Optionals: *fastqRead.Optionals}
380+
_, _ = fastqStruct.WriteTo(&w) // other than memory problems, there are no circumstances where bytes.Buffer errors
381+
}
382+
return gen.PostIoFastqWrite200TextResponse(w.String()), nil
361383
}
362384
func (app *App) LuaIoFastqWrite(L *lua.LState) int { return 0 }
363385

@@ -367,7 +389,20 @@ func (app *App) PostIoSlow5Parse(ctx context.Context, request gen.PostIoSlow5Par
367389
func (app *App) LuaIoSlow5Parse(L *lua.LState) int { return 0 }
368390

369391
func (app *App) PostIoSlow5Write(ctx context.Context, request gen.PostIoSlow5WriteRequestObject) (gen.PostIoSlow5WriteResponseObject, error) {
370-
return nil, nil
392+
var w bytes.Buffer
393+
var headerValues []slow5.HeaderValue
394+
requestHeaderValues := request.Body.Header.HeaderValues
395+
for _, headerValue := range requestHeaderValues {
396+
headerValues = append(headerValues, slow5.HeaderValue{ReadGroupID: uint32(headerValue.ReadGroupID), Slow5Version: headerValue.Slow5Version, EndReasonHeaderMap: headerValue.EndReasonHeaderMap})
397+
}
398+
header := slow5.Header{HeaderValues: headerValues}
399+
reads := request.Body.Reads
400+
_, _ = header.WriteTo(&w)
401+
for _, read := range *reads {
402+
slow5Struct := ConvertSlow5ReadToRead(read)
403+
_, _ = slow5Struct.WriteTo(&w)
404+
}
405+
return gen.PostIoSlow5Write200TextResponse(w.String()), nil
371406
}
372407
func (app *App) LuaIoSlow5Write(L *lua.LState) int { return 0 }
373408

@@ -387,7 +422,12 @@ func (app *App) PostIoPileupParse(ctx context.Context, request gen.PostIoPileupP
387422
func (app *App) LuaIoPileupParse(L *lua.LState) int { return 0 }
388423

389424
func (app *App) PostIoPileupWrite(ctx context.Context, request gen.PostIoPileupWriteRequestObject) (gen.PostIoPileupWriteResponseObject, error) {
390-
return nil, nil
425+
var w bytes.Buffer
426+
for _, pileupLine := range *request.Body {
427+
pileupStruct := ConvertGenPileupLineToPileupLine(pileupLine)
428+
_, _ = pileupStruct.WriteTo(&w) // other than memory problems, there are no circumstances where bytes.Buffer errors
429+
}
430+
return gen.PostIoPileupWrite200TextResponse(w.String()), nil
391431
}
392432
func (app *App) LuaIoPileupWrite(L *lua.LState) int { return 0 }
393433

api/api/converters.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,94 @@ package api
33
import (
44
"github.com/koeng101/dnadesign/api/gen"
55
"github.com/koeng101/dnadesign/lib/bio/genbank"
6+
"github.com/koeng101/dnadesign/lib/bio/pileup"
7+
"github.com/koeng101/dnadesign/lib/bio/slow5"
68
)
79

10+
func ConvertPileupLineToGenPileupLine(pileupLine pileup.Line) gen.PileupLine {
11+
return gen.PileupLine{
12+
Position: int(pileupLine.Position),
13+
Quality: pileupLine.Quality,
14+
ReadCount: int(pileupLine.ReadCount),
15+
ReadResults: pileupLine.ReadResults,
16+
ReferenceBase: pileupLine.ReferenceBase,
17+
Sequence: pileupLine.Sequence,
18+
}
19+
}
20+
21+
func ConvertGenPileupLineToPileupLine(genPileupLine gen.PileupLine) pileup.Line {
22+
return pileup.Line{
23+
Sequence: genPileupLine.Sequence,
24+
Position: uint(genPileupLine.Position),
25+
ReferenceBase: genPileupLine.ReferenceBase,
26+
ReadCount: uint(genPileupLine.ReadCount),
27+
ReadResults: genPileupLine.ReadResults,
28+
Quality: genPileupLine.Quality,
29+
}
30+
}
31+
32+
//nolint:dupl
33+
func ConvertSlow5ReadToRead(slow5Read gen.Slow5Read) slow5.Read {
34+
var read slow5.Read
35+
36+
// Convert and assign each field
37+
read.ReadID = slow5Read.ReadID
38+
read.ReadGroupID = uint32(slow5Read.ReadGroupID)
39+
read.Digitisation = float64(slow5Read.Digitisation)
40+
read.Offset = float64(slow5Read.Offset)
41+
read.Range = float64(slow5Read.Range)
42+
read.SamplingRate = float64(slow5Read.SamplingRate)
43+
read.LenRawSignal = uint64(slow5Read.LenRawSignal)
44+
45+
read.RawSignal = make([]int16, len(slow5Read.RawSignal))
46+
for i, v := range slow5Read.RawSignal {
47+
read.RawSignal[i] = int16(v)
48+
}
49+
50+
// Auxiliary fields
51+
read.ChannelNumber = slow5Read.ChannelNumber
52+
read.MedianBefore = float64(slow5Read.MedianBefore)
53+
read.ReadNumber = int32(slow5Read.ReadNumber)
54+
read.StartMux = uint8(slow5Read.StartMux)
55+
read.StartTime = uint64(slow5Read.StartTime)
56+
read.EndReason = slow5Read.EndReason
57+
58+
read.EndReasonMap = slow5Read.EndReasonMap
59+
60+
return read
61+
}
62+
63+
//nolint:dupl
64+
func ConvertReadToSlow5Read(read slow5.Read) gen.Slow5Read {
65+
var slow5Read gen.Slow5Read
66+
67+
// Convert and assign each field
68+
slow5Read.ReadID = read.ReadID
69+
slow5Read.ReadGroupID = int(read.ReadGroupID)
70+
slow5Read.Digitisation = float32(read.Digitisation)
71+
slow5Read.Offset = float32(read.Offset)
72+
slow5Read.Range = float32(read.Range)
73+
slow5Read.SamplingRate = float32(read.SamplingRate)
74+
slow5Read.LenRawSignal = int(read.LenRawSignal)
75+
76+
slow5Read.RawSignal = make([]int, len(read.RawSignal))
77+
for i, v := range read.RawSignal {
78+
slow5Read.RawSignal[i] = int(v)
79+
}
80+
81+
// Auxiliary fields
82+
slow5Read.ChannelNumber = read.ChannelNumber
83+
slow5Read.MedianBefore = float32(read.MedianBefore)
84+
slow5Read.ReadNumber = int(read.ReadNumber)
85+
slow5Read.StartMux = int(read.StartMux)
86+
slow5Read.StartTime = int(read.StartTime)
87+
slow5Read.EndReason = read.EndReason
88+
89+
slow5Read.EndReasonMap = read.EndReasonMap
90+
91+
return slow5Read
92+
}
93+
894
// ConvertGenbankToGenbankRecord converts a genbank.Genbank object to a gen.GenbankRecord object.
995
func ConvertGenbankToGenbankRecord(gb genbank.Genbank) gen.GenbankRecord {
1096
var features []gen.Feature

0 commit comments

Comments
 (0)