I have problem with a code. Im trying to create new bed file (The BED (Browser Extensible Data) format is a text file format used to store genomic regions as coordinates and associated annotations) to use it in my school work. I think its fine but it gives me back an error:
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python38\nano.py", line 50, in <module>
read_bed( simple_repeats_file )
File "C:\Users\AppData\Local\Programs\Python\Python38\nano.py", line 30, in read_bed
with(open(file,"r")) as bed:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
and I don´t know what to do with it.
#!/usr/bin/python
import os
import random
import pathlib
pick_random = 100000
mask_regions = dict()
genome = {
'chr01' : 41767771,
'chr02' : 34826099,
'chr03' : 43931233,
'chr04' : 45086258,
'chr05' : 46513039,
'chr06' : 43117521,
'chr07' : 39373400,
'chr08' : 51314288,
'chr09' : 47719527,
'chr10' : 40511255,
'chr11' : 34663808
}
genome_length = sum(genome.values())
simple_repeats_file = open("simpleRepeats.bed","w")
gaps_file = open("gap.bed","w")
def read_bed( file ):
with(open(file,"r")) as bed:
for line in bed:
line = line.rstrip()
chr, start, end = line.split("\t")
chr = chr.replace('chr','')
if chr == 'X':
chr = 23
try:
chr = int(chr)
except:
continue
if not chr in genome:
continue
if not chr in mask_regions:
mask_regions[chr] = dict()
if not start in mask_regions[chr]:
mask_regions[chr][int(start)] = dict()
mask_regions[chr][int(start)][int(end)] = 1
read_bed( simple_repeats_file )
read_bed( gaps_file )
random_positions = dict()
while( len(random_positions.keys()) < pick_random ):
randnum = random.randrange(1,genome_length)
tmp_length = 0
for chr, chrlength in sorted(genome.iteritems()):
if randnum < tmp_length+chrlength:
randchr = chr
randpos = randnum-tmp_length
break
tmp_length += chrlength
mask = False
for mask_start in sorted(mask_regions[randchr]):
if randpos < mask_start:
break
for mask_end in sorted(mask_regions[randchr][mask_start]):
if randpos <= mask_end:
#print( "MASK" )
mask = True
break
if not mask:
random_positions["\t".join([str(randchr),str(randpos),str(randpos+1)])] = 1
print( "\n".join(random_positions.keys()) )
I tried to solve it with PathLib library and some other things I found on inte.net, but still nothing.
You open
two files right away:
simple_repeats_file = open("simpleRepeats.bed","w")
gaps_file = open("gap.bed","w")
Then you call read_bed
on those:
read_bed( simple_repeats_file )
read_bed( gaps_file )
read_bed
tries opening the file given to it as an argument:
def read_bed( file ):
with(open(file,"r")) as bed:
...
So what essentially happens is
with open(open("simpleRepeats.bed","w"), "r") as bed:
...
This code, of course, crashes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.