File handling – exercise
Lets write a (new) file
Use as a name of the file: "file.txt"
By using "w" it will overwrite any existing files
The file will be located in the current working directory, unless you specify the entire path before the filename
File handling – good practice
After opening also close your files:
The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done. It is a good practice to use close().
File handling – good practice
Before opening a file its also good to actually check the file exists
File handling – writing
To write to a file you use the command below:
file.write("What you want to write")
Try yourself to write some variables to a file
File handling – reading
Try to read the lines you just wrote to a file in the previous exercise
Newline characters
Most of the times the newline characters at the end of a line are simply said “annoying”, we can remove them using the following command:
line.rstrip() ## removes newline character
File handling – splitting the lines
To split your line, you can use the line.split() function using any delimiter (example below)
But of course if we want to split on a bit more difficult pattern we rather use the Regex split function we discussed this morning
File handling – exercise
The file "exercise.bed" contains genomic regions and is rather large, the perfect opportunity to use Python to process this file
Read the BED file and make an output file were "chr" in front of the number is removed (e.g. chr1 will be 1).
Determine the number of regions covered in each chromosome (e.g. each line is a region chromosome 1 --> 71906 regions)
Parse and print to screen the total number of regions and size covered for each chromosome
Challenge: combine number 1 and 2
As frequently requested two examples:
Limit the raw_input to only nucleotides:
As frequently requested two examples:¶
make reverse complement a sequence easy: