import Bio
from Bio import Entrez

Entrez.email = "k.lyons@utexas.edu"

"""
Explanation: Script to find additional organisms to add to the tree, namely from crocodiles and birds. Not used in final paper.
End of explanation
"""


sp = ["Gallus gallus", "Struthio camelus", "Chrysemys picta", "Pelomedusa subrufa", "Crocodylus porosus", "Alligator mississippiensis"]

"""
Explanation: List of species we are interested in adding.
End of explanation
"""


ge = ["16S", "18S", "BDNF", "CMOS", "CYTB", "ND2", "ND4", "NT3", "PDC", "R35", "RAG1", "RAG2"]


results = []

"""
Explanation: List of loci in our dataset that we would like to add for these species.
End of explanation
"""


for i in sp:
    for j in ge:
        handle = Entrez.esearch(db="nucleotide", term = "{0}[Orgn] & {1}[Gene]".format(i, j))
        record = Entrez.read(handle)
        handle.close()
        if len(record["IdList"])!=0:
            print record["IdList"]
            results.append(record["IdList"])
print results        

print
    

"""
Explanation: Search loop: using Entrez database, pull DNA records matching our organisms and genes.
End of explanation
"""


from Bio import SeqIO
for sp in results:
    if len(sp)==0:
        pass
    elif len(sp)!=0:    
        for num in sp:
            handle = Entrez.efetch(db="nucleotide", rettype="gb", retmode="text", id="{0}".format(num))
            for seq_record in SeqIO.parse(handle, "gb"):
                if len(seq_record)<2699:
                    print num
                    print seq_record.id, seq_record.description[:50] + "..."
                    print "Sequence length %i," % len(seq_record)
            handle.close()

import pandas as pd
seqs = pd.read_csv("sequences.csv",index_col=0)
print seqs
print type(seqs)

bdnf = seqs.loc['BDNF',:]
print type(bdnf)

for x in bdnf:
    handle = Entrez.efetch(db="nucleotide", rettype="fasta", retmode="text", id="{0}".format(int(x)))
    seq_record = SeqIO.read(handle, "fasta")
    handle.close()
    print("%s with %i features" % (seq_record.id, len(seq_record.features)))


for x in bdnf:
    print (x)
    print type(x)

Copyright (c) <2014> <Katie Lyons, k.lyons@gmail.com>


Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

"""
Explanation: If sequences are of appropriate length, print the names.
End of explanation
"""


