sequence_description = ">gb:AF333238|A/Brevig Mission/1/1918(H1N1)|Segment:8|Subtype:H1N1|Host:Human"
sequence = "ATGGATTCCAACACTGTGTCAAGCTTTCAGGTAGACTGCTTTCTTTGGCATGTCCGCAAACGGTTTGCAG\n\
ACCAAGAACTGGGTGATGCCCCATTCCTTGATCGGCTTCGCCGAGATCAGAAGTCCCTAAGAGGAAGAGG\n\
CAGCACTCTTGGTCTGGACATCGAGACAGCCACCCGTGCTGGAAAGCAGATAGTGGAGCGGATTCTGAAG\n\
GAAGAATCCGATGAGGCACTTAAAATGACCATTGCCTCTGTACCTGCTTCGCGCTACCTAACTGACATGA\n\
CTCTTGAGGAGATGTCAAGGGACTGGTTCATGCTCATGCCCAAGCAGAAAGTGGCAGGCTCTCTTTGTAT\n\
CAGAATGGACCAGGCGATCATGGATAAGAACATCATACTGAAAGCGAACTTCAGTGTGATTTTCGACCGG\n\
CTGGAGACTCTAATACTACTAAGGGCTTTCACCGAAGAGGGAGCAATTGTTGGCGAAATTTCACCATTGC\n\
CTTCTCTTCCAGGACATACTGATGAGGATGTCAAAAATGCAGTTGGGGTCCTCATCGGAGGACTTGAATG\n\
GAATGATAACACAGTTCGAGTCTCTGAAACTCTACAGAGATTCGCTTGGAGAAGCAGTAATGAGAATGGG\n\
AGACCTCCACTCCCTCCAAAACAGAAACGGAAAATGGCGAGAACAATTAAGTCAGAAGTTTGAAGAAATA\n\
AGATGGTTGATTGAAGAAGTGAGACATAGACTGAAGATAACAGAGAATAGTTTTGAGCAAATAACATTTA\n\
TGCAAGCCTTACAACTATTGCTTGAAGTGGAGCAAGAGATAAGAACTTTCTCGTTTCAGCTTATTTAA"

"""
Explanation: Working with Files
This notebook builds upon the Python for Biologists and Software Carpentry materials.
Text versus Binary Files
A text file is a file that contains charaster or string data. Openning a text file in a text editor will display the file. Some exmaples of bioinformatics text documents include:

FASTA files of DNA or protein sequences
files containing output from command-line programs (e.g. BLAST)
FASTQ files containing DNA sequencing reads
HTML files
word processing documents
and of course, Python code

In contrast, many other files will be binary files – ones which are not made up of characters and lines, but of bytes. Examples include:

BAM files
image files (JPEGs and PNGs)
audio files
video files
compressed files (e.g. ZIP files)


Making a new file and writing to it
End of explanation
"""


help(open)

"""
Explanation: Lets say that we want to save the information above into a FASTA file. How do we do it?

We have to create a new file
We have to give the file a name
We have to tell python that we want to write to teh file, not just read form it.
As a good programmer we will close teh file when we are done with it

How do we create a new file?
We create a new file using the open() function of python
End of explanation
"""


# Now that we have the file name we will create a few file,
# which RETURNS a _file ahdnle_

my_file_connector = open("flu_seg_8.fasta", 'w')

# Now that we ahve a file handle we can write to the file handle

my_file_connector.write

my_file_connector.write(sequence)

len(sequence)

"""
Explanation: How to we read the help information?


What arguments (or options) are reguired? (Hint: file is a required argument in the open function)


What arguments are optional? (Hint: all other arguments are optional)


How do I know? (Hint: do you see any equal signs?)


Positional versus keyword


So, these python commands are the same:
open("new_file.txt")
open("new_file.txt", 'r')
open(file="new_file.txt", mode='r')

Lets write our data to a new file
End of explanation
"""


# No lets close our file taht we have written to

my_file_connector.close()

# %load flu_seg_8.fasta
>gb:AF333238|A/Brevig Mission/1/1918(H1N1)|Segment:8|Subtype:H1N1|Host:HumanATGGATTCCAACACTGTGTCAAGCTTTCAGGTAGACTGCTTTCTTTGGCATGTCCGCAAACGGTTTGCAG
ACCAAGAACTGGGTGATGCCCCATTCCTTGATCGGCTTCGCCGAGATCAGAAGTCCCTAAGAGGAAGAGG
CAGCACTCTTGGTCTGGACATCGAGACAGCCACCCGTGCTGGAAAGCAGATAGTGGAGCGGATTCTGAAG
GAAGAATCCGATGAGGCACTTAAAATGACCATTGCCTCTGTACCTGCTTCGCGCTACCTAACTGACATGA
CTCTTGAGGAGATGTCAAGGGACTGGTTCATGCTCATGCCCAAGCAGAAAGTGGCAGGCTCTCTTTGTAT
CAGAATGGACCAGGCGATCATGGATAAGAACATCATACTGAAAGCGAACTTCAGTGTGATTTTCGACCGG
CTGGAGACTCTAATACTACTAAGGGCTTTCACCGAAGAGGGAGCAATTGTTGGCGAAATTTCACCATTGC
CTTCTCTTCCAGGACATACTGATGAGGATGTCAAAAATGCAGTTGGGGTCCTCATCGGAGGACTTGAATG
GAATGATAACACAGTTCGAGTCTCTGAAACTCTACAGAGATTCGCTTGGAGAAGCAGTAATGAGAATGGG
AGACCTCCACTCCCTCCAAAACAGAAACGGAAAATGGCGAGAACAATTAAGTCAGAAGTTTGAAGAAATA
AGATGGTTGATTGAAGAAGTGAGACATAGACTGAAGATAACAGAGAATAGTTTTGAGCAAATAACATTTA
TGCAAGCCTTACAACTATTGCTTGAAGTGGAGCAAGAGATAAGAACTTTCTCGTTTCAGCTTATTTAA

"""
Explanation: What is the number that is returned? It is the number of characters written by the write() function.
End of explanation
"""


my_file_connector.write(sequence_description + '\n')
my_file_connector.write(sequence)

# We ahve closed the stream to our file

my_file_connector = open('flu_seg_8.fasta', 'w')

%load flu_seg_8.fasta


"""
Explanation: Questions...

Did we create a file?
Is the file correctly formatted?
If not, how do we fix it?
How to we check it again?
End of explanation
"""


# Type the above command here and run it


"""
Explanation: Opening a file
In Python, as in the physical world, we have to open a file before we can read what’s inside it. The Python function that carries out the job of opening a file is very sensibly called open. It takes one argument – a string which contains the name of the file – and returns a file object:
sequence_file = open('flu_seg_8.fasta")
End of explanation
"""


# Type the above commands here and run it


"""
Explanation: Files, contents and file names
When learning to work with files it’s very easy to get confused between a file handle, a file name, and the contents of a file. Take a look at the following bit of code:
my_file_name = "flu_seg_8.fasta"
my_file_handle = open(my_file_name)
my_file_contents = my_file_handle.read()
End of explanation
"""


# Type the above command here and run it



"""
Explanation: What type of object is each variable above? Try:
type(my_file_name)
type(my_file_handle)
type(my_file_contents)
End of explanation
"""


import glob
print(glob.glob('*.fasta'))

"""
Explanation: Common errors:
A common error is to try to use the read method on the wrong thing. Recall that read is a method that only works on file objects. If we try to use the read method on the file name:
my_file_name = "flu_seg_8.fasta"
my_contents = my_file_name.read()

we’ll get an AttributeError – Python will complain that strings don’t have a read method3 :
AttributeError: 'str' object has no attribute 'read'

Another common error is to use the file object when we meant to use the file contents. If we try to print the file object:
my_file_name = "flu_seg_8.fasta"
my_file = open('/Users/squiresrb/Doc/BIOF309/week3/my_file_name')
print(my_file)

we won’t get an error, but we’ll get an odd-looking line of output:
&lt;open file 'flu_seg_8.fasta', mode 'r' at 0x7fc5ff7784b0&gt;

Missing files
What happens if we try to read a file that doesn’t exist?
my_file = open("nonexistent.txt")

We get a new type of error that we’ve not seen before:
IOError: [Errno 2] No such file or directory: 'nonexistent.txt'

Ideally, we’d like to be able to check if a file exists before we try to open it. To do this we use 
Advanced: Working with many files
We now have almost everything we need to process all our data files. The only thing that’s missing is a library with a rather unpleasant name:
import glob

The glob library contains a function, also called glob, that finds files and directories whose names match a pattern. We provide those patterns as strings: the character * matches zero or more characters, while ? matches any one character. We can use this to get the names of all the CSV files in the current directory:
print(glob.glob('*'))
End of explanation
"""


