<jupyter_start><jupyter_text>How to write a script on your own Start with the problem: AnalysisThe problem we want to solve is:I want a program which translates all my protein file to nucleotide files.Although, this is a simple problem, there is not enough information for us to get started with the solution. A little more analysis is required. For example, how do we specify which files are to be processed? How does the translation work? Can all the information be translated or should we skip some information? How are the translated files stored? After analysing the problem properly, we design our program.  Design and test We make a list of things about how our program should work. We decompose the order of functional steps the program should follow and for each step we think about what information is needed to process the step and what should be the result of the step. For instance, in our case we need the following steps1.	Get a list of files that need to be processed2.	Process per filea.	Get to be translated amino acid sequence b.	Translate the amino acid sequence to dna sequencec.	Store the translated dna sequence to an outputfileSince we need a,b and c to execute multiple times it is best to put them in functions. Best is to write a simple body of functions and test the logic with print.<jupyter_code>#!/usr/bin/env python3

""" translates all protein files to nucleotide files"""

__author__ = 'fennaf'

#imports
import sys

#functions
def fetch_sequence():
    print("fetch sequence")

def translate_aa2dna():
    print("translate aa to dna")

def store_translation():
    print("store stranslation")

def main(args):
    fetch_sequence()
    translate_aa2dna()
    store_translation()
    
if __name__ == '__main__':
    exitcode = main(sys.argv)
    sys.exit(exitcode)<jupyter_output>fetch sequence
translate aa to dna
store stranslation<jupyter_text>So let us now think about what information is needed to process the step and what should be the result of the step. For the fetch_sequence we need a file to be read and we should return a amino acid sequence. For the translate_aa2dna we need a amino acid sequence to be translated and a translation process. The output will be a dna sequence. The store_translation function needs a name of a file to write to and the translated sequence. If we design the needed input and output into our script it looks as follow:<jupyter_code>#!/usr/bin/env python3

""" translates all protein files to nucleotide files"""

__author__ = 'fennaf'

#imports
import sys

#functions
def fetch_sequence(file):
    sequence = ""
    print("fetch sequence")
    return sequence

def translate_aa2dna(aa_sequence):
    dna_sequence = ""
    print("translate aa to dna")
    return dna_sequence


def store_translation(output_file, sequence):
    print("store stranslation")
    o = open(output_file, "w")
    o.write(sequence)
    o.close()


def main(args):
    input_file = "CFTR_protein.fasta"
    aa_sequence = fetch_sequence(input_file)
    dna_sequence = translate_aa2dna(aa_sequence)
    store_translation("test.txt", dna_sequence)
    
if __name__ == '__main__':
    exitcode = main(sys.argv)
    sys.exit(exitcode)<jupyter_output>fetch sequence
translate aa to dna
store stranslation<jupyter_text>It is now time to move one step further and develop the logic per function. Best practise is to develop and test small pieces. First let us develop the fetch_sequence function. We need to open the file, read the file but skip the first line because this has no sequence information. Only if the file starts with the letter M we now that it contains protein information.<jupyter_code>def fetch_sequence(file):
    sequence = ""
    f = open(file)
    for line in f:
        if not line.startswith('>'):
            sequence += line
    print("fetch sequence", sequence)
    return sequence

def main(args):
    input_file = "CFTR_protein.fasta"
    aa_sequence = fetch_sequence(input_file)
    # dna_sequence = translate_aa2dna(aa_sequence)
    # store_translation("test.txt", dna_sequence)
    
    
if __name__ == '__main__':
    exitcode = main(sys.argv)
    sys.exit(exitcode)<jupyter_output>fetch sequence MQRSPLEKASVVSKLFFSWTRPILRKGYRQRLELSDIYQIPSVDSADNLSEKLEREWDRELASKKNPKLI
NALRRCFFWRFMFYGIFLYLGEVTKAVQPLLLGRIIASYDPDNKEERSIAIYLGIGLCLLFIVRTLLLHP
AIFGLHHIGMQMRIAMFSLIYKKTLKLSSRVLDKISIGQLVSLLSNNLNKFDEGLALAHFVWIAPLQVAL
LMGLIWELLQASAFCGLGFLIVLALFQAGLGRMMMKYRDQRAGKISERLVITSEMIENIQSVKAYCWEEA
MEKMIENLRQTELKLTRKAAYVRYFNSSAFFFSGFFVVFLSVLPYALIKGIILRKIFTTISFCIVLRMAV
TRQFPWAVQTWYDSLGAINKIQDFLQKQEYKTLEYNLTTTEVVMENVTAFWEEGFGELFEKAKQNNNNRK
TSNGDDSLFFSNFSLLGTPVLKDINFKIERGQLLAVAGSTGAGKTSLLMVIMGELEPSEGKIKHSGRISF
CSQFSWIMPGTIKENIIFGVSYDEYRYRSVIKACQLEEDISKFAEKDNIVLGEGGITLSGGQRARISLAR
AVYKDADLYLLDSPFGYLDVLTEKEIFESCVCKLMANKTRILVTSKMEHLKKADKILILHEGSSYFYGTF
SELQNLQPDFSSKLMGCDSFDQFSAERRNSILTETLHRFSLEGDAPVSWTETKKQSFKQTGEFGEKRKNS
ILNPINSIRKFSIVQKTPLQMNGIEEDSDEPLERRLSLVPDSEQGEAILPRISVISTGPTLQARRRQSVL
NLMTHSVNQGQNIHRKTTASTRKVSLAPQANLTELDIYSRRLSQETGLEISEEINEEDLKECFFDDMESI
PAVTTWNTYLRYITVHKSLIFVLIWCLVIFLAEVAASLVVLWLLGNTPLQDKGNSTHSRNNSYAVIITST
SSYYVFYIYVGVADTLLAMGFFRGLPLVHTLITVSKILHHKMLHSVLQAPMSTLNTLKAGGI[...]<jupyter_text>The print statement helps us to debug the function. It seems to work, so we eliminate the print function.<jupyter_code>def fetch_sequence(file):
    sequence = ""
    f = open(file)
    for line in f:
        if not line.startswith('>'):
            sequence += line
    return sequence

def main(args):
    input_file = "CFTR_protein.fasta"
    aa_sequence = fetch_sequence(input_file)
    # dna_sequence = translate_aa2dna(aa_sequence)
    # store_translation("test.txt", dna_sequence)
    
    
if __name__ == '__main__':
    exitcode = main(sys.argv)
    sys.exit(exitcode)<jupyter_output><empty_output><jupyter_text>Now we move on to the translate function. To translate we need to go character(one amino acid is one character) by charachter through the file and translate this to the dna codon. The dna codon must be added to the dan_sequence. I use a simple reverse_codon table first to test the principle<jupyter_code>def fetch_sequence(file):
    sequence = ""
    f = open(file)
    for line in f:
        if not line.startswith('>'):
            sequence += line
    return sequence

def translate_aa2dna(aa_sequence):
    dna_sequence = ""
    # this need to be extended
    reverse_codon = {
        "M" : "ATG",
        "F" : "TTT",
        "D" : "GAT"
        }
    for char in aa_sequence:
        if char in reverse_codon:
            dna_sequence += reverse_codon[char]
    print(dna_sequence)
    return dna_sequence

def main(args):
    input_file = "CFTR_protein.fasta"
    aa_sequence = fetch_sequence(input_file)
    dna_sequence = translate_aa2dna(aa_sequence)
    # store_translation("test.txt", dna_sequence)
    
    
if __name__ == '__main__':
    exitcode = main(sys.argv)
    sys.exit(exitcode)<jupyter_output>ATGTTTTTTGATGATGATGATTTTTTTTTTATGTTTTTTGATGATTTTTTTATGATGATGTTTGATTTTGATTTTATGTTTTTTTTTATGATGATGGATATGATGATGTTTTTTTTTTTTTTTTTTTTTTTTTTTATGTTTGATGATTTTATGTTTTTTTTTGATGATTTTTTTTTTGATTTTATGATGTTTTTTATGTTTGATGATTTTGATGATGATGATTTTGATTTTATGATGGATTTTTTTGATTTTATGGATTTTGATTTTTTTGATTTTTTTTTTATGGATGATGATATGGATGATTTTTTTGATGATATGTTTTTTGATTTTGATATGTTTTTTATGATGTTTGATGATGATTTTGATTTTTTTTTTATGTTTTTTTTTTTTTTTTTTTTTATGATGTTTTTTTTTTTTATGATGGATGATATGTTTTTTGATATGATGGATGATATGGATTTTTTTGATGATTTTTTTTTTTTTGATGATGATTTTGATTTTGATATGGATGATTTTGATATGTTTGATTTTGATTTTGAT<jupyter_text>Now let us test the final part. To make sure that each output file is stored with a different name we make an outputvariable<jupyter_code>def fetch_sequence(file):
    sequence = ""
    f = open(file)
    for line in f:
        if not line.startswith('>'):
            sequence += line
    return sequence

def translate_aa2dna(aa_sequence):
    dna_sequence = ""
    # this need to be extended
    reverse_codon = {
        "M" : "ATG",
        "F" : "TTT",
        "D" : "GAT"
        }
    for char in aa_sequence:
        if char in reverse_codon:
            dna_sequence += reverse_codon[char]
    return dna_sequence


def store_translation(output_file, sequence):
    print("store stranslation")
    o = open(output_file, "w")
    o.write(sequence)
    o.close()

def main(args):
    input_file = "CFTR_protein.fasta"
    aa_sequence = fetch_sequence(input_file)
    dna_sequence = translate_aa2dna(aa_sequence)
    output_file = "output" + input_file[:-6] 
    store_translation(output_file, dna_sequence)
    
    
if __name__ == '__main__':
    exitcode = main(sys.argv)
    sys.exit(exitcode)<jupyter_output>store stranslation<jupyter_text>The final thing we need to do is incoperate process multiple files feature, so we design a for loop:<jupyter_code>def main(args):
    files = args[1:]
    for input_file in files:
        aa_sequence = fetch_sequence(input_file)
        dna_sequence = translate_aa2dna(aa_sequence)
        output_file = "output" + input_file[:-6] 
        store_translation(output_file, dna_sequence)
    
if __name__ == '__main__':
    exitcode = main(sys.argv)
    sys.exit(exitcode)<jupyter_output>store stranslation
{'fraction_non_alphanumeric': 0.06399298706991015, 'fraction_numerical': 0.0020819636204251588, 'mean_word_length': 4.575442883323152, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 15, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '415250', 'license': 'CNRI-Python', 'n_tokens_mistral': 3249, 'n_tokens_neox': 3078, 'n_words': 841, 'path': 'how_to_write_your_own_script.ipynb', 'repo_name': 'fenna/BFVP3INF2_DEMO', 'size': '9126'}