Bioinformatics/Sequence mutation: Difference between revisions

Content deleted Content added
Galileo (talk | contribs)
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 19: Line 19:
{{trans|Python}}
{{trans|Python}}


<lang 11l>UInt32 seed = 0
<syntaxhighlight lang=11l>UInt32 seed = 0
F nonrandom(n)
F nonrandom(n)
:seed = 1664525 * :seed + 1013904223
:seed = 1664525 * :seed + 1013904223
Line 69: Line 69:
print(‘ #10 @#.’.format(kind, index))
print(‘ #10 @#.’.format(kind, index))
print()
print()
seq_pp(mseq)</lang>
seq_pp(mseq)</syntaxhighlight>


{{out}}
{{out}}
Line 115: Line 115:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Containers.Vectors;
<syntaxhighlight lang=Ada>with Ada.Containers.Vectors;
with Ada.Numerics.Discrete_Random;
with Ada.Numerics.Discrete_Random;
with Ada.Text_Io;
with Ada.Text_Io;
Line 231: Line 231:
Pretty_Print (Sequence);
Pretty_Print (Sequence);


end Mutations;</lang>
end Mutations;</syntaxhighlight>
{{out}}
{{out}}
<pre>Initial sequence:
<pre>Initial sequence:
Line 267: Line 267:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>bases: ["A" "T" "G" "C"]
<syntaxhighlight lang=rebol>bases: ["A" "T" "G" "C"]
dna: map 1..200 => [sample bases]
dna: map 1..200 => [sample bases]


Line 334: Line 334:
print "------------------------------"
print "------------------------------"
prettyPrint dna
prettyPrint dna
print ""</lang>
print ""</syntaxhighlight>


{{out}}
{{out}}
Line 373: Line 373:
=={{header|C}}==
=={{header|C}}==
Adenine ( A ) is always swapped for Thymine ( T ) and vice versa. Similarly with Cytosine ( C ) and Guanine ( G ).
Adenine ( A ) is always swapped for Thymine ( T ) and vice versa. Similarly with Cytosine ( C ) and Guanine ( G ).
<syntaxhighlight lang=C>
<lang C>
#include<stdlib.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdio.h>
Line 585: Line 585:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
Sample run :
Sample run :
<pre>
<pre>
Line 674: Line 674:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <array>
<syntaxhighlight lang=cpp>#include <array>
#include <iomanip>
#include <iomanip>
#include <iostream>
#include <iostream>
Line 797: Line 797:
sequence_generator::print_sequence(std::cout, sequence);
sequence_generator::print_sequence(std::cout, sequence);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 836: Line 836:
:: :genome <i><Genome Sequence></i>)
:: :genome <i><Genome Sequence></i>)
<b>All keys are optional. <i><Genome length></i> is discarded when :genome is set.</b>
<b>All keys are optional. <i><Genome length></i> is discarded when :genome is set.</b>
<lang lisp>
<syntaxhighlight lang=lisp>
(defun random_base ()
(defun random_base ()
(random 4))
(random 4))
Line 910: Line 910:
(t (delete_base genome)))))
(t (delete_base genome)))))
(output_genome_info genome "MUTATED"))
(output_genome_info genome "MUTATED"))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 981: Line 981:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: assocs combinators.random formatting grouping io kernel
<syntaxhighlight lang=factor>USING: assocs combinators.random formatting grouping io kernel
macros math math.statistics namespaces prettyprint quotations
macros math math.statistics namespaces prettyprint quotations
random sequences sorting ;
random sequences sorting ;
Line 1,054: Line 1,054:
[ mutate ] curry times nl "MUTATED " write show-dna ;
[ mutate ] curry times nl "MUTATED " write show-dna ;


MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,103: Line 1,103:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 1,201: Line 1,201:
fmt.Println()
fmt.Println()
prettyPrint(dna, 50)
prettyPrint(dna, 50)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,257: Line 1,257:
</pre>
</pre>
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (group, sort)
<syntaxhighlight lang=haskell>import Data.List (group, sort)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import System.Random (Random, randomR, random, newStdGen, randoms, getStdRandom)
import System.Random (Random, randomR, random, newStdGen, randoms, getStdRandom)
Line 1,336: Line 1,336:
showSequence = mapM_ (uncurry (printf "%3d: %s\n")) . chunkedDNASequence
showSequence = mapM_ (uncurry (printf "%3d: %s\n")) . chunkedDNASequence
showBaseCounts = mapM_ (uncurry (printf "%s: %3d\n")) . baseCounts
showBaseCounts = mapM_ (uncurry (printf "%s: %3d\n")) . baseCounts
showSumBaseCounts xs = putStrLn (replicate 6 '-') >> printf "Σ: %d\n\n" (length xs)</lang>
showSumBaseCounts xs = putStrLn (replicate 6 '-') >> printf "Σ: %d\n\n" (length xs)</syntaxhighlight>
{{out}}
{{out}}
<pre>Initial Sequence:
<pre>Initial Sequence:
Line 1,379: Line 1,379:


=={{header|J}}==
=={{header|J}}==
<lang J>ACGT=: 'ACGT'
<syntaxhighlight lang=J>ACGT=: 'ACGT'
MUTS=: ;: 'del ins mut'
MUTS=: ;: 'del ins mut'


Line 1,414: Line 1,414:
)
)


simulate=: (sim@(1 1 1&; &. |. ))`sim@.(3=#)</lang>
simulate=: (sim@(1 1 1&; &. |. ))`sim@.(3=#)</syntaxhighlight>


{{out}}
{{out}}
Line 1,508: Line 1,508:


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.util.Arrays;
<syntaxhighlight lang=java>import java.util.Arrays;
import java.util.Random;
import java.util.Random;


Line 1,620: Line 1,620:
private static final int OP_COUNT = 3;
private static final int OP_COUNT = 3;
private static final char[] BASES = {'A', 'C', 'G', 'T'};
private static final char[] BASES = {'A', 'C', 'G', 'T'};
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,654: Line 1,654:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>// Basic set-up
<syntaxhighlight lang=javascript>// Basic set-up
const numBases = 250
const numBases = 250
const numMutations = 30
const numMutations = 30
Line 1,811: Line 1,811:
console.log('\nMUTATED BASE COUNTS:')
console.log('\nMUTATED BASE COUNTS:')
printBases(mut);
printBases(mut);
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,878: Line 1,878:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>dnabases = ['A', 'C', 'G', 'T']
<syntaxhighlight lang=julia>dnabases = ['A', 'C', 'G', 'T']
randpos(seq) = rand(1:length(seq)) # 1
randpos(seq) = rand(1:length(seq)) # 1
mutateat(pos, seq) = (s = seq[:]; s[pos] = rand(dnabases); s) # 2-1
mutateat(pos, seq) = (s = seq[:]; s[pos] = rand(dnabases); s) # 2-1
Line 1,931: Line 1,931:


testbioseq()
testbioseq()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
500nt DNA sequence:
500nt DNA sequence:
Line 1,994: Line 1,994:
=={{header|Lua}}==
=={{header|Lua}}==
Using the <code>prettyprint()</code> function from [[Bioinformatics/base_count#Lua]] (not replicated here)
Using the <code>prettyprint()</code> function from [[Bioinformatics/base_count#Lua]] (not replicated here)
<lang lua>math.randomseed(os.time())
<syntaxhighlight lang=lua>math.randomseed(os.time())
bases = {"A","C","T","G"}
bases = {"A","C","T","G"}
function randbase() return bases[math.random(#bases)] end
function randbase() return bases[math.random(#bases)] end
Line 2,022: Line 2,022:


print("MUTATED:")
print("MUTATED:")
prettyprint(seq)</lang>
prettyprint(seq)</syntaxhighlight>
{{out}}
{{out}}
<pre>ORIGINAL:
<pre>ORIGINAL:
Line 2,056: Line 2,056:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
BioSequence is a fundamental data type in Mathematica:
BioSequence is a fundamental data type in Mathematica:
<lang Mathematica>SeedRandom[13122345];
<syntaxhighlight lang=Mathematica>SeedRandom[13122345];
seq = BioSequence["DNA", "ATAAACGTACGTTTTTAGGCT"];
seq = BioSequence["DNA", "ATAAACGTACGTTTTTAGGCT"];
randompos = RandomInteger[seq["SequenceLength"]];
randompos = RandomInteger[seq["SequenceLength"]];
Line 2,090: Line 2,090:
ends = Rest[Accumulate[Prepend[StringLength /@ parts, 0]]];
ends = Rest[Accumulate[Prepend[StringLength /@ parts, 0]]];
StringRiffle[MapThread[ToString[#1] <> "-" <> ToString[#2] <> ": " <> #3 &, {begins, ends, parts}], "\n"]
StringRiffle[MapThread[ToString[#1] <> "-" <> ToString[#2] <> ": " <> #3 &, {begins, ends, parts}], "\n"]
Tally[Characters[seq["SequenceString"]]]</lang>
Tally[Characters[seq["SequenceString"]]]</syntaxhighlight>
{{out}}
{{out}}
<pre>1-50: TAGCAGGGGAATTGTCGACTCCCGGGTTTCAATTGCCAACCAAGCATATT
<pre>1-50: TAGCAGGGGAATTGTCGACTCCCGGGTTTCAATTGCCAACCAAGCATATT
Line 2,106: Line 2,106:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import random
<syntaxhighlight lang=Nim>import random
import strformat
import strformat
import strutils
import strutils
Line 2,222: Line 2,222:
echo "\nMutated sequence"
echo "\nMutated sequence"
echo "————————————————\n"
echo "————————————————\n"
dnaSeq.display()</lang>
dnaSeq.display()</syntaxhighlight>


{{out}}
{{out}}
Line 2,261: Line 2,261:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 2,305: Line 2,305:
say "Total bases: ". length $mutate;
say "Total bases: ". length $mutate;
say "$_: $cnt{$_}" for @bases;
say "$_: $cnt{$_}" for @bases;
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Original DNA strand:
<pre>Original DNA strand:
Line 2,332: Line 2,332:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #004080;">string</span> <span style="color: #000000;">dna</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">200</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">300</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">dna</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">200</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">300</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dna</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000000;">dna</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ACGT"</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dna</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000000;">dna</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ACGT"</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
Line 2,368: Line 2,368:
<span style="color: #000000;">mutate</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">mutate</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,410: Line 2,410:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>#BASE$="ACGT"
<syntaxhighlight lang=PureBasic>#BASE$="ACGT"
#SEQLEN=200
#SEQLEN=200
#PROTOCOL=#True
#PROTOCOL=#True
Line 2,467: Line 2,467:
PrintN("After 10 mutations:")
PrintN("After 10 mutations:")
pprint()
pprint()
Input()</lang>
Input()</syntaxhighlight>
{{out}}
{{out}}
<pre>Initial sequence:
<pre>Initial sequence:
Line 2,499: Line 2,499:
Similarly parameter choice is chosen from to give the base for substitution or insertion - the more any base appears, the more likely it is to be chosen in any insertion/substitution.
Similarly parameter choice is chosen from to give the base for substitution or insertion - the more any base appears, the more likely it is to be chosen in any insertion/substitution.
<lang python>import random
<syntaxhighlight lang=python>import random
from collections import Counter
from collections import Counter


Line 2,544: Line 2,544:
print(f" {kind:>10} @{index}")
print(f" {kind:>10} @{index}")
print()
print()
seq_pp(mseq)</lang>
seq_pp(mseq)</syntaxhighlight>


{{out}}
{{out}}
Line 2,592: Line 2,592:
<code>prettyprint</code> and <code>tallybases</code> are defined at [[Bioinformatics/base count#Quackery]].
<code>prettyprint</code> and <code>tallybases</code> are defined at [[Bioinformatics/base count#Quackery]].


<lang Quackery> [ $ "ACGT" 4 random peek ] is randomgene ( --> c )
<syntaxhighlight lang=Quackery> [ $ "ACGT" 4 random peek ] is randomgene ( --> c )


[ $ "" swap times
[ $ "" swap times
Line 2,609: Line 2,609:
cr cr say "Mutating..." cr
cr cr say "Mutating..." cr
10 times mutate
10 times mutate
dup prettyprint cr cr tallybases</lang>
dup prettyprint cr cr tallybases</syntaxhighlight>


{{out}}
{{out}}
Line 2,643: Line 2,643:
=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket
<syntaxhighlight lang=racket>#lang racket


(define current-S-weight (make-parameter 1))
(define current-S-weight (make-parameter 1))
Line 2,720: Line 2,720:
(define s+d (parameterize ((current-D-weight 5)) (for/fold ((s initial-sequence)) ((_ 10)) (mutate s))))
(define s+d (parameterize ((current-D-weight 5)) (for/fold ((s initial-sequence)) ((_ 10)) (mutate s))))
(newline)
(newline)
(report-sequence s+d))</lang>
(report-sequence s+d))</syntaxhighlight>


{{out}}
{{out}}
Line 2,791: Line 2,791:




<lang perl6>my @bases = <A C G T>;
<syntaxhighlight lang=raku line>my @bases = <A C G T>;


# The DNA strand
# The DNA strand
Line 2,822: Line 2,822:
sub diff ($orig, $repl) {
sub diff ($orig, $repl) {
($orig.comb Z $repl.comb).map( -> ($o, $r) { $o eq $r ?? $o !! $r.lc }).join
($orig.comb Z $repl.comb).map( -> ($o, $r) { $o eq $r ?? $o !! $r.lc }).join
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>ORIGINAL DNA STRAND:
<pre>ORIGINAL DNA STRAND:
Line 2,849: Line 2,849:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang=ring>
row = 0
row = 0
dnaList = []
dnaList = []
Line 2,962: Line 2,962:
total = dnaBase["A"] + dnaBase["T"] + dnaBase["C"] + dnaBase["G"]
total = dnaBase["A"] + dnaBase["T"] + dnaBase["C"] + dnaBase["G"]
see "Total: " + total+ nl + nl
see "Total: " + total+ nl + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,010: Line 3,010:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>class DNA_Seq
<syntaxhighlight lang=ruby>class DNA_Seq
attr_accessor :seq
attr_accessor :seq
Line 3,044: Line 3,044:
test.delete
test.delete
puts test
puts test
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre> 0 TAAGGTGAGGAGTGTGATGGAGTTCGGTGGCTAGCCACAAATACAACACA
<pre> 0 TAAGGTGAGGAGTGTGATGGAGTTCGGTGGCTAGCCACAAATACAACACA
Line 3,067: Line 3,067:
=={{header|Swift}}==
=={{header|Swift}}==


<lang swift>let bases: [Character] = ["A", "C", "G", "T"]
<syntaxhighlight lang=swift>let bases: [Character] = ["A", "C", "G", "T"]


enum Action: CaseIterable {
enum Action: CaseIterable {
Line 3,123: Line 3,123:
}
}


printSeq(d)</lang>
printSeq(d)</syntaxhighlight>


{{out}}
{{out}}
Line 3,154: Line 3,154:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>import rand
<syntaxhighlight lang=vlang>import rand
import rand.seed
import rand.seed


Line 3,246: Line 3,246:
println('')
println('')
pretty_print(dna, 50)
pretty_print(dna, 50)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,306: Line 3,306:
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang=ecmascript>import "random" for Random
import "/fmt" for Fmt
import "/fmt" for Fmt
import "/sort" for Sort
import "/sort" for Sort
Line 3,383: Line 3,383:
for (i in 0...muts) dna = mutate.call(dna, w)
for (i in 0...muts) dna = mutate.call(dna, w)
System.print()
System.print()
prettyPrint.call(dna, 50)</lang>
prettyPrint.call(dna, 50)</syntaxhighlight>


{{out}}
{{out}}
Line 3,441: Line 3,441:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|Phix}}
{{trans|Phix}}
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Sequence_mutation
<syntaxhighlight lang=Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Sequence_mutation
// by Galileo, 07/2022
// by Galileo, 07/2022


Line 3,493: Line 3,493:
show()
show()
mutate()
mutate()
show()</lang>
show()</syntaxhighlight>
{{out}}
{{out}}
<pre>1: TCCATCGTGG GATCGCTCTA GCGGTATGCT ATCATTCCTA TAGCAATTCT
<pre>1: TCCATCGTGG GATCGCTCTA GCGGTATGCT ATCATTCCTA TAGCAATTCT
Line 3,528: Line 3,528:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>var [const] bases="ACGT", lbases=bases.toLower();
<syntaxhighlight lang=zkl>var [const] bases="ACGT", lbases=bases.toLower();
dna:=(190).pump(Data().howza(3),(0).random.fp(0,4),bases.get); // bucket of bytes
dna:=(190).pump(Data().howza(3),(0).random.fp(0,4),bases.get); // bucket of bytes


Line 3,560: Line 3,560:
[0..*,50].zipWith(fcn(n,bases){ println("%6d: %s".fmt(n,bases.concat())) },
[0..*,50].zipWith(fcn(n,bases){ println("%6d: %s".fmt(n,bases.concat())) },
dna.walker().walk.fp(50)).pump(Void); // .pump forces the iterator
dna.walker().walk.fp(50)).pump(Void); // .pump forces the iterator
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,590: Line 3,590:


=={{header|Rust}}==
=={{header|Rust}}==
<lang Rust>
<syntaxhighlight lang=Rust>
use rand::prelude::*;
use rand::prelude::*;
use std::collections::HashMap;
use std::collections::HashMap;
Line 3,687: Line 3,687:
println!("\nMutated sequence:\n{}", seq);
println!("\nMutated sequence:\n{}", seq);
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>