-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_gtf_description.py
50 lines (44 loc) · 1.89 KB
/
get_gtf_description.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from bs4 import BeautifulSoup
import requests
from urllib.parse import urljoin
def get_tag_info():
"""function to get the description of gtf tags from Gencode
"""
r = requests.get("https://www.gencodegenes.org/pages/tags.html")
doc = BeautifulSoup(r.text, "html.parser")
descriptions = doc.find_all("dd")
terms = doc.find_all("dt")
with open("gtf_tags.csv", "w") as filewriter:
for i in range(0, len(terms)):
term = terms[i].text.strip()
description = descriptions[i].text.strip().replace("\n","")
description = " ".join(description.split())
filewriter.write("{},{}\n".format(term, description))
def get_format_definition():
"""function to get the gtf format description from Gencode
"""
r = requests.get("https://www.gencodegenes.org/pages/data_format.html")
doc = BeautifulSoup(r.text, "html.parser")
tables = doc.find_all("table")
for table in tables:
title = table["summary"]
title = title.replace(" ", "_")
title = "{}.tsv".format(title)
with open(title, "w") as filewriter:
header = table.find("thead")
column_names = header.find_all("th")
column_names = [c.text.strip().replace(" ","_") for c in column_names]
column_names = "\t".join(column_names)
filewriter.write(column_names)
table_lines = table.find_all("tr")
for lines in table_lines:
cells = lines.find_all("td")
cells = [c.text.strip().replace("\n", " ") for c in cells]
if cells and " " in cells[0]:
cells[0] = cells[0].split(" ")[0]
cells = [" ".join(c.split()) for c in cells]
cells = "\t".join(cells)
filewriter.write("{}\n".format(cells))
if __name__ == "__main__":
get_format_definition()
get_tag_info()