Skip to content

Timeseries filtering

gregcaporaso edited this page Dec 12, 2012 · 16 revisions

@jrrideout added qiime.filter.sample_ids_from_category_state_coverage (now merged - some additional features are coming soon). This lets us explore the effect of different timeseries filtering strategies. We pass a mapping file and can specify the minimum number of timepoints that an individual must have provided to include them and/or specific timepoints that must be present for a given individual. Here's how to use it if others want to explore the data.

# do some setup
In [0]: from qiime.filter import sample_ids_from_category_state_coverage as s
In [1]: f = list(open("./StudentMicrobiomeProject-map.txt",'U'))
In [2]: cc = "WeeksSinceStart"
In [3]: sc = "PersonalID"

# call help to learn how to use this function
In [4]: help(s)

# Now let's explore the data:
# How many individuals donated at least 1 timepoint?
In [7]: s(f,cc,sc,1)[1]
Out[7]: 128

# How many individuals donated at least 5 timepoint?
In [8]: s(f,cc,sc,5)[1]
Out[8]: 89

# What about 6 and up?
In [9]: s(f,cc,sc,6)[1]
Out[9]: 89
In [10]: s(f,cc,sc,7)[1]
Out[10]: 87
In [11]: s(f,cc,sc,8)[1]
Out[11]: 79
In [12]: s(f,cc,sc,9)[1]
Out[12]: 70
In [13]: s(f,cc,sc,10)[1]
Out[13]: 51
In [14]: s(f,cc,sc,11)[1]
Out[14]: 17
In [15]: s(f,cc,sc,12)[1]
Out[15]: 10

# We can also specify specific timepoints that we care about.
# How many individuals donated at samples at timepoints 0 and 10?
In [8]: s(f,cc,sc,1,[0,10])[1]
Out[8]: 51
# And some other specific timepoints
In [9]: s(f,cc,sc,1,[0,9])[1]
Out[9]: 50
In [10]: s(f,cc,sc,1,[0,8])[1]
Out[10]: 76
In [11]: s(f,cc,sc,1,[0,7])[1]
Out[11]: 54

# We can also check how many individuals we retain if we require a certain number of 
# timepoints in some range. For example, how many individuals provided at least 6 samples
# between timepoints 0 and 8 (optionally including those timepoints). 

In [29]: s(f,cc,sc,6,considered_states=[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8])[1]
Out[29]: 86

# ... or at least 7 of those timepoints
In [30]: s(f,cc,sc,7,considered_states=[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8])[1]
Out[30]: 82
# ... or at least 8 of those timepoints
In [31]: s(f,cc,sc,8,considered_states=[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8])[1]
Out[31]: 62

# Alternatively, if we require 7 samples between timepoints 0 and 9 we retain the same number of 
# individuals as with 6 samples between timepoints 0 and 8, so we'll go with that.
In [35]: s(f,cc,sc,7,considered_states=[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9])[1]
Out[35]: 86

The commands below illustrate how the GutTimeseries, TongueTimeseries, PalmTimeseries, ForeheadTimeseries, and AnyTimeseries columns were created in the mapping file.

# Filter the mapping file to create body-site-specific mapping files.
In [ ]: from qiime.filter import filter_mapping_file_by_metadata_states
In [ ]: open('./StudentMicrobiomeProject_forehead.txt','w').write(filter_mapping_file_by_metadata_states(open('./StudentMicrobiomeProject-map.txt','U'),"BodySite:forehead"))
In [ ]: open('./StudentMicrobiomeProject_gut.txt','w').write(filter_mapping_file_by_metadata_states(open('./StudentMicrobiomeProject-map.txt','U'),"BodySite:gut"))
In [ ]: open('./StudentMicrobiomeProject_palm.txt','w').write(filter_mapping_file_by_metadata_states(open('./StudentMicrobiomeProject-map.txt','U'),"BodySite:palm"))
In [ ]: open('./StudentMicrobiomeProject_tongue.txt','w').write(filter_mapping_file_by_metadata_states(open('./StudentMicrobiomeProject-map.txt','U'),"BodySite:tongue"))


# Determine number of individuals retained with on a per-body-site basis
In [ ]: def f(min_num_states,considered_states=None):
    sites = ['gut','tongue','forehead','palm']
    for site in sites:
        print site, s(open('./StudentMicrobiomeProject_%s.txt' % site,'U'),cc,sc,min_num_states,considered_states=considered_states)[1]
   ....:         

In [ ]: f(7,considered_states=[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9])
gut 84
tongue 86
forehead 86
palm 85

In [ ]: considered_states=[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9]

# Generate the list of sids with enough timepoints on a per-body-site basis
In [ ]: gut_sids = s(open('./StudentMicrobiomeProject_gut.txt','U'),cc,sc,7,considered_states=considered_states)[0]
In [ ]: tongue_sids = s(open('./StudentMicrobiomeProject_tongue.txt','U'),cc,sc,7,considered_states=considered_states)[0]
In [ ]: palm_sids = s(open('./StudentMicrobiomeProject_palm.txt','U'),cc,sc,7,considered_states=considered_states)[0]
In [ ]: forehead_sids = s(open('./StudentMicrobiomeProject_forehead.txt','U'),cc,sc,7,considered_states=considered_states)[0]

# Parse the mapping file to a dict
In [ ]: from qiime.parse import parse_mapping_file_to_dict
In [ ]: map = parse_mapping_file_to_dict(open('./StudentMicrobiomeProject-map.txt','U'))[0]

# Create a new mapping file for just this timeseries data which will be 
# merged with the mapping file in Google Docs. Note that we are only keeping the
# timepoints between WeeksSinceStart 0 and 8 (inclusive) so we have well-defined
# start and end points.
In [99]: g = open('ts_map.txt','w')

In [100]: for k, v in map.items():
    if v['WeeksSinceStart'] == 'na' or float(v['WeeksSinceStart']) > 9:
        g.write('%s\t%s\n' % (k,'\t'.join(["No"] * 5)))
    else:
        fields = []
        if k in gut_sids:
            fields.append('Yes')
        else:
            fields.append('No')
        if k in tongue_sids:
            fields.append('Yes')
        else:
            fields.append('No')
        if k in palm_sids:
            fields.append('Yes')
        else:
            fields.append('No')
        if k in forehead_sids:
            fields.append('Yes')
        else:
            fields.append('No')
        if 'Yes' in fields:
            fields.append('Yes')
        else:
            fields.append('No')
        g.write('%s\t%s\n' % (k,'\t'.join(fields)))
   .....:         

In [101]: g.close()