#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""
A script to convert help fill in the TSV sheet of metadata.
"""

# Constants #
directory = "/home/lucass/proj32/INBOX/141219_M00485_0182_000000000-ABHAA/"
#directory = "/home/lucass/proj83/INBOX/150313_M00485_0189_000000000-AD6UJ/"
#directory = "/home/lucass/proj83/INBOX/150313_M00485_0189_000000000-AD6UJ/"

# Modules #
import illumitag
import natsort, pandas
from plumbing.autopaths import DirectoryPath
from collections import Counter
from tqdm import tqdm
from fasta import PairedFASTQ, FASTQ

# Print file names and md5 #
directory = DirectoryPath(directory)
samples = list(directory.flat_directories)
samples = natsort.natsorted(samples, alg=natsort.ns.IGNORECASE)
samples.pop(samples.index([s for s in samples if s.name == 'Plots'][0]))
samples.pop(samples.index([s for s in samples if s.name == 'Undetermined_indices'][0]))
for s in samples: print s.flat_files[0].filename
for s in samples: print s.flat_files[1].filename
for s in samples: print PairedFASTQ(*s.flat_files).count
for s in samples: print FASTQ(s.flat_files[0]).count if s.flat_files else ''
for s in samples: print FASTQ(s.flat_files[1]).count if s.flat_files else ''
for s in samples: print s.flat_files[0].md5 if s.flat_files else ''
for s in samples: print s.flat_files[1].md5 if s.flat_files else ''

# Get the indicies from pilar's table #
df = "/home/lucass/repos/illumitag/scripts/run012/orig/from_pilar.txt"
df = pandas.io.parsers.read_csv(df, sep='\t', index_col=0)
df = df.reindex([s.name for s in samples])
for i, row in df.iterrows(): print row['Extraction sample ID']
for i, row in df.iterrows(): print row['i5 (Fwd.)']
for i, row in df.iterrows(): print row['i7 (Rev.)']
revcompl = lambda x: ''.join([{'A':'T','C':'G','G':'C','T':'A'}[B] for B in x][::-1])
for i, row in df.iterrows(): print revcompl(row['i7 (Rev.)'])

# Figure out missing MIDs -- the forward index is missing a base pair #
undetermined = illumitag.runs[12][-1]
undetermined.load()
indices = Counter()
for read in tqdm(undetermined.fwd):
    mids = read.description[-15:] # Would be -16 normally
    indices.update([(mids[8:], mids[:8])])
print indices.most_common(100)
f9043e550f84bebc7819f226c1072209c0135a5c
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""
A script to convert help fill in the TSV sheet of metadata.
"""

# Constants #
directory = "/home/lucass/proj32/INBOX/141219_M00485_0182_000000000-ABHAA/"
#directory = "/home/lucass/proj83/INBOX/150313_M00485_0189_000000000-AD6UJ/"
#directory = "/home/lucass/proj83/INBOX/150313_M00485_0189_000000000-AD6UJ/"

# Modules #
import illumitag
import natsort, pandas
from plumbing.autopaths import DirectoryPath
from collections import Counter
from tqdm import tqdm
from fasta import PairedFASTQ, FASTQ

# Print file names and md5 #
directory = DirectoryPath(directory)
samples = list(directory.flat_directories)
samples = natsort.natsorted(samples, alg=natsort.ns.IGNORECASE)
samples.pop(samples.index([s for s in samples if s.name == 'Plots'][0]))
samples.pop(samples.index([s for s in samples if s.name == 'Undetermined_indices'][0]))
for s in samples: print s.flat_files[0].filename
for s in samples: print s.flat_files[1].filename
for s in samples: print PairedFASTQ(*s.flat_files).count
for s in samples: print FASTQ(s.flat_files[0]).count if s.flat_files else ''
for s in samples: print FASTQ(s.flat_files[1]).count if s.flat_files else ''
for s in samples: print s.flat_files[0].md5 if s.flat_files else ''
for s in samples: print s.flat_files[1].md5 if s.flat_files else ''

# Get the indicies from pilar's table #
df = "/home/lucass/repos/illumitag/scripts/run012/orig/from_pilar.txt"
df = pandas.io.parsers.read_csv(df, sep='\t', index_col=0)
df = df.reindex([s.name for s in samples])
for i, row in df.iterrows(): print row['Extraction sample ID']
for i, row in df.iterrows(): print row['i5 (Fwd.)']
for i, row in df.iterrows(): print row['i7 (Rev.)']
revcompl = lambda x: ''.join([{'A':'T','C':'G','G':'C','T':'A'}[B] for B in x][::-1])
for i, row in df.iterrows(): print revcompl(row['i7 (Rev.)'])

# Figure out missing MIDs -- the forward index is missing a base pair #
undetermined = illumitag.runs[12][-1]
undetermined.load()
indices = Counter()
for read in tqdm(undetermined.fwd):
    mids = read.description[-15:] # Would be -16 normally
    indices.update([(mids[8:], mids[:8])])
print indices.most_common(100)