Tag Archives: Python tips

[Solved] Python Networkx Error: Network error: random_state_index is incorrect

A few months ago, you can also use the Networkx package in Python. Recently, you rerun the previous code and report the following error

Network error: random_state_index is incorrect

Now the landlord has solved this problem. The specific steps are as follows:

First: open Anaconda prompt (Anaconda 3), demote Networkx package and decorator package, and enter the following code:

pip install --user decorator==4.3.0
pip install --user networkx==2.3

Second: turn off Anaconda prompt (Anaconda 3) and reopen Python to run without problem.

[Solved] Python Read bam File Error: &&OSError: no BGZF EOF marker; file may be truncated

1. Read FASTA file:

(1) Method 1: Bio Library

from Bio import SeqIO

fa_seq = SeqIO.read("res/sequence1.fasta", "fasta")

seq = str(fa_seq.seq)

seqs = [fa.seq for fa in SeqIO.parse("res/multi.fasta", "fasta")]

(2) Method 2: pysam Library

fa = pysam.FastaFile(‘filename’)

fa.references

fa.lengths

seq = fa.fetch(reference=’chr21’, start=a, end=b)

seq = fa.fetch(reference=’chr21’)

2. Convert sequence to string: str (FA. SEQ)


print("G Counts: ", fa.count("G"))

print("reverse: ", fa[::-1])

print("Reverse complement: ", fa.complement())

3、pysam read bam file error:
File “pysam/libcalignmentfile.pyx”, line 742, in pysam.libcalignmentfile.AlignmentFile.__cinit__
File “pysam/libcalignmentfile.pyx”, line 952, in pysam.libcalignmentfile.AlignmentFile._open
File “pysam/libchtslib.pyx”, line 365, in pysam.libchtslib.HTSFile.check_truncation
OSError: no BGZF EOF marker; file may be truncated
The file can be read this way, i.e. by adding ignore_truncation=True:

bam = pysam.AlignmentFile(‘**.bam’, "rb", ignore_truncation=True)

4. Cram format file: it has the characteristics of high compression and has a higher compression rate than BAM. Most files in cram format may be used in the future

5. Comparison data (BAM/cram/SAM), variation data: (VCF/BCF)

6. Get each read

for read in bam:
    read.reference_name # The name of the chromosome to which the reference sequence is compared.

    read.pos # the position of the read alignment

    read.mapq # the quality value of the read comparison

    read.query_qualities # read sequence base qualities

    read.query_sequence # read sequence bases

    read.reference_length # 在reference genome上read比对的长度

7. Read cram file

cf = pysam.AlignmentFile(‘*.cram’, ‘rc’)

8. Read SAM file

samfile = pysam.AlignmentFile(‘**.sam’, ‘r’)

9. Get a region in Bam file

for r in pysam.AlignmentFile(‘*.bam’, ‘rb’).fetch(‘chr21’, 300, 310):
    pass  # This is done provided that the *.bam file is indexed

No code, a solution to the error in the path of reading CSV file by Python: filenotfounderror: [errno 2] no such file or directory: ‘XX. CSV‘

As a python Xiaobai, what I want to see most is a simple tutorial, just like a fool operation. In recent days, I have found a simple way to solve the problem of Python path error reporting.

First of all, the owner does put the code. Py file and the original data. CSV in a folder, then open Spyder and click open file – & gt; Open code.py, but the error is as follows:

After trying to use several methods mentioned on the Internet, they all have no effect

In Spyder’s navigation bar, as shown in the figure below,

Click the folder symbol on the right side of the figure above (the part circled by the red box), the following selection box will pop up, and then select the folder where your code.py is located. At this point, the path has been changed.

Finally, make sure that the path of the following two red boxes is consistent, and the code can run smoothly

Original is not easy, feel useful, please like collection forward! Grow up together on the way of self-learning Python!

The end, the flowers~