Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:51:36+00:00 2026-06-11T20:51:36+00:00

I have a dataset with FASTA formatted sequencing, basically like this: >pc284 ATCGCGACTCGAC >pc293

  • 0

I have a dataset with FASTA formatted sequencing, basically like this:

>pc284  
ATCGCGACTCGAC

>pc293  
ACCCGACCTCAGC

I want to take to use each tag as a key in the dictionary, and store the gene as a value.

This is the code I have, but really isn’t doing anything:

import re
fileData = open('d.fasta', 'r')

myDict = dict()

for line in fileData:
  match = re.search('(\>)(\w+)(\r)(\w+)', line)
  if match: 
    gene = match.group(3)
    myDict[gene[0]] = gene[1]

print myDict
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:51:36+00:00Added an answer on June 11, 2026 at 8:51 pm

    \r is not a valid character class, I think you meant to use \s instead. You can reduce the groups if you don’t use them either.

    But most of all, you need to extract your groups correctly:

    match = re.search(r'>(\w+)\s+(\w+)', line)
    if match:
        tag, gene = match.groups()
        myDict[tag] = gene
    

    By creating only two capturing groups, we can more simply extract those two with .groups() and directly assign them to two variables, tag and gene.

    However, reading up on the FASTA format seems to indicate this is a multi-line format with the tag on one line, the gene data on multiple lines after that. In that case your \r was meant to match the newline. This won’t work as you read the file one line at a time.

    It would be much simpler to read that format without regular expressions like so:

    myDict = {}
    
    with open('d.fasta', 'rU') as fileData:
        tag = None
        for line in fileData:
            line = line.strip()
            if not line:
                continue
            if line[0] == '>':
                tag = line[1:]
                myDict[tag] = ''
            else:
                assert tag is not None, 'Invalid format, found gene without tag'
                myDict[tag] += line
    
    print myDict
    

    This reads the file line by line, detecting tags based on the starting > character, then reads multiple lines of gene information collecting it into your dictionary under the most-recently read tag.

    Note the rU mode; we open the file using python’s universal newlines mode, to handle whatever newline convention was used to create the file.

    Last but not least; take a look at the BioPy project; their Bio.SeqIO module handles FASTA plus many other formats perfectly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataset that analogously looks like this: X | U | datetime
I have a dataset with one datatable. Now I want to set a filter
I have a dataset with some 30 records in it. I want to update
I have a dataset that looks like: ColA ColB ColC ColD ColE rs778 C
I have DataSet which has 3 columns. Name - insurance comp. name - treatmentDate
I have a dataset with ~20M rows and I'm observing the following behavior. The
I have a DataSet , where in the first table and in the row
I have a dataset that i am trying to set the default value as
I have following dataset: name1 <- c(P1, P2, IndA, IndB, IndC, IndD, IndE, IndF,
We have a Dataset with about 40 TableAdapters on it. These are used everywhere

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.