Skip to content

Instantly share code, notes, and snippets.

@flashton2003
Created February 24, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flashton2003/43079947b79b65c99d02 to your computer and use it in GitHub Desktop.
Save flashton2003/43079947b79b65c99d02 to your computer and use it in GitHub Desktop.
from __future__ import division
import datetime
import random
## inhandle is formatted 'sample_id\tdate', no header
inhandle = '/Users/flashton/Desktop/sample_dates'
def read_file(inhandle):
res_dict = {}
with open(inhandle) as fi:
lines = fi.readlines()
lines = [x.strip() for x in lines]
for line in lines:
split_line = line.split('\t')
res_dict[split_line[0]] = split_line[1]
return res_dict
def calc_year_proportion(sample_date_dict):
## this is going to assume your data is in dd/mm/yyyy format - if it isn't you need to change lines 22 and 24 of this script
res_dict = {}
for each in sample_date_dict:
# print each, sample_date_dict[each]
split_date = sample_date_dict[each].split('/')
split_date = [int(x) for x in split_date]
date = datetime.date(split_date[2], split_date[1], split_date[0])
# print (date.timetuple().tm_yday / 365)
# print date.year
final_date = date.year + (date.timetuple().tm_yday / 365)
if str(final_date) in res_dict:
# print
# print each, final_date
final_date = final_date + (random.random() / 100000)
# print each, final_date
# if each == '2309_H14146069601-1':
# print final_date
res_dict[str(final_date)] = each
else:
res_dict[str(final_date)] = each
return res_dict
def main(inhandle):
sample_date_dict = read_file(inhandle)
# print sample_date_dict['2309_H14146069601-1']
# print len(sample_date_dict)
year_prop_date = calc_year_proportion(sample_date_dict)
# print len(year_prop_date)
for each in year_prop_date:
print year_prop_date[each] + '\t' + each
main(inhandle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment