Question

In: Computer Science

RUBY Write a DNA class that gets initialized with a string of nucleotide characters. An instance...

RUBY

Write a DNA class that gets initialized with a string of nucleotide characters. An instance of DNA responds to the to_s, length, hamming_distance, contains, positions, and frequencies messages. Where a and b are DNA instances of the same length, the message send a.hamming_distance(b) returns the Hamming distance between a and b. a.contains(b) returns true if b appears as a substring in a, and returns false otherwise. a.positions(n) returns an array of the (zero-based) positions in which the nucleotide n occurs in increasing order. Frequencies returns a hash that maps each of the four nucleotides to the number of times it occurs in the DNA instance.

output:

>> dna1 = DNA.new('ATTGCC') => ATTGCC
>> dna1.length
=> 6

>> puts dna1
ATTGCC
>> dna1.contains('TGC')
true
>> dna1.contains(DNA.new('AT'))
true
>> dna1.contains('GG')
false
>> dna1.positions('T')
[1, 2]
>> dna2 = DNA.new('GTTGAC')
=> GTTGAC
>> dna1.hamming_distance(dna2)
=> 2
>> dna1.hamming_distance(dna1)
=> 0
>> dna1.hamming_distance(DNA.new('AT')) ArgumentError: dnas of different lengths

from ass2.rb:17:in `hamming_distance' from (irb):8
from C:/Ruby22/bin/irb:11:in '<main>'

>> dna1.frequencies
{"A"=>1, "T"=>2, "G"=>1, "C"=>2}

Solutions

Expert Solution

Copiable code:

class DNA

def initialize(string_dna)

@str_dna=string_dna

end

# method for print

def printdna()

puts @str_dna

end

# method for return

def getstring()

return @str_dna

end

# method for returning the length

def length

puts @str_dna.length

return @str_dna.length

end

  

#method for computing distance

def hamming_distance(dna2)

#If length not matched, return an error message

if @str_dna.length!=dna2.length

puts "ArgumentError: DNAs of different lengths"

#Compute distance

else

$ival = 0

$numval = dna2.length

$cnt_lo=0

begin

if @str_dna[$ival].chr!= dna2.getstring()[$ival].chr

$cnt_lo+=1

end

$ival +=1

end

while $ival < $numval

puts $cnt_lo

return $cnt_lo

end

end

end

end


Related Solutions

Write a class named GasTank containing: An instance variable named amount of type double, initialized to...
Write a class named GasTank containing: An instance variable named amount of type double, initialized to 0. An instance variable named capacity of type double. A constructor that accepts a parameter of type double. The value of the parameter is used to initialize the value of capacity. A method named addGas that accepts a parameter of type double. The value of the amount instance variable is increased by the value of the parameter. However, if the value of amount is...
A DNA strand is represented by a string of the characters A, C, G, and T,...
A DNA strand is represented by a string of the characters A, C, G, and T, each of which represents a nucleotide. Each nucleotide has its complement as indicated by this Ruby hash: NUCLEOTIDE_COMPLEMENT = { 'A' => 'T', 'T' => 'A', 'C' => 'G', 'G' => 'C' } The reverse-complement of a DNA string is a new string in which each nucleotide is replaced by its complement and the string is reversed. Reverse-complements are important in bioinformatics since a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Implement the ADT character string as the class LinkedString by using a linked list of characters....
Implement the ADT character string as the class LinkedString by using a linked list of characters. Include the following LinkedString constructors and methods: LinkedString(char[] value) Allocates a new character linked list so that it represents the sequence of characters currently contained in the character array argument. LinkedString(String original) Initializes a new character linked list so that it represents the same sequence of characters as the argument. char charAt(int index) Returns the char value at the specified index. The first character...
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced.
Write Java code that prompts the user for a string and tells them if the grouping characters in that string are balanced. Grouping characters are ( and ), [ and ], and { and }. I got the basic idea for this problem from HackerRank. It’s also a very common interview question.*******************8Example output**************Enter the expression:{[()]} {[()]}is balancedEnter the expression: {()()} {()()}is balancedEnter the expression: {([[])} {([[])}is not balancedEnter the expression: {([)]} {([)]}is not balanced
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
Write a method that computes the number of lowercase letters (a-z) characters in a String: The...
Write a method that computes the number of lowercase letters (a-z) characters in a String: The method signature is as follows:  public int count(String s)
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Given a String variable named sentence that has been initialized, write an expression whose value is...
Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence. write it in python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT