Question

PYTHON The text file motifFinding.txt contains two strings of DNA code, separated by a new-line character....

PYTHON

The text file motifFinding.txt contains two strings of DNA code, separated by a new-line character. Write a program that opens the file, and stores its contents into two strings, s and t, respectively.   Write code that will find all instances of the string t within the string s. At the end, your program should output the number of times the sub-string t occurs within s, along with the index of the starting position of each occurrence of t within s. You should not use any built-in Python string methods except len(<string>) to complete this project, but you may use string-slicing as needed.

NOTE: Depending on how you read the data from the file, you may also need to use the string method, rstrip() to clear white space. Using this method as needed is fine for this project.  

Output example:

The sub-string t occurs 4 times within the string s at starting positions indexed at 7, 19, 28 & 59.

Your code should be independent of the two strings s and t, and it should work for any two strings given in a different text file, as long as that file has the the same format as motifFinding.txt

motifFinding.txt :

CTCATGGTTTTCATGGTTCATGGTAGTTCGCCACGATCTGACTGTCATGGTTCATGGTTCATGGTGTCATGGTG

AGTCAAGTCATGGTCCCCTCATGGTATCATGGTAAAAAATAAAGCGATGATCATGGTGTCATGGTGTCATGGTG

ATCATGGTTCATGGTCTCTGTCATGGTGCGGTCATGGTGTGCCATGCTTTCATGGTTCATGGTATCATGGTTCA

TGGTTCATGGTACTGTCATCATCATGGTCAGTCATGGTTCATGGTTCTCATGGTCGATCATGGTTCATGGTTTT

GAGATCATGGTTTCATGGTGTAGTCATGGTCTGCTCATGGTTCATGGTTGTTTCATGGTAAATTCATGGTTTCA

TGGTTCATGGTGCAGCATCATGGTACGTCATGGTTGGTCATGGTATGTATCATGGTTACGATCATGGTGTTAAC

TTTCATGGTCTCATGGTGTTGCAGGGCATGTCTCTCTTATTGGCTTCATGGTATCATGGTTTATCATGGTATCC

TCCTCATGGTAGTTCATGGTCATCATGGTACCATCATGGTCGGATCATGGTTTCATGGTTCATGGTTCATCATG

GTTCATGGTCTTTATCATGGTTCATGGTGTTTCATGGTTGTCATGGTTTCATGGTCATGTCATGGTATATCATG

GTGGGCTCATGGTTCATGGTCTCATGGTATCATGGTATCATGGTCGAGTCATGGTCTTCATGGTTTTTAATCAT

GGTGATCATGGTTCATGGTGCTAAAGTTCATGGTACGTCATGGTTCATGGTTCATGGTTTGGCACGATCATGGT

CTAAATCATGGTATTCATGGT

TCATGGTTC

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python 3.7.4 (default, Jul 9 26 [GCC 6.3.0 20170516] on linux At index 10 main.py GTGTCATGGTG AGTCAAGTCATGGTCCCCTCATGGTATCATG

def readFile(fName):
        lines = open(fName, 'r').readlines()
        lines = [l.rstrip() for l in lines]
        newLineIndex = lines.index('')
        s = ''.join(lines[:newLineIndex])
        t = ''.join(lines[newLineIndex+1:])
        return (s, t)

def countOccurrence(s, t):
        count = 0
        for i in range(len(s)):
                if s[i:i+len(t)] == t:
                        print('At index', i)
                        count += 1
        print('Pattern occurs', count, 'times')


s, t = readFile('motifFinding.txt')
countOccurrence(s, t)

My results are difference because i used a different file probably.. I am assuming that in the file, there will be one line which is blank and that separates the two strings.. If any issues, please share the link to your text file. thanks!

Add a comment
Know the answer?
Add Answer to:
PYTHON The text file motifFinding.txt contains two strings of DNA code, separated by a new-line character....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python Create a program to open a text file for reading, find the maximum number in...

    python Create a program to open a text file for reading, find the maximum number in the file, determine if that maximum number is even, and write to an output text file. You should either write Yes if the number is even, otherwise write the maximum number. You should note the following: • Your input file must be named input.txt • The input file has one integer number per line • Your output file must be named output.txt • Your...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • In Python Provide me with your code file, output file and the text file Create a...

    In Python Provide me with your code file, output file and the text file Create a file having different integers than the first one. Save it as numbers1. txt . Write a program that reads all the number and calculates their average. Important: The two files that you are creating will contain different numbers.

  • 1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat,...

    1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat, a recursive function that takes two alphabetically-ordered strings and merges them, leaving the letters in alphabetical order. Note that the strings may be different lengths. a main method that inputs two ordered strings and calls the orderedConcat method to return a resulting merged, ordered string. Your main method should print the two input strings and the result. Note: You may not use any of...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • Create a python code named LetterCount with a text file named words.txt that contains at least...

    Create a python code named LetterCount with a text file named words.txt that contains at least 100 words in any format - some words on the same line, some alone (this program involves no writing so you should not be erasing this file). Then create a program in the main.py that prompts the user for a file name then iterates through all the letters in words.txt, counting the frequency of each letter, and then printing those numbers at the end...

  • Python: Using your favorite text editor, write a program that outputs the string representation of numbers...

    Python: Using your favorite text editor, write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Submit by uploading a .py file Example: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"...

  • PYTHON CODE First Code: Write a program that uses a text file to store the daysand...

    PYTHON CODE First Code: Write a program that uses a text file to store the daysand hours that a user worked in a week. The program should begin by prompting for the number of days worked in the week. It should continue with a loop for input of the days and hours and for writing these to the file, each on its own line. Sample Output (inputs shown in boldface) How many days did you work this week? 5 Enter...

  • please type code in Python find all the VALID email address in this input text file...

    please type code in Python find all the VALID email address in this input text file and store them as a single column of valid email addresses in an output text file labeled OutputFile First cut/paste the block of text below into a separate text file and label that data file InputFile: FinancialAid Email: [email protected] Scholarships Financial Aid Phone: 979-555-3236 Email: [email protected] Financial [email protected] Aid [email protected] Help Desk Central [email protected] Dean's Office      Jill Educational Program Coordinator II    Create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT