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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:08:47+00:00 2026-05-30T21:08:47+00:00

I’m not sure if my title is correct for what I’m looking for, but

  • 0

I’m not sure if my title is correct for what I’m looking for, but I think that the referencing is the problem.

I have a Reader object through which I can loop:

msrun = pymzml.run.Reader(mzmlFile)
for feature in msrun:
    print feature['id']

With this code I get the id’s, starting at 1, of all the features in msrun. However, I need to loop through the code first and get all the keys that I want and put them in a list, like this:

def getKeys(msrun, excludeList):
    spectrumKeys = []
    done = False
    for spectrum in msrun:
        if done:
            break
        if spectrum['ms level'] == 2:
            for key in spectrum:
                if key not in excludeList and not key.startswith('MS:'): 
                    done = True
                    spectrumKeys.append(key)
            spectrumKeys.extend(spectrum['precursors'][0].keys())
            precursorKeys = spectrum['precursors'][0].keys()
            break
        return spectrumKeys, precursorKeys

However, if I would run this code:

msrun = pymzml.run.Reader(mzmlFile)
specKeys, precursKeys = getKeys(msrun, ['title','name'])
for feature in msrun:
    print feature['id']

it starts of at the id that hasn’t been in the loop in getKeys() (it starts at 11 instead of 1). So I guess pymzml.run.Reader() works like a generator object. So I tried copying the object. First I tried

copyMsrun = msrun
specKeys, precursKeys = getKeys(copyMsrun, ['title','name'])

But this gives the same problem, if I understood correctly because doing copyMsrun = msrun makes them point to the same thing.

Then I tried

import copy
copyMsrun = copy.copy(msrun)

But I still had the same problem. I used copy.copy instead of copy.deepcopy because I don’t think that the Reader objects contains other objects, and when I try deepcopy I get

TypeError: object.__new__(generator) is not safe, use generator.__new__().

So how do I copy an object so that looping through one doesn’t affect the other? Should I just do

msrun = pymzml.run.Reader(mzmlFile)
copyMsrun = pymzml.run.Reader(mzmlFile)

?


Edit:
On Ade YU’s comment, I tried that too but when I do

spectrumList = []
for spectrum in msrun:
    print spectrum['id']
    spectrumList.append(spectrum)

for spectrum in spectrumList:
    print spectrum['id']

The first print gives me 1-10, but the second print give me ten times 10

  • 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-05-30T21:08:48+00:00Added an answer on May 30, 2026 at 9:08 pm

    From the publication of pymzML and the documentation, it is clear that this “pathologically design” is done on purpose. Initializing thousands of spectrum objects will create a huge computational overhead, memory and cpu cycle wise that are simply not needed. Normally, parsing large sets of mzML naturally calls for analyze-while-parsing approach rather then collecting everything one needs to analyze later.

    Having said this, pymzML still offers the function to “deep copy” the spectrum simply by calling spectrum.deRef(). The advantage by using this function is that all unnecessary data will be stripped prior copying, hence offering smaller objects. pymzML deRef

      run = pymzml.run.Reader(file_to_read, MS1_Precision = 5e-6, MSn_Precision = 20e-6)
      for spec in run:
       tmp = spec.deRef()
    

    Hope that helps.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need a function that will clean a strings' special characters. I do NOT
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.