def main(args):

    with fileutil.open_or_stdin(args.i) as fin:
        total_sum = np.sum(np.loadtxt(fin))

    with fileutil.open_or_stdout(args.o, 'w') as fout:
        print >> fout, total_sum
def main(args):

    with fileutil.open_or_stdin(args.i) as fin:
        M = np.loadtxt(fin, delimiter='\t')
        v = np.sum(M, axis=0) + np.sum(M, axis=1)

    with fileutil.open_or_stdout(args.o, 'w') as fout:
        np.savetxt(fout, v, fmt='%g', delimiter='\t')
def main(args):

    # read the description of the genetic code
    with open(args.code) as fin_gcode:
        arr = list(csv.reader(fin_gcode, delimiter='\t'))
        indices, aminos, codons = zip(*arr)
        if [int(x) for x in indices] != range(len(indices)):
            raise ValueError

    # read the input
    with fileutil.open_or_stdin(args.i) as fin:
        paragraphs = list(gen_paragraphs(fin))

    human_header = paragraphs[1][0]
    human_lines = paragraphs[1][1:]
    chimp_header = paragraphs[2][0]
    chimp_lines = paragraphs[2][1:]

    if human_header != 'Human_Horai':
        raise ValueError
    if chimp_header != 'Chimp_Horai':
        raise ValueError

    human_dna = ''.join(human_lines)
    human_codons = [human_dna[i:i+3] for i in range(0, len(human_dna), 3)]

    chimp_dna = ''.join(chimp_lines)
    chimp_codons = [chimp_dna[i:i+3] for i in range(0, len(chimp_dna), 3)]

    codon_alignment_columns = zip(*(human_codons, chimp_codons))

    patterns = design.get_pattern_array(codons, codon_alignment_columns)

    ncodons = len(codons)
    counts = np.zeros((ncodons, ncodons), dtype=int)
    for i, j in patterns:
        counts[i, j] += 1

    # write the (ncodons, ncodons) array of counts of human to chimp changes
    with fileutil.open_or_stdout(args.counts_out, 'w') as fout:
        np.savetxt(fout, counts, fmt='%g', delimiter='\t')
    for i, line in enumerate(paragraphs[0]):
        name = line.split()[0]
        name_to_index[name] = i

    # check that the selected taxa are in the file
    missing_taxa = set(selected_taxa) - set(name_to_index)
    if missing_taxa:
        raise Exception('could not find taxa: ' + ' '.join(missing_taxa))

    # write selected lines from each paragraph
    for p in paragraphs:
        for name in selected_taxa:
            i = name_to_index[name]
            print >> fout, p[i]
        print >> fout


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-i', default='-',
            help='input interleaved phylip file (default stdin)')
    parser.add_argument('-o', default='-',
            help='output interleaved phylip file (default stdout)')
    parser.add_argument('taxa', nargs='+',
            help='taxa to include in the output')
    args = parser.parse_args()
    with fileutil.open_or_stdin(args.i) as fin:
        with fileutil.open_or_stdout(args.o, 'w') as fout:
            main(args.taxa, fin, fout)

예제 #5
0
        counts[istate, jstate] += weight

    # write the count array
    np.savetxt(fout, counts, fmt='%g', delimiter='\t')


if __name__ == '__main__':

    # define the command line usage
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--taxa', required=True,
            help='a tabular file that defines the taxon ordering')
    parser.add_argument('--patterns', required=True,
            help='each row of this integer array is a pattern'),
    parser.add_argument('--pattern-weights',
            help='a list of pattern weights (by default all weights are 1)')
    parser.add_argument('--initial-taxon-name', required=True,
            help='name of the initial taxon')
    parser.add_argument('--final-taxon-name', required=True,
            help='name of the final taxon')
    parser.add_argument('--nstates', type=moretypes.pos_int,
            help='size of the Markov state space')
    parser.add_argument('--counts-out', default='-',
            help='write the count matrix here (default is stdout)')

    args = parser.parse_args()

    with fileutil.open_or_stdout(args.counts_out, 'w') as fout:
        main(args, fout)