alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (regexdna.scala)

This example Scala source code file (regexdna.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, array, b, d, io, k, n, n, pair, pair, stringbuffer, v, w, w, y

The Scala regexdna.scala source code

/* The Computer Language Shootout
   http://shootout.alioth.debian.org/
   contributed by Isaac Gouy
*/

import java.io.InputStreamReader, java.util.regex._

object regexdna { 
   def main(args: Array[String]) = {

      var sequence = readFully()
      val initialLength = sequence.length

      // remove FASTA sequence descriptions and new-lines
      var m = Pattern.compile(">.*\n|\n").matcher(sequence)
      sequence = m.replaceAll("")
      val codeLength = sequence.length

      // regex match
      val variants = Array (
         "agggtaaa|tttaccct"
         ,"[cgt]gggtaaa|tttaccc[acg]"
         ,"a[act]ggtaaa|tttacc[agt]t"
         ,"ag[act]gtaaa|tttac[agt]ct"
         ,"agg[act]taaa|ttta[agt]cct"
         ,"aggg[acg]aaa|ttt[cgt]ccct"
         ,"agggt[cgt]aa|tt[acg]accct"
         ,"agggta[cgt]a|t[acg]taccct"
         ,"agggtaa[cgt]|[acg]ttaccct"
         )

      for (v <- variants){
         var count = 0
         m = Pattern.compile(v).matcher(sequence)
         while (m.find()) count = count + 1
         Console.println(v + " " + count)
      }

      // regex substitution
      val codes = Array (
             Pair("B", "(c|g|t)")
            ,Pair("D", "(a|g|t)")
            ,Pair("H", "(a|c|t)")
            ,Pair("K", "(g|t)")
            ,Pair("M", "(a|c)")
            ,Pair("N", "(a|c|g|t)")
            ,Pair("R", "(a|g)")
            ,Pair("S", "(c|g)")
            ,Pair("V", "(a|c|g)")
            ,Pair("W", "(a|t)")
            ,Pair("Y", "(c|t)")
         )

      for (iub <- codes){
         iub match { 
            case Pair(code,alternative) => 
               sequence = Pattern.compile(code).matcher(sequence).replaceAll(alternative)
         }
      }

      Console.println("\n" + initialLength + "\n" + codeLength + "\n" + sequence.length)
   } 


   def readFully() = {
      val blockSize = 10240
      val block = new Array[Char](blockSize)
      val buffer = new StringBuffer(blockSize)
      val r = new InputStreamReader(System.in)

      var charsRead = r.read(block, 0, blockSize)
      while (charsRead > -1){
         buffer.append(block,0,charsRead)
         charsRead = r.read(block, 0, blockSize)
      }

      r.close
      buffer.toString
   }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala regexdna.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.