~ubuntu-branches/ubuntu/vivid/idba/vivid

« back to all changes in this revision

Viewing changes to src/tools/sort_reads.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2014-02-13 13:57:55 UTC
  • Revision ID: package-import@ubuntu.com-20140213135755-c97jpl9wl5yzfwfw
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file fq2fa.cpp
 
3
 * @brief 
 
4
 * @author Yu Peng (ypeng@cs.hku.hk)
 
5
 * @version 1.0.6
 
6
 * @date 2011-10-31
 
7
 */
 
8
 
 
9
#include <algorithm>
 
10
#include <cctype>
 
11
#include <cstdio>
 
12
#include <cstring>
 
13
#include <deque>
 
14
#include <iostream>
 
15
#include <stdexcept>
 
16
 
 
17
#include "misc/options_description.h"
 
18
#include "misc/utils.h"
 
19
#include "sequence/sequence.h"
 
20
#include "sequence/sequence_io.h"
 
21
#include "sequence/short_sequence.h"
 
22
 
 
23
using namespace std;
 
24
 
 
25
bool is_paired = false;
 
26
bool is_merged = false;
 
27
bool is_filtered = false;
 
28
int min_length = 0;
 
29
deque<ShortSequence> reads;
 
30
vector<int> aux;
 
31
 
 
32
bool Compare(int x, int y)
 
33
{
 
34
    if (reads[x] != reads[y])
 
35
        return reads[x] < reads[y];
 
36
    else
 
37
        return reads[x+1] < reads[y+1];
 
38
}
 
39
 
 
40
int main(int argc, char *argv[])
 
41
{
 
42
    OptionsDescription desc;
 
43
    desc.AddOption("paired", "", is_paired, "if the reads are paired-end in one file");
 
44
    desc.AddOption("merge", "", is_merged, "if the reads are paired-end in two files");
 
45
    desc.AddOption("filter", "", is_filtered, "filter out reads containing 'N'");
 
46
    desc.AddOption("min_length", "", min_length, "minimum length ");
 
47
 
 
48
    try
 
49
    {
 
50
        desc.Parse(argc, argv);
 
51
 
 
52
        if (argc < 3)
 
53
            throw logic_error("not enough parameters");
 
54
 
 
55
    }
 
56
    catch (exception &e)
 
57
    {
 
58
        cerr << e.what() << endl;
 
59
        cerr << "fq2fa - Convert Fastq sequences to Fasta sequences." << endl;
 
60
        cerr << "Usage: fq2fa tmp.fq tmp.fa [...] " << endl;
 
61
        cerr << "       fq2fa --paired tmp.fq tmp.fa" << endl;
 
62
        cerr << "       fq2fa --merge tmp_1.fq tmp_2.fq tmp.fa" << endl;
 
63
        cerr << "Allowed Options: " << endl;
 
64
        cerr << desc << endl;
 
65
        exit(1);
 
66
    }
 
67
 
 
68
    ReadSequence(argv[1], reads);
 
69
 
 
70
    cout << "read" << endl;
 
71
    for (int i = 0; i < (int)reads.size(); i += 2)
 
72
    {
 
73
        if (reads[i+1] < reads[i])
 
74
            swap(reads[i], reads[i+1]);
 
75
 
 
76
        if ((int)reads[i].size() >= min_length && (int)reads[i+1].size() >= min_length)
 
77
            aux.push_back(i);
 
78
    }
 
79
 
 
80
    sort(aux.begin(), aux.end(), Compare);
 
81
    cout << "sort" << endl;
 
82
 
 
83
    int index = 0;
 
84
    int last = -1;
 
85
    FastaWriter writer(argv[2]);
 
86
    for (int i = 0; i < (int)aux.size(); ++i)
 
87
    {
 
88
        int id = aux[i];
 
89
        if (last == -1 || reads[id] != reads[last] || reads[id+1] != reads[last+1])
 
90
        {
 
91
            Sequence seq1(reads[id]);
 
92
            Sequence seq2(reads[id+1]);
 
93
            writer.Write(seq1, FormatString("reads_%d/1", index));
 
94
            writer.Write(seq2, FormatString("reads_%d/2", index));
 
95
            index += 2;
 
96
        }
 
97
 
 
98
        last = id;
 
99
    }
 
100
 
 
101
    cout << index << " " << reads.size();
 
102
 
 
103
    return 0;
 
104
}
 
105