Skip to content

Instantly share code, notes, and snippets.

@drdozer
Created March 16, 2017 11:04
Show Gist options
  • Save drdozer/809b61802ef6516ce5ab4dae83e02bb7 to your computer and use it in GitHub Desktop.
Save drdozer/809b61802ef6516ce5ab4dae83e02bb7 to your computer and use it in GitHub Desktop.
example of calling blast from ammonite
/// ceremony begins
import ammonite.ops._
import ammonite.ops.ImplicitWd._
import scala.util.Random
/// now we can write our script
println("Making random library")
val library = randomLibrary(10, 20, new Random())
val libraryAsfasta = library map (formatFasta _)
val libraryFile = "library.fa"
write.over(pwd/libraryFile, libraryAsfasta.mkString("\n"))
println("Running formatdb")
val dbName = "rand"
%(root / 'usr / 'bin / 'formatdb, "-t", "Random sequences", "-i", libraryFile, "-p", "F", "-n", dbName)
println("Running blast")
val blastFile = "blastResult"
%(root / 'usr / 'bin / 'blastn, "-db", dbName, "-query", libraryFile, "-out", blastFile )
println("Done")
/// helper types and functions
case class FastaSeq(descrTxt: String, seq: String)
def randomLibrary(size: Int, length: Int, rand: Random) =
for(i <- 1 to size) yield
FastaSeq(s"randseq_$i}",
randomDna(length, rand))
def randomDna(length: Int, rand: Random): String = {
val nucs = for(_ <- 0 until length) yield randomN(rand)
nucs.mkString
}
def randomN(rand: Random): Char = rand.nextInt(4) match {
case 0 => 'a'
case 1 => 'g'
case 2 =>'c'
case 3 => 't'
}
def formatFasta(s: FastaSeq) =
s""">${s.descrTxt}
|${s.seq.grouped(60).mkString("\n")}""".stripMargin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment