forked from pycam/python-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpybasic_ex2_4_1.py
More file actions
29 lines (25 loc) · 1.06 KB
/
Copy pathpybasic_ex2_4_1.py
File metadata and controls
29 lines (25 loc) · 1.06 KB
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
# Script that reads a tab delimited file which has 4 columns:
# gene, chromosome, start and end coordinates; that computes each gene's length
# stores it into a dictionary; and writes the results into a new tab separated file.
gene_file = ('data/genes.txt')
output_file = "gene_lengths.txt"
results = []
# Read a tab delimited file which has 4 columns: gene, chrom, start and end.
with open(gene_file) as f:
header = f.readline()
for line in f:
gene, chrom, start, end = line.strip().split("\t")
# compute the length of each gene and
# store its name and corresponding length into a dictionary
row = {'gene': gene, 'length': int(end) - int(start) + 1}
results.append(row)
# print results
print(results)
# Write the results into a new tab separated file
with open(output_file, "w") as out:
out.write('gene' + "\t" + 'length' + "\n") # write header
for record in results:
out.write(record['gene'] + "\t" + str(record['length']) + "\n")
# print contents of output file
with open(output_file) as f:
print(f.read())