<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_age_file(self):
 """ Write er_ages.txt based on updated ErMagicBuilder data object """

        if not self.write_ages:
            print('-I- No age data available to write')
            return
        first_headers = self.first_age_headers
        actual_headers = sorted(self.headers['age']['er'][0])
        for header in first_headers:
            if header in actual_headers:
                actual_headers.remove(header)
        add_headers = ['er_specimen_name', 'er_sample_name', 'er_site_name', 'er_location_name']
        actual_headers[:0] = first_headers
        full_headers = add_headers[:]
        full_headers.extend(actual_headers)

        header_string = '\t'.join(full_headers)
        ages = []
        for dtype in ['specimen', 'sample', 'site', 'location']:
            ages_list = sorted(self.data_lists[dtype][0], key=lambda item: item.name)
            ages.extend(ages_list)

        age_strings = []
        for age in ages:
            ind = self.ancestry.index(age.dtype)
            ancestors = ['' for num in range(len(self.ancestry) - (ind+2))]
            data_found = False
            string = ''
            if age.dtype == 'specimen':
                string += age.name + '\t'
            elif age.dtype == 'sample':
                string += '\t' + age.name + '\t'
            elif age.dtype == 'site':
                string += '\t\t' + age.name + '\t'
            elif age.dtype == 'location':
                string += '\t\t\t' + age.name + '\t'
            parent = age.get_parent()
            grandparent = None
            if parent:
                ancestors[0] = parent.name
                grandparent = parent.get_parent()
                if grandparent:
                    ancestors[1] = grandparent.name
                    greatgrandparent = grandparent.get_parent()
                    if greatgrandparent:
                        ancestors[2] = greatgrandparent.name
            for ancestor in ancestors:
                string += ancestor + '\t'
            for key in actual_headers:
                try:
                    add_string = age.age_data[key]
                except KeyError:
                    add_string = ''
                    age.age_data[key] = ''
                if add_string and not key == 'er_citation_names':
                    data_found = True
                if key == 'er_citation_names' and not add_string.strip('\t'):
                    add_string = 'This study'
                string += add_string + '\t'
            # prevent extra '' at the end of age string
            if string.endswith('\t'):
                string = string[:-1]
            # only write ages to file if there is data provided
            if data_found:
                age_strings.append(string)
        outfile = open(os.path.join(self.WD, 'er_ages.txt'), 'w')
        outfile.write('tab\ter_ages\n')
        outfile.write(header_string + '\n')
        if not age_strings:
            outfile.close()
            os.remove(os.path.join(self.WD, 'er_ages.txt'))
            return False
        for string in age_strings:
            outfile.write(string + '\n')
        outfile.close()
        return outfile