2
$\begingroup$
file_name = "consen1.txt"
    with open(file_name) as infile:
        new_dict = {}
        for line in infile:
            if line.startswith(">"):
                key = line.strip('\n')
            else:
                line = ''.join(x.strip('\n') for x in infile)
                new_dict[key] = line
    print(new_dict)  
    for key,values in new_dict.items():
        gc = values.count('G') + values.count('C')
        content = (gc/len(values)) *100
        new_dict[key] = content
    print(new_dict)

I am reading from a text file, and everything works fine but for some reason it is skipping the line of sequence from the text file. Here's how the text file looks like,

>Rosalind_4478
GCTACATCAACGCCGTGCATGGCGTGTAAAGACCCGCATTTTACAGGCAGTGTATACGAC
CGGTAACACGATTTAGCATTTACTGAGCCCCTGCGTTATTGGAATATGAGGGAACGTTGC
TTATGTGCAAGACATTCCACCTCATCCATAGATACTCTCGCGGCAGATTCTGTGACGGAT
CGGATCGCTTCAAAGCACGGTACGGTATTAGACGAATGGGTTAAAACTCTCTACAAGCAC
TCGTACAACTTATGCACACCAACTTCAGCGTATAACATGAGAGCCCGGAAAGGCATATTC
CTTATCTTAAGGTAGGGAATACGAAAACCCGCGTCGTCCGCCCACCTCGACGGTACGGAA
TGTTTGAGGGGCATCCGGGTCTGGCAGGAACTGTGTGGACTGGTGCGTCATCAGCTGGGC
TGATTGCGCGAGCCAGAATCGCATGCGCTTCGAATTTTAGGAGAGCGTGCCGACTCACGT
ATATCATTGACATGGTGAGGGTAGCAGAATCTACATTGCAGCCTATGACGCTACACGGCC
GTATAATCAGTGTTAGTGCAGTATTGTGGCATTTCCGTTTCGCATGGTTAACTGAGGCCA
AGGTTTGTGCCGGGGGACCGGATGGCTACCTGGAAACTAGGATCTGGCCGTAGTGGTTCT
CGCGAACCCTAGTTTCAAGCAAACGCCAGCCGGAGACCGAAGTTAAACGCATCACTACTA
CTCACGAGTCTTGGACCAAAACAGAAAGTCGGATGCACCATGTACGAGCTGACGTTGTCT
TTATGTCCAGTTGGATCTGGACAGCACATCCGAGATTTTGAATTAAAACTTCTTTTTGTG
TGATGCTTGGAACACCCGCCATACAACAAATAATAGCGCCTTAAACAGCCTACGGCACAG
TA

Expected output:

 {'>Rosalind_4478': 'GCTACATCAACGCCGTGCATGGCGTGTAAAGACCCGCATTTTACAGGCAGTGTATACGACCGGTAACACGATTTAGCATTTACTGAGCCCCTGCGTTATTGGAATATGAGGGAACGTTGCTTATGTGCAAGACATTCCACCTCATCCATAGATACTCTCGCGGCAGATTCTGTGACGGATCGGATCGCTTCAAAGCACGGTACGGTATTAGACGAATGGGTTAAAACTCTCTACAAGCACTCGTACAACTTATGCACACCAACTTCAGCGTATAACATGAGAGCCCGGAAAGGCATATTCCTTATCTTAAGGTAGGGAATACGAAAACCCGCGTCGTCCGCCCACCTCGACGGTACGGAATGTTTGAGGGGCATCCGGGTCTGGCAGGAACTGTGTGGACTGGTGCGTCATCAGCTGGGCTGATTGCGCGAGCCAGAATCGCATGCGCTTCGAATTTTAGGAGAGCGTGCCGACTCACGTATATCATTGACATGGTGAGGGTAGCAGAATCTACATTGCAGCCTATGACGCTACACGGCCGTATAATCAGTGTTAGTGCAGTATTGTGGCATTTCCGTTTCGCATGGTTAACTGAGGCCAAGGTTTGTGCCGGGGGACCGGATGGCTACCTGGAAACTAGGATCTGGCCGTAGTGGTTCTCGCGAACCCTAGTTTCAAGCAAACGCCAGCCGGAGACCGAAGTTAAACGCATCACTACTACTCACGAGTCTTGGACCAAAACAGAAAGTCGGATGCACCATGTACGAGCTGACGTTGTCTTTATGTCCAGTTGGATCTGGACAGCACATCCGAGATTTTGAATTAAAACTTCTTTTTGTGTGATGCTTGGAACACCCGCCATACAACAAATAATAGCGCCTTAAACAGCCTACGGCACAGTA'}

Real Output:

{'>Rosalind_4478': 'CGGTAACACGATTTAGCATTTACTGAGCCCCTGCGTTATTGGAATATGAGGGAACGTTGCTTATGTGCAAGACATTCCACCTCATCCATAGATACTCTCGCGGCAGATTCTGTGACGGATCGGATCGCTTCAAAGCACGGTACGGTATTAGACGAATGGGTTAAAACTCTCTACAAGCACTCGTACAACTTATGCACACCAACTTCAGCGTATAACATGAGAGCCCGGAAAGGCATATTCCTTATCTTAAGGTAGGGAATACGAAAACCCGCGTCGTCCGCCCACCTCGACGGTACGGAATGTTTGAGGGGCATCCGGGTCTGGCAGGAACTGTGTGGACTGGTGCGTCATCAGCTGGGCTGATTGCGCGAGCCAGAATCGCATGCGCTTCGAATTTTAGGAGAGCGTGCCGACTCACGTATATCATTGACATGGTGAGGGTAGCAGAATCTACATTGCAGCCTATGACGCTACACGGCCGTATAATCAGTGTTAGTGCAGTATTGTGGCATTTCCGTTTCGCATGGTTAACTGAGGCCAAGGTTTGTGCCGGGGGACCGGATGGCTACCTGGAAACTAGGATCTGGCCGTAGTGGTTCTCGCGAACCCTAGTTTCAAGCAAACGCCAGCCGGAGACCGAAGTTAAACGCATCACTACTACTCACGAGTCTTGGACCAAAACAGAAAGTCGGATGCACCATGTACGAGCTGACGTTGTCTTTATGTCCAGTTGGATCTGGACAGCACATCCGAGATTTTGAATTAAAACTTCTTTTTGTGTGATGCTTGGAACACCCGCCATACAACAAATAATAGCGCCTTAAACAGCCTACGGCACAGTA'}

I do realize that I can do this kind of computation easily with modules like biopython, But I wanted to try doing this from scratch. Thanks in advance for the help.

$\endgroup$
1
  • $\begingroup$ You can also unlock hundreds of solutions to this problem on the Rosalind website, once you submit the correct answer. $\endgroup$
    – gaspanic
    Jul 29, 2022 at 0:29

2 Answers 2

1
$\begingroup$

Your approach is a bit risky code ... Anyway not tested but I think this will work:

with open(file_name) as infile:
    new_dict = {}
    for l in infile:
        line = l.rstrip('\n')
        if line.startswith(">"):
            k = line.lstrip('>')
            new_dict[k] = [] # must initiate
            # new_dict = {k:[]} is possible alternative initiation 
        else:
            new_dict[key].append(line)

I not sure without testing which dictionary initiation will work. People do use defaultdict

from collections import defaultdict
new = defaultdict(def_value)

This circumvents the initiation issue to a large extent (if the dictionary key ain't correctly initiated it will throw a value error, or some sort of bug). However it isn't actually needed and I find it unnecessary.

What I am certain about is the following should be avoided because your code depends on the exact line by line order between the fasta header and the code you are associating it with. You likely break that association with the code below,

line = ''.join(x.strip('\n') for x in infile) # you are calling the entire infile not the line

BTW

from collections import Count ()
new = Count()

Is a very good approach for the next part of your script, i.e. counting %GC

$\endgroup$
3
  • 1
    $\begingroup$ Thank you so much... Now I see my mistakes... BTW You seem really experienced with bioinfo, if it's not too much to ask... what does one have to do in order to become really good at bioinfo? $\endgroup$ Jul 25, 2022 at 10:09
  • 1
    $\begingroup$ Thanks, the following is just my opinion for bioinformatics. Enjoy reading technical computing books, e.g. O'Reilly or Packet (they are exciting, technology, advances, applications), a good IDE with breakpoints certainly helps to start (I use Spyder) and for me what's really important is to visualise an algorithm without seeing the code. $\endgroup$
    – M__
    Jul 25, 2022 at 11:30
  • 1
    $\begingroup$ TRY : rosalind.info $\endgroup$
    – pippo1980
    Jul 28, 2022 at 16:08
2
$\begingroup$

needed to edit accepted code to make it works:

file_name = "consen1.txt"

with open(file_name) as infile:
    new_dict = {}
    for l in infile:
        line = l.rstrip('\n')
        if line.startswith(">"):
            k = line.lstrip('>')
            new_dict[k] = str() # must initiate like string !!!!!!!!!!!!!
            # new_dict = {k:[]} #is possible alternative initiation 
        else:
            new_dict[k] += line
            
print(new_dict)

for key,values in new_dict.items():
    # print(key, values)
    gc = values.count('G') + values.count('C')
    content = (gc/len(values)) *100
    new_dict[key] = content
    
print(new_dict)

output :

{'Rosalind_4478': 'GCTACATCAACGCCGTGCATGGCGTGTAAAGACCCGCATTTTACAGGCAGTGTATACGACCGGTAACACGATTTAGCATTTACTGAGCCCCTGCGTTATTGGAATATGAGGGAACGTTGCTTATGTGCAAGACATTCCACCTCATCCATAGATACTCTCGCGGCAGATTCTGTGACGGATCGGATCGCTTCAAAGCACGGTACGGTATTAGACGAATGGGTTAAAACTCTCTACAAGCACTCGTACAACTTATGCACACCAACTTCAGCGTATAACATGAGAGCCCGGAAAGGCATATTCCTTATCTTAAGGTAGGGAATACGAAAACCCGCGTCGTCCGCCCACCTCGACGGTACGGAATGTTTGAGGGGCATCCGGGTCTGGCAGGAACTGTGTGGACTGGTGCGTCATCAGCTGGGCTGATTGCGCGAGCCAGAATCGCATGCGCTTCGAATTTTAGGAGAGCGTGCCGACTCACGTATATCATTGACATGGTGAGGGTAGCAGAATCTACATTGCAGCCTATGACGCTACACGGCCGTATAATCAGTGTTAGTGCAGTATTGTGGCATTTCCGTTTCGCATGGTTAACTGAGGCCAAGGTTTGTGCCGGGGGACCGGATGGCTACCTGGAAACTAGGATCTGGCCGTAGTGGTTCTCGCGAACCCTAGTTTCAAGCAAACGCCAGCCGGAGACCGAAGTTAAACGCATCACTACTACTCACGAGTCTTGGACCAAAACAGAAAGTCGGATGCACCATGTACGAGCTGACGTTGTCTTTATGTCCAGTTGGATCTGGACAGCACATCCGAGATTTTGAATTAAAACTTCTTTTTGTGTGATGCTTGGAACACCCGCCATACAACAAATAATAGCGCCTTAAACAGCCTACGGCACAGTA'}
{'Rosalind_4478': 49.55654101995565}

works using this snippet too. but feel like is not orthodox python:

file_name = "consen1.txt"


with open(file_name) as infile:
        new_dict = {}
        for line in infile:
            if line.startswith(">"):
                key = line.strip('\n')
                
                print(key)
                
                line_2 = None
                
            else:
                
                if not line_2:
                
                    line_2 = ''.join(line.strip('\n'))
                
                else:
                    
                    line_2 += ''.join(line.strip('\n'))
                
                # print(line_2)
                
                new_dict[key] = line_2
                
                
print(new_dict)  

for key,values in new_dict.items():
    gc = values.count('G') + values.count('C')
    content = (gc/len(values)) *100
    new_dict[key] = content
    
print(new_dict)

output:

>Rosalind_4478
{'>Rosalind_4478': 'GCTACATCAACGCCGTGCATGGCGTGTAAAGACCCGCATTTTACAGGCAGTGTATACGACCGGTAACACGATTTAGCATTTACTGAGCCCCTGCGTTATTGGAATATGAGGGAACGTTGCTTATGTGCAAGACATTCCACCTCATCCATAGATACTCTCGCGGCAGATTCTGTGACGGATCGGATCGCTTCAAAGCACGGTACGGTATTAGACGAATGGGTTAAAACTCTCTACAAGCACTCGTACAACTTATGCACACCAACTTCAGCGTATAACATGAGAGCCCGGAAAGGCATATTCCTTATCTTAAGGTAGGGAATACGAAAACCCGCGTCGTCCGCCCACCTCGACGGTACGGAATGTTTGAGGGGCATCCGGGTCTGGCAGGAACTGTGTGGACTGGTGCGTCATCAGCTGGGCTGATTGCGCGAGCCAGAATCGCATGCGCTTCGAATTTTAGGAGAGCGTGCCGACTCACGTATATCATTGACATGGTGAGGGTAGCAGAATCTACATTGCAGCCTATGACGCTACACGGCCGTATAATCAGTGTTAGTGCAGTATTGTGGCATTTCCGTTTCGCATGGTTAACTGAGGCCAAGGTTTGTGCCGGGGGACCGGATGGCTACCTGGAAACTAGGATCTGGCCGTAGTGGTTCTCGCGAACCCTAGTTTCAAGCAAACGCCAGCCGGAGACCGAAGTTAAACGCATCACTACTACTCACGAGTCTTGGACCAAAACAGAAAGTCGGATGCACCATGTACGAGCTGACGTTGTCTTTATGTCCAGTTGGATCTGGACAGCACATCCGAGATTTTGAATTAAAACTTCTTTTTGTGTGATGCTTGGAACACCCGCCATACAACAAATAATAGCGCCTTAAACAGCCTACGGCACAGTA'}
{'>Rosalind_4478': 49.55654101995565}

be sure to check if they are both working with your input

$\endgroup$
1
  • $\begingroup$ Thanks @pippo1980. I like the new_dict[k] = str() new_dict[k] += line approach rather than append. The append approach will work. Making += work is cool without using from collections import defaultdict. The GC% using count() method directly rather than via Count() $\endgroup$
    – M__
    Jul 28, 2022 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.