This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Order of files parsed into my jupyter notebook

Hello, I am very very new to python.

I have defined a function to parse some files using pysam and added them to a list in my Jupyter notebook using something like:

def grab_all_files_(fdir):
    files_ = []
    for f in glob.glob(fdir + '/*.bam'):
        bams.append(pysam.AlignmentFile(f, "rb"))
return files_

How do I know the order in which the files are listed? (I am using python 3.9.12) For example, how do I know which file is files_[0], files_[1]...

Thank you

python jupyter python3.9.12

Looking past that after the line def grab_all_files_(fdir):, there should be indentation...
As written, I don't see you adding anything into the list files_?

You are right. I fixed my question, sorry. It was correct in my notebook, though. How do I know the order in which the files are listed? (I am using python 3.9.12) For example, how do I know which file is files_[0], files_[1]... please?

You fixed the indenting of the function some. (The return line should be indented, too, I suspect.) However, you aren't adding anything to files_ still with current code you show here (pasted below with posted error(s) intact).

def grab_all_files_(fdir):
    files_ = []
    for f in glob.glob(fdir + '/*.bam'):
        bams.append(pysam.AlignmentFile(f, "rb"))
return files_

Is anything getting returned from grab_all_files_ when you actually run your code?

As for how do you know the order ... They'd be in the order that glob.glob() iterates on them which I assume is how the files are listed in your system? (You can easily test this in a separate notebook, by running glob.glob(fdir + '/*.bam') in a cell.) Usually you don't need to know the order because either you make a file that has a name similar to the input or you use a dictionary to track the correspondences.

Thank you so much! That answered my question. Oh, the function works as is... (:

0 answers

No answers yet.

Log in to answer this question.