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 8310913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:37:58+00:00 2026-06-08T19:37:58+00:00

I got the following method which is used to read a txt file and

  • 0

I got the following method which is used to read a txt file and return a dictionary. It takes ~7 mins to read a ~5MB file (67000 lines, 70 chars in each line).

public static Dictionary<string, string> FASTAFileReadIn(string file)
{
    Dictionary<string, string> seq = new Dictionary<string, string>();

    Regex re;
    Match m;
    GroupCollection group;
    string currentName = string.Empty;

    try
    {
        using (StreamReader sr = new StreamReader(file))
        {
            string line = string.Empty;
            while ((line = sr.ReadLine()) != null)
            {
                if (line.StartsWith(">"))
                {// Match Sequence
                    re = new Regex(@"^>(\S+)");
                    m = re.Match(line);
                    if (m.Success)
                    {
                        group = m.Groups;
                        if (!seq.ContainsKey(group[1].Value))
                        {
                            seq.Add(group[1].Value, string.Empty);
                            currentName = group[1].Value;
                        }
                    }
                }
                else if (Regex.Match(line.Trim(), @"\S+").Success &&
                            currentName != string.Empty)
                {
                    seq[currentName] += line.Trim();
                }
            }
        }
    }
    catch (IOException e)
    {
        Console.WriteLine("An IO exception has benn thrown!");
        Console.WriteLine(e.ToString());
    }
    finally { }

    return seq;
}

Which part of the code is most time consuming and how to speed it up?

Thanks

  • 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-08T19:38:00+00:00Added an answer on June 8, 2026 at 7:38 pm

    Cache and compile regular expressions, reorder conditionals, lessen number of trimmings, and such.

    public static Dictionary<string, string> FASTAFileReadIn(string file) {
        var seq = new Dictionary<string, string>();
    
        Regex re = new Regex(@"^>(\S+)", RegexOptions.Compiled);
        Regex nonWhitespace = new Regex(@"\S", RegexOptions.Compiled);
        Match m;
        string currentName = string.Empty;
    
        try {
            foreach(string line in File.ReadLines(file)) {
                if(line[0] == '>') {
                    m = re.Match(line);
    
                    if(m.Success) {
                        if(!seq.ContainsKey(m.Groups[1].Value)) {
                            seq.Add(m.Groups[1].Value, string.Empty);
                            currentName = m.Groups[1].Value;
                        }
                    }
                } else if(currentName != string.Empty) {
                    if(nonWhitespace.IsMatch(line)) {
                        seq[currentName] += line.Trim();
                    }
                }
            }
        } catch(IOException e) {
            Console.WriteLine("An IO exception has been thrown!");
            Console.WriteLine(e.ToString());
        }
    
        return seq;
    }
    

    However, that’s just a naïve optimization. Reading up on the FASTA format, I wrote this:

    public static Dictionary<string, string> ReadFasta(string filename) {
        var result = new Dictionary<string, string>
        var current = new StringBuilder();
        string currentKey = null;
    
        foreach(string line in File.ReadLines(filename)) {
            if(line[0] == '>') {
                if(currentKey != null) {
                    result.Add(currentKey, current.ToString());
                    current.Clear();
                }
    
                int i = line.IndexOf(' ', 2);
    
                currentKey = i > -1 ? line.Substring(1, i - 1) : line.Substring(1);
            } else if(currentKey != null) {
                current.Append(line.TrimEnd());
            }
        }
    
        if(currentKey != null)
            result.Add(currentKey, current.ToString());
    
        return result;
    }
    

    Tell me if it works; it should be much faster.

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

Sidebar

Related Questions

I've got following method in a class, which is a custom adapter for an
I've got following problem: (c#) There is some class (IRC bot), which has method,
I've got a method which currently takes an IList as a parameter, where UserBO
In my widgets_controller.rb I've got the following method # GET /widgets/index_of_deleted def index_of_deleted @widgets
I've got a GET method that looks like the following: GetMethod method = new
so I've got the following structures; Start.master (Start.master.cs) Contains a Method DoSomething(string Text) {
I've got a Ruby method like the following: # Retrieve all fruits from basket
Hello got the error in following code.. The method RefeshDataGridView() is static.I run it
I have got the following java class. When I am calling the login method
I have the following problem. I got a class PluginLoader which oversees loading of

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.