简体   繁体   中英

Python printing with user defined functions

I'm trying to write a code that will take data from a file and write it differently. I have the code for the most part but when i run it, everything is on one line.

import csv
#Step 4
def read_data(filename):
    try:
        data = open("dna.txt", "r")
    except IOError:
        print( "File not found")
    return data

#Step 5
def get_dna_stats(dna_string):
    a_letters = ""
    t_letters = ""
    if "A" in dna_string:
        a_letters.append("A")
    if "T" in dna_string:
        t_letters.append("T")
    nucleotide_content = ((len(a_letters) + len(t_letters))/len(dna_string))

#Step 6
def get_dna_complement(dna_string):
    dna_complement = ""
    for i in dna_string:
        if i == "A":
            dna_complement.append("T")
        elif i == "T":
            dna_complement.append("A")
        elif i == "G":
            dna_complement.append("C")
        elif i == "C":
            dna_complement.append("G")
        else:
            break
    return dna_complement

#Step 7
def print_dna(dna_strand):
    dna_complement = get_dna_complement(dna_strand)
    for i in dna_strand:
        for j in dna_complement:
            print( i + "=" + j)


#Step 8
def get_rna_sequence(dna_string):
    rna_complement = ""
    for i in dna_string:
        if i == "A":
            rna_complement.append("U")
        elif i == "T":
            rna_complement.append("A")
        elif i == "G":
            rna_complement.append("C")
        elif i == "C":
            rna_complement.append("G")
        else:
            break
    return rna_complement

#Step 9
def extract_exon(dna_strand, start, end):
    return (f"{dna_strand} between {start} and {end}")

#Step 10
def calculate_exon_pctg(dna_strand, exons):
    exons_length = 0
    for i in exons:
        exons_length += 1
    return exons_length/ len(dna_strand)

#Step 11
def format_data(dna_string):
    x = "dna_strand"[0:62].upper()
    y = "dna_strand"[63:90].lower()
    z = "dna_strand"[91:-1].upper()
    return x+y+z

#Step 12
def write_results(output, filename):
    try:
        with open("output.csv","w") as csvFile:
            writer = csv.writer(csvFile)
            for i in output:
                csvFile.write(i)
    except IOError:
        print("Error writing file")

#Step 13
def main():
    read_data("dna.txt")
    output = []
    output.append("The AT content is" + get_dna_stats() + "% of the DNA sequence.")
    get_dna_stats("dna_sequence")
    output.append("The DNA complement is " + get_dna_complement())
    get_dna_complement("dna_sequence")
    output.append("The RNA sequence is" + get_rna_sequence())
    get_rna_sequence("dna_sequence")
    exon1 = extract_exon("dna_sequence", 0, 62)
    exon2 = extract_exon("dna_sequence", 91, len("dna_sequence"))
    output.append(f"The exon regions are {exon1} and {exon2}")
    output.append("The DNA sequence, which exons in uppercase and introns in lowercase, is" + format_dna())
    format_data("dna_sequence")
    output.append("Exons comprise " + calculate_exon_pctg())
    calculate_exon_pctg("dna_sequence",[exon1, exon2])
    write_results(output, "results.txt")
    print("DNA processing complete")

#Step 14
if __name__ == "__main__":
    main()

When I run it, its supposed to output a file that looks like this but my code ends up putting every word on the top line like this I have a feeling it has to do with the write_results function but that's all i know on how to write to the file.

The second mistake I'm making is that I'm not calling the functions correctly in the append statements. I've tried concatenating and I've tried formatting the string but now I'm hitting a road block on what I need to do.

When you write to the file you need to concat a '\\n' to the end of the string every time you want to have something on a new line in the written file

for example:

output.append("The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n')

To solve your second problem I would change your code to something like this:

temp = "The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n'
output.append(temp)

When you append to a list and call a function it will take the literal text of the function instead of calling it. Doing it with a temp string holder will call the function before the string is concatenated. Then you are able to append the string to the list

read_data() doesn't actually read anything (just opens file). It should read the file and return its contents:

def read_data(filename):
    with open(filename, "r") as f:
        return f.read()

get_dna_stats() won't get DNA stats (won't return anything, and it doesn't count "A"s or "T"s, only checks if they're present, nucleotide_content is computed but never used or returned. It should probably count and return the results:

def get_dna_stats(dna_string):
    num_a = dna_string.count("A")
    num_t = dna_string.count("T")
    nucleotide_content = (num_a + num_t) /float(len(dna_string))
    return nucleotide_content

get_dna_complement() and get_rna_sequence() : you can't append to a string. Instead use

dna_complement += "T"

... and rather than break , you either append a "?" to denote a failed transscription, or raise ValueError("invalid letter in DNA: "+i)

print_dna() is a bit more interesting. I'm guessing you want to "zip" each letter of the DNA and its complement. Coincidentally, you can use the zip function to achieve just that:

def print_dna(dna_strand):
    dna_complement = get_dna_complement(dna_strand)
    for dna_letter, complement in zip(dna_strand, dna_complement):
        print(dna_letter + "=" + complement)

As for extract_exon() , I don't know what that is, but presumably you just want the substring from start to end , which is achieved by:

def extract_exon(dna_strand, start, end):
    return dna_strand[start:end]  # possibly end+1, I don't know exons

I am guessing that in calculate_exon_pctg() , you want exons_length += len(i) to sum the lengths of the exons. You can achieve this by using the buildin function sum :

exons_length = sum(exons)

In function format_data() , loose the doublequotes. You want the variable.

main() doesn't pass any data around. It should pass the results of read_data() to all the other functions:

def main():
    data = read_data("dna.txt")                                                 
    output = []
    output.append("The AT content is " + get_dna_stats(data) + "% of the DNA sequence.")
    output.append("The DNA complement is " + get_dna_complement(data))
    output.append("The RNA sequence is" + get_rna_sequence(data))
    ...
    write_results(output, "results.txt")                                        
    print("DNA processing complete")                                            

The key for you at this stage is to understand how function calls work: they take data as input parameters, and they return some results. You need to a) provide the input data, and b) catch the results.

write_results() - from your screenshot, you seem to want to write a plain old text file, yet you use csv.writer() (which writes CSV, ie tabular data). To write plain text,

def write_results(output, filename):
    with open(filename, "w") as f:
        f.write("\n".join(output))  # join output lines with newline
        f.write("\n")  # extra newline at file's end

If you really do want a CSV file, you'll need to define the columns first, and make all the output you collect fit that column format.

You never told your program to make a new line. You could either append or prepend the special "\\n" character to each of your strings or you could do it in a system agnostic way by doing

import os

at the top of your file and writing your write_results function like this:

def write_results(output, filename):
    try:
        with open("output.csv","w") as csvFile:
            writer = csv.writer(csvFile)
            for i in output:
                csvFile.write(i)
                os.write(csvFile, os.linesep)  # Add this line! It is a system agnostic newline
    except IOError:
        print("Error writing file")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM