|
| 1 | +from __future__ import print_function |
| 2 | + |
| 3 | +from intermine.webservice import Service |
| 4 | +service = Service("https://yeastmine.yeastgenome.org/yeastmine/service") |
| 5 | + |
| 6 | +import csv |
| 7 | +import re |
| 8 | +import sys |
| 9 | +import os |
| 10 | +import datetime |
| 11 | +import pytz |
| 12 | +import tzlocal |
| 13 | + |
| 14 | +# Get Network Data from Yeastmine |
| 15 | + |
| 16 | +query = service.new_query("Gene") |
| 17 | + |
| 18 | +query.add_view( |
| 19 | + "primaryIdentifier", "secondaryIdentifier", "symbol", "name", "sgdAlias", |
| 20 | + "regulationSummary.summaryParagraph", |
| 21 | + "regulationSummary.publications.pubMedId", |
| 22 | + "regulationSummary.publications.citation" |
| 23 | +) |
| 24 | +query.outerjoin("regulationSummary.publications") |
| 25 | + |
| 26 | +regulators = {} |
| 27 | +all_genes = {} |
| 28 | +print("COLLECTING REGULATORS\n") |
| 29 | +for row in query.rows(): |
| 30 | + systematic_name = row["secondaryIdentifier"] |
| 31 | + standard_name = row["symbol"] |
| 32 | + if standard_name == None: |
| 33 | + standard_name = systematic_name |
| 34 | + |
| 35 | + regulators[standard_name] = systematic_name |
| 36 | + all_genes[standard_name] = systematic_name |
| 37 | + |
| 38 | +regulators_to_targets = {} |
| 39 | +all_targets = {} |
| 40 | + |
| 41 | + |
| 42 | +print("COLLECTING TARGETS\n") |
| 43 | +for regulator in regulators: |
| 44 | + query = service.new_query("Gene") |
| 45 | + query.add_constraint("regulatoryRegions", "TFBindingSite") |
| 46 | + query.add_view( |
| 47 | + "regulatoryRegions.regulator.symbol", |
| 48 | + "regulatoryRegions.regulator.secondaryIdentifier", "symbol", |
| 49 | + "secondaryIdentifier", "regulatoryRegions.regEvidence.ontologyTerm.name", |
| 50 | + "regulatoryRegions.regEvidence.ontologyTerm.identifier", |
| 51 | + "regulatoryRegions.experimentCondition", |
| 52 | + "regulatoryRegions.strainBackground", |
| 53 | + "regulatoryRegions.regulationDirection", |
| 54 | + "regulatoryRegions.publications.pubMedId", "regulatoryRegions.datasource", |
| 55 | + "regulatoryRegions.annotationType" |
| 56 | + ) |
| 57 | + query.add_sort_order("Gene.secondaryIdentifier", "ASC") |
| 58 | + query.add_constraint("regulatoryRegions.regulator", "LOOKUP", regulator, "S. cerevisiae", code="A") |
| 59 | + targets = {} |
| 60 | + |
| 61 | + for row in query.rows(): |
| 62 | + target_systematic_name = row["secondaryIdentifier"] |
| 63 | + target_standard_name = row["symbol"] |
| 64 | + if target_standard_name == None: |
| 65 | + target_standard_name = target_systematic_name |
| 66 | + targets[target_standard_name] = target_systematic_name |
| 67 | + all_targets[target_standard_name] = target_systematic_name |
| 68 | + all_genes[target_standard_name] = target_systematic_name |
| 69 | + |
| 70 | + regulators_to_targets[regulator] = { "systematic_name": regulators[regulator], "targets": targets} |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +def create_regulator_to_target_row(target, all_regulators): |
| 75 | + result = "" + target |
| 76 | + for regulator in all_regulators: |
| 77 | + if target in all_regulators[regulator]["targets"]: |
| 78 | + result += "\t" + "1" |
| 79 | + else: |
| 80 | + result += "\t" + "0" |
| 81 | + return result |
| 82 | + |
| 83 | + |
| 84 | +# Create files |
| 85 | + |
| 86 | +# Create folder paths |
| 87 | +if not os.path.exists('../script-results'): |
| 88 | + os.makedirs('../script-results') |
| 89 | + |
| 90 | +if not os.path.exists('../script-results/networks'): |
| 91 | + os.makedirs('../script-results/networks') |
| 92 | + |
| 93 | +if not os.path.exists('../script-results/processed-loader-files'): |
| 94 | + os.makedirs('../script-results/processed-loader-files') |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +# Files to be generated |
| 99 | + |
| 100 | +# Create Networks |
| 101 | + |
| 102 | +REGULATORS_TO_TARGETS_MATRIX = '../script-results/networks/regulators_to_targets.csv' |
| 103 | +REGULATORS_TO_REGULATORS_MATRIX = '../script-results/networks/regulators_to_regulators.csv' |
| 104 | + |
| 105 | + |
| 106 | +targets = [] |
| 107 | +for target in all_targets: |
| 108 | + if target != None: |
| 109 | + targets.append(target) |
| 110 | + |
| 111 | +regulators_list = [] |
| 112 | +for regulator in regulators_to_targets: |
| 113 | + if regulator != None: |
| 114 | + regulators_list.append(regulator) |
| 115 | + |
| 116 | +print(f'Creating REGULATORS TO TARGETS MATRIX\n') |
| 117 | +regulator_to_target_file = open(REGULATORS_TO_TARGETS_MATRIX, 'w') |
| 118 | +headers = "cols regulators/rows targets" |
| 119 | +headers += '\t'.join(regulators_list) |
| 120 | +regulator_to_target_file.write(f'{headers}\n') |
| 121 | +for target in targets: |
| 122 | + result = create_regulator_to_target_row(target, regulators_to_targets) |
| 123 | + if result != False: |
| 124 | + regulator_to_target_file.write(f'{result}\n') |
| 125 | +regulator_to_target_file.close() |
| 126 | + |
| 127 | +print(f'Creating REGULATORS TO TARGETS MATRIX\n') |
| 128 | +regulator_to_regulator_file = open(REGULATORS_TO_REGULATORS_MATRIX, 'w') |
| 129 | +headers = "cols regulators/rows targets" |
| 130 | +headers += '\t'.join(regulators_list) |
| 131 | +regulator_to_regulator_file.write(f'{headers}\n') |
| 132 | +for target in targets: |
| 133 | + result = create_regulator_to_target_row(target, regulators_to_targets) |
| 134 | + if result != False: |
| 135 | + regulator_to_regulator_file.write(f'{result}\n') |
| 136 | +regulator_to_regulator_file.close() |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +# Create loader-files |
| 141 | + |
| 142 | +# Source Table |
| 143 | + |
| 144 | +SOURCE_DESTINATION = '../script-results/processed-loader-files/source.csv' |
| 145 | +dt = datetime.datetime.now() |
| 146 | + |
| 147 | +year = dt.year |
| 148 | +month = f'{dt.month}' |
| 149 | +if len(month) == 1: |
| 150 | + month = "0" + month |
| 151 | +day = f'{dt.day}' |
| 152 | +if len(day) == 1: |
| 153 | + day = "0" + day |
| 154 | +hour = dt.hour |
| 155 | +minute = dt.minute |
| 156 | +second = dt.second |
| 157 | + |
| 158 | + |
| 159 | +timestamp = f'{year}-{month}-{day} {hour}:{minute}:{second}' |
| 160 | +source = "YeastMine - Saccharomyces Genome Database" |
| 161 | + |
| 162 | +source_file = open(SOURCE_DESTINATION, 'w') |
| 163 | +headers = f'Timestamp\tSource\n{timestamp}\t{source}' |
| 164 | +source_file.write(f'{headers}\n') |
| 165 | +source_file.close() |
| 166 | + |
| 167 | +# Gene Table |
| 168 | + |
| 169 | +GENE_DESTINATION = '../script-results/processed-loader-files/gene.csv' |
| 170 | + |
| 171 | +species = "Saccharomyces cerevisiae" |
| 172 | +taxon_id = "559292" |
| 173 | + |
| 174 | +print(f'Creating gene.csv\n') |
| 175 | +gene_file = open(GENE_DESTINATION, 'w') |
| 176 | +headers = f'Gene ID\tDisplay Gene ID\tSpecies\tTaxon ID\tRegulator' |
| 177 | +gene_file.write(f'{headers}\n') |
| 178 | +for gene in all_genes: |
| 179 | + if gene in regulators: |
| 180 | + gene_file.write(f'{all_genes[gene]}\t{gene}\t{species}\t{taxon_id}\ttrue\n') |
| 181 | + else: |
| 182 | + gene_file.write(f'{all_genes[gene]}\t{gene}\t{species}\t{taxon_id}\tfalse\n') |
| 183 | + |
| 184 | +gene_file.close() |
| 185 | + |
| 186 | + |
| 187 | +# Network Table |
| 188 | + |
| 189 | +NETWORK_DESTINATION = '../script-results/processed-loader-files/network.csv' |
| 190 | + |
| 191 | + |
| 192 | +print(f'Creating network.csv\n') |
| 193 | +network_file = open(NETWORK_DESTINATION, 'w') |
| 194 | +headers = f'Regulator Gene ID\tTarget Gene ID\tTaxon ID\tTimestamp\tSource' |
| 195 | +network_file.write(f'{headers}\n') |
| 196 | +for gene in regulators_to_targets: |
| 197 | + for target_gene in regulators_to_targets[gene]["targets"]: |
| 198 | + network_file.write(f'{regulators_to_targets[gene]["systematic_name"]}\t{regulators_to_targets[gene]["targets"][target_gene]}\t{taxon_id}\t{timestamp}\t{source}\n') |
| 199 | +network_file.close() |
0 commit comments