|
| 1 | +#!/usr/bin/env python |
| 2 | +#==============================================================================# |
| 3 | +# Copyright (C) 2016 Rodrigo Honorato |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | +#==============================================================================# |
| 18 | + |
| 19 | +#==============================================================================# |
| 20 | +# create a parsed database exploting CAZY html structure |
| 21 | +import os, sys, urllib, re, string, time, string |
| 22 | +from bs4 import BeautifulSoup |
| 23 | + |
| 24 | +def main(): |
| 25 | + #==============================================================================# |
| 26 | + # Species part |
| 27 | + #==============================================================================# |
| 28 | + print '>> Gathering species codes for species with full genomes' |
| 29 | + # a = archea // b = bacteria // e = eukaryota // v = virus |
| 30 | + species_domain_list = ['a', 'b', 'e', 'v'] |
| 31 | + species_dic = {} |
| 32 | + for initial in string.uppercase: |
| 33 | + for domain in species_domain_list: |
| 34 | + link = 'http://www.cazy.org/%s%s.html' % (domain, initial) |
| 35 | + f = urllib.urlopen(link) |
| 36 | + species_list_hp = f.read() |
| 37 | + # parse webpage |
| 38 | + index_list = re.findall('"http://www.cazy.org/(b\d.*).html" class="nav">(.*)</a>', species_list_hp) |
| 39 | + for entry in index_list: |
| 40 | + index, species = entry |
| 41 | + try: |
| 42 | + species_dic[species].append(index) |
| 43 | + except: |
| 44 | + species_dic[species] = [index] |
| 45 | + |
| 46 | + # Double check to see which of the species codes are valid |
| 47 | + for species in species_dic: |
| 48 | + entry_list = species_dic[species] |
| 49 | + if len(entry_list) > 1: |
| 50 | + # More than one entry for this species |
| 51 | + # > This is (likely) a duplicate |
| 52 | + # > Use the higher number, should be the newer page |
| 53 | + newer_entry = max([int(i.split('b')[-1]) for i in entry_list]) |
| 54 | + selected_entry = 'b%i' % newer_entry |
| 55 | + |
| 56 | + species_dic[species] = selected_entry |
| 57 | + else: |
| 58 | + species_dic[species] = species_dic[species][0] |
| 59 | + |
| 60 | + #==============================================================================# |
| 61 | + # Enzyme class part |
| 62 | + #==============================================================================# |
| 63 | + |
| 64 | + enzyme_classes = ['Glycoside-Hydrolases', |
| 65 | + 'GlycosylTransferases', |
| 66 | + 'Polysaccharide-Lyases', |
| 67 | + 'Carbohydrate-Esterases', |
| 68 | + 'Auxiliary-Activities'] |
| 69 | + |
| 70 | + db_dic = {} |
| 71 | + protein_counter = 0 |
| 72 | + for e_class in enzyme_classes: |
| 73 | + print '>> %s' % e_class |
| 74 | + main_class_link = 'http://www.cazy.org/%s.html' % e_class |
| 75 | + |
| 76 | + |
| 77 | + #==============================================================================# |
| 78 | + # Family section |
| 79 | + #==============================================================================# |
| 80 | + soup = BeautifulSoup(urllib.urlopen(main_class_link)) |
| 81 | + family_table = soup.findAll(name='table')[0] |
| 82 | + rows = family_table.findAll(name='td') |
| 83 | + |
| 84 | + family_list = [str(r.find('a')['href'].split('/')[-1].split('.html')[0]) for r in rows] |
| 85 | + |
| 86 | + print '>> %i families found on %s' % (len(family_list), main_class_link) |
| 87 | + #==============================================================================# |
| 88 | + # Identification section |
| 89 | + #==============================================================================# |
| 90 | + for family in family_list: |
| 91 | + print '> %s' % family |
| 92 | + # |
| 93 | + main_link = 'http://www.cazy.org/%s.html' % family |
| 94 | + family_soup = BeautifulSoup(urllib.urlopen(main_link)) |
| 95 | + # main_link_dic = {'http://www.cazy.org/%s_all.html#pagination_PRINC' % family: '', |
| 96 | + # 'http://www.cazy.org/%s_characterized.html#pagination_PRINC' % family: 'characterized'} |
| 97 | + #====================# |
| 98 | + superfamily_list = [l.find('a')['href'] for l in family_soup.findAll('span', attrs={'class':'choix'})][1:] |
| 99 | + |
| 100 | + # remove structure tab, for now |
| 101 | + superfamily_list = [f for f in superfamily_list if not 'structure' in f] |
| 102 | + |
| 103 | + # DEBUG |
| 104 | + # superfamily_list = superfamily_list[:-2] |
| 105 | + #====================# |
| 106 | + for main_link in superfamily_list: |
| 107 | + |
| 108 | + page_zero = main_link |
| 109 | + |
| 110 | + soup = BeautifulSoup(urllib.urlopen(main_link)) |
| 111 | + |
| 112 | + # Get page list for the family // 1, 2, 3, 4, 5, 7 |
| 113 | + page_index_list = soup.findAll(name = 'a', attrs={'class':'lien_pagination'}) |
| 114 | + # page_list = ['http://www.cazy.org/' + str(l['href']) for l in page_index_list] # deprecated |
| 115 | + if bool(page_index_list): |
| 116 | + first_page_idx = int(page_index_list[0]['href'].split('PRINC=')[-1].split('#')[0]) # be careful with this |
| 117 | + last_page_idx = int(page_index_list[-2]['href'].split('PRINC=')[-1].split('#')[0]) # be careful with this |
| 118 | + |
| 119 | + # generate page_list |
| 120 | + page_list = [] |
| 121 | + page_list.append(page_zero) |
| 122 | + for i in range(first_page_idx, last_page_idx+first_page_idx, first_page_idx): |
| 123 | + link = 'http://www.cazy.org/' + page_index_list[0]['href'].split('=')[0] + '=' + str(i) + '#' + page_index_list[0]['href'].split('#')[1] |
| 124 | + page_list.append(link) |
| 125 | + else: |
| 126 | + page_list = [page_zero] |
| 127 | + |
| 128 | + # page_list.append(main_link) # deprecated |
| 129 | + # page_list = list(set(page_list)) # deprecated |
| 130 | + for link in page_list: |
| 131 | + # print link |
| 132 | + # tr = rows // # td = cells |
| 133 | + soup = BeautifulSoup(urllib.urlopen(link)) |
| 134 | + table = soup.find('table', attrs={'class':'listing'}) |
| 135 | + domain = '' |
| 136 | + |
| 137 | + # consistency check to look for deleted families. i.e. GH21 |
| 138 | + try: |
| 139 | + check = table.findAll('tr') |
| 140 | + except AttributeError: |
| 141 | + # not a valid link, move on |
| 142 | + continue |
| 143 | + |
| 144 | + for row in table.findAll('tr'): |
| 145 | + try: |
| 146 | + if row['class'] == 'royaume' and row.text != 'Top': |
| 147 | + domain = str(row.text).lower() |
| 148 | + except: |
| 149 | + pass |
| 150 | + |
| 151 | + tds = row.findAll('td') |
| 152 | + if len(tds) > 1 and tds[0].text != 'Protein Name': |
| 153 | + # valid line |
| 154 | + db_dic[protein_counter] = {} |
| 155 | + |
| 156 | + db_dic[protein_counter]['protein_name'] = tds[0].text.replace(' ','') |
| 157 | + db_dic[protein_counter]['family'] = family |
| 158 | + db_dic[protein_counter]['domain'] = domain |
| 159 | + db_dic[protein_counter]['ec'] = tds[1].text.replace(' ','') |
| 160 | + db_dic[protein_counter]['organism'] = tds[2].text.replace(' ','') |
| 161 | + try: |
| 162 | + db_dic[protein_counter]['genbank'] = tds[3].find('a').text.replace(' ','') # get latest entry |
| 163 | + except: |
| 164 | + # there is a crazy aberration when there is no genbank available |
| 165 | + db_dic[protein_counter]['genbank'] = 'unavailable' |
| 166 | + # |
| 167 | + db_dic[protein_counter]['uniprot'] = tds[4].text.replace(' ','') |
| 168 | + db_dic[protein_counter]['pdb'] = tds[5].text.replace(' ','') |
| 169 | + |
| 170 | + # check if this is species has a complete genome |
| 171 | + try: |
| 172 | + db_dic[protein_counter]['organism_code'] = species_dic[tds[2].text.replace(' ','')] |
| 173 | + except: |
| 174 | + db_dic[protein_counter]['organism_code'] = 'invalid' |
| 175 | + |
| 176 | + # check if there are subfamilies |
| 177 | + try: |
| 178 | + db_dic[protein_counter]['subfamily'] = tds[6].text.replace(' ','') |
| 179 | + except: |
| 180 | + db_dic[protein_counter]['subfamily'] = '' |
| 181 | + |
| 182 | + if 'characterized' in main_link: |
| 183 | + db_dic[protein_counter]['tag'] = 'characterized' |
| 184 | + else: |
| 185 | + db_dic[protein_counter]['tag'] = '' |
| 186 | + # debug entries |
| 187 | + # print '\t'.join(db_dic[protein_counter].keys()) |
| 188 | + # print '\t'.join(db_dic[protein_counter].values()) |
| 189 | + protein_counter += 1 |
| 190 | + |
| 191 | + # Ouput |
| 192 | + output_f = 'CAZy_DB_%s.csv' % time.strftime("%d-%m-%Y") |
| 193 | + out = open(output_f, 'w') |
| 194 | + header = '\t'.join(db_dic[0].keys()) |
| 195 | + out.write(header + '\n') |
| 196 | + |
| 197 | + for p in db_dic: |
| 198 | + tbw = '\t'.join(db_dic[p].values()) |
| 199 | + tbw = tbw.encode('utf8') # make sure codification is ok |
| 200 | + out.write(tbw + '\n') |
| 201 | + |
| 202 | + out.close() |
| 203 | + |
| 204 | + sys.exit(0) |
| 205 | + |
| 206 | +if __name__ == '__main__': |
| 207 | + main() |
| 208 | +# done. |
0 commit comments