-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add samtools (without documentation) to minimap2 branch.
- Loading branch information
Showing
18 changed files
with
1,361 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
>Cn_Pp_Killer-alpha-MF-Delta | ||
AATGACCAAACCAACGCAAGTCTTAGTTCGTTCAGTCTCTATTTTATTCTTCATCACACTGTTGCACTTGGTTGTTGCAATGAGATTTCCTAGTATTTTCACTGCTGTGCTATTTGCCGCTAGTTCCGCTCTAGCTGCTCCAGTTAATACTACTACTGAAGATGAATTGGAGGGTGACTTCGATGTTGCTGTTCTGCCTTTTTCCGCTTCTATCGCAGCCAAGGAAGAAGGTGTATCTCTAGAGAAGCGTC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
Package samtools wraps the samtools cli to be used with Go. | ||
*/ | ||
package samtools | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// Pileup runs a | ||
func Pileup(templateFastas io.Reader, samAlignments io.Reader, w io.Writer) error { | ||
// Create a temporary file for the template fasta | ||
tmpFile, err := os.CreateTemp("", "template_*.fasta") | ||
if err != nil { | ||
return err | ||
} | ||
defer os.Remove(tmpFile.Name()) // Clean up file afterwards | ||
|
||
// Write template fasta data to the temporary file | ||
if _, err := io.Copy(tmpFile, templateFastas); err != nil { | ||
return err | ||
} | ||
tmpFile.Close() // Close the file as it's no longer needed | ||
|
||
g, ctx := errgroup.WithContext(context.Background()) | ||
|
||
// Setup pipe connections between commands | ||
viewSortReader, viewSortWriter := io.Pipe() | ||
sortMpileupReader, sortMpileupWriter := io.Pipe() | ||
|
||
// Define commands with context | ||
viewCmd := exec.CommandContext(ctx, "samtools", "view", "-bF", "4") | ||
sortCmd := exec.CommandContext(ctx, "samtools", "sort", "-") | ||
mpileupCmd := exec.CommandContext(ctx, "samtools", "mpileup", "-f", tmpFile.Name(), "-") | ||
|
||
// Goroutine for the first command: samtools view | ||
g.Go(func() error { | ||
defer viewSortWriter.Close() // ensure the pipe is closed after this function exits | ||
|
||
viewCmd.Stdin = samAlignments | ||
viewCmd.Stdout = viewSortWriter | ||
|
||
if err := viewCmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
viewCmd.Process.Signal(syscall.SIGTERM) | ||
return ctx.Err() | ||
default: | ||
return viewCmd.Wait() | ||
} | ||
}) | ||
|
||
// Goroutine for the second command: samtools sort | ||
g.Go(func() error { | ||
defer sortMpileupWriter.Close() // ensure the pipe is closed after this function exits | ||
|
||
sortCmd.Stdin = viewSortReader | ||
sortCmd.Stdout = sortMpileupWriter | ||
|
||
if err := sortCmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
sortCmd.Process.Signal(syscall.SIGTERM) | ||
return ctx.Err() | ||
default: | ||
return sortCmd.Wait() | ||
} | ||
}) | ||
|
||
// Goroutine for the third command: samtools mpileup | ||
g.Go(func() error { | ||
mpileupCmd.Stdin = sortMpileupReader | ||
mpileupCmd.Stdout = w | ||
|
||
if err := mpileupCmd.Start(); err != nil { | ||
return err | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
mpileupCmd.Process.Signal(syscall.SIGTERM) | ||
return ctx.Err() | ||
default: | ||
return mpileupCmd.Wait() | ||
} | ||
}) | ||
|
||
// Wait for all goroutines to complete and return the first non-nil error | ||
if err := g.Wait(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package samtools_test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/koeng101/dnadesign/external/samtools" | ||
"github.com/koeng101/dnadesign/lib/bio" | ||
) | ||
|
||
func TestPileup(t *testing.T) { | ||
// Open the template FASTA file | ||
templateFile, err := os.Open("./data/template.fasta") | ||
if err != nil { | ||
t.Fatalf("Failed to open template FASTA file: %v", err) | ||
} | ||
defer templateFile.Close() | ||
|
||
// Open the sam file | ||
samFile, err := os.Open("./data/aln.sam") | ||
if err != nil { | ||
t.Fatalf("Failed to open sam alignment file: %v", err) | ||
} | ||
defer samFile.Close() | ||
|
||
// Prepare the writer to capture the output | ||
var buf bytes.Buffer | ||
|
||
// Execute the pileup function | ||
err = samtools.Pileup(templateFile, samFile, &buf) | ||
if err != nil { | ||
t.Errorf("Pileup returned error: %s", err) | ||
} | ||
|
||
// Read as pileup file | ||
parser := bio.NewPileupParser(&buf) | ||
lines, err := parser.Parse() | ||
if err != nil { | ||
t.Errorf("Failed while parsing: %s", err) | ||
} | ||
|
||
expectedQuality := "3555457667367556657568659884340:7" | ||
if lines[5].Quality != expectedQuality { | ||
t.Errorf("Bad quality return. Got: %s Expected: %s", lines[5].Quality, expectedQuality) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.