1 DNA := Object clone do (
   2     complementWith := Map clone
   3     complementWith atPut("A", "T")
   4     complementWith atPut("T", "A")
   5     complementWith atPut("G", "C")
   6     complementWith atPut("C", "G")
   7 
   8     title := ""
   9     sequence := ""
  10 
  11     getRcSequence := method(
  12         result := list
  13         sequence foreach(i, v,
  14             nucleotide:= v asCharacter
  15             result append(complementWith at(nucleotide))
  16         )
  17         result reverse join("")
  18     )
  19 
  20     setFromString := method(s,
  21         lines := s split("\n")
  22         title = lines first slice(1)
  23         lines removeFirst
  24         
  25         slines:= stripLines(lines)
  26         sequence = slines join("")
  27     )
  28 
  29     stripLines := method(lines,
  30         slines:= list
  31         lines foreach(i, line,
  32             slines append(line strip)
  33         )
  34     )
  35 
  36     toString := method(col,
  37         lines := list()
  38         for(i,0, ((sequence size) / col) floor,
  39             lines append(sequence slice(col*i, col*i+col))
  40         )
  41         ">" .. title .. "\n"..(lines join("\n"))
  42     )
  43 )
  44 
  45 splitFasta := method(file,
  46     totalLines:= list
  47     lines := list(file readLine)
  48     while(true,
  49         line := file readLine
  50         if(line == nil, break)
  51         if(line at(0) asCharacter == ">",
  52             totalLines append(lines join("\n").."\n")
  53             lines = list(line)
  54         ,
  55             lines append(line)
  56         )
  57     )
  58     totalLines append(lines join("\n").."\n")
  59 )
  60 
  61 main := method(
  62     dna := DNA clone
  63     records := splitFasta(stdin)
  64     records foreach(i, record,
  65         dna setFromString(record)
  66         dna sequence = dna getRcSequence
  67         dna toString(3) println
  68     )
  69 )
  70 
  71 MockFile := Object clone do(
  72     lines := list
  73     setString := method(s,
  74         lines = s split("\n")
  75     )
  76 
  77     readLine := method(    
  78         line := lines first
  79         lines removeFirst
  80         line
  81     )
  82 )
  83 
  84 DnaTest := UnitTest clone do(
  85     testGetRcSequence := method(
  86         dna := DNA clone
  87         dna title := "test"
  88         dna sequence := "AGTC"
  89         assertEquals("GACT", dna getRcSequence)
  90         dna sequence = "AAGGTCCC"
  91         assertEquals("GGGACCTT", dna getRcSequence)
  92     )
  93 
  94     testSetFromString := method(
  95         dna := DNA clone
  96         dna setFromString(">test\nAGTC \nAAA")
  97         assertEquals("test", dna title)
  98         assertEquals("AGTCAAA", dna sequence)
  99     )
 100 
 101     testFastaRecord := method(
 102         file := MockFile clone
 103         file setString(">test\nAGTC\nAAA\n>test2\nGGG\n") 
 104         fastaRecords := splitFasta(file)
 105         assertEquals(">test\nAGTC\nAAA\n", fastaRecords at(0))
 106         assertEquals(">test2\nGGG\n", fastaRecords at(1))
 107     )
 108 
 109     testDnaToString := method(
 110         dna := DNA clone
 111         dna title = "test"
 112         dna sequence = "AGTC"
 113         assertEquals(">test\nAGT\nC", dna toString(3))
 114     )
 115 )

ReverseComplement.io (last edited 2011-08-03 11:00:52 by localhost)

web biohackers.net