File: PerM.cpp

package info (click to toggle)
perm 0.4.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 976 kB
  • sloc: cpp: 13,499; makefile: 98; sh: 12
file content (171 lines) | stat: -rw-r--r-- 6,719 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//==============================================================
// Name        : PerM
// Author      : Yangho Chen at University of southern California.
// Version     : 0.4.0
// Copyright   : Open source
// Description : C++, Ansi-style
//============================================================================
#include "PairedReadsMapping.h"
#include "ReadsMapping.h"
#include "Genome_Index_TableQ.h"
#include "PairedReadsSet.h"
#include "ReadInBitsSet.h"
#include "ReadInBits.h"
#include "ParameterList.h"
#include "Filename.h"
#include "chdir.h"
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;

time_t startt, endt;

bool retriveReadSetsAndSettings\
(ParameterList& P, vector<string>& readSetsList1, vector<string>& readSetsList2)
{
    // (1) check reference
    if (!P.checkRefValidity()) {
        return(false);
    }
    // (2) check reads files
    if (getReadSetsFilenames(P, readSetsList1, readSetsList2)) {
        if ((int)readSetsList1.size() > 0) {
            const char* firstReadSetFileName = readSetsList1.at(0).c_str();
            P.bMappedSOLiDRead = P.bMappedSOLiDRead ||\
                                 is_colorspace_reads(P.readsFileFormat) ||\
                                 is_colorspace_reads(firstReadSetFileName);
            P.cFileFormatSymbol = getReadsFileFormatSymbol(firstReadSetFileName, P.readsFileFormat);
            P.readLength = getReadLength(firstReadSetFileName, P.cFileFormatSymbol);
        } else {
            LOG_INFO("Info %d: Cannot get read set from the read list.\n", ERROR_LOG);
            return(false);
        }
    } else if (atoi(P.readsFile) >= (int)MIN_READ_LENGTH) {
        P.bMakeIndex = true;
        P.bSaveIndex = true;
        P.bMappedSOLiDRead = is_colorspace_reads(P.readsFileFormat);
        P.readLength = atoi(P.readsFile); // Must call P.truncatReadLength() later.
    } else if (atoi(P.readsFile) > 0) {
        LOG_INFO("Info %d: Incorrect reads file or the read length is too short\n", ERROR_LOG);
        return(false);
    } else {
        return(false); // can not open the read file
    }
    P.truncatReadLength(); // truncated reads according to options or mapping long reads
    // (3) select seed according to the setting
    P.seedId = selectSeed(P);
    // (4) avoid to use the same name
    if (readSetsList1.size() > 1)
        P.outputFileN[0] = '\0';
    // (5) TODO remove the mask repeat process
    P.bMaskedMathRepeat \
        = (!P.bMatePairedReads && !P.bMappedLongRead && P.bExcludeAmbiguousReads);

    bool validSetting = true;
    validSetting &= printOptWarning4PairedEndOpts(P);
    validSetting &= printOptWarning4PairedEndOpts(P);
    return(validSetting);
}

string get_Index_Path(ParameterList P)
{
    if (hasTheExtName(P.refFile, ".index") || P.refFormat == "index" ) {
        return(string(P.refFile));
    } else {
        string indexPath = default_index_path(getBasename\
                                              (P.refFile), P.bMappedSOLiDRead, P.seedId, P.readLength);
        return(indexPath);
    }
}

void setQueryIndexSubThreshold(CGenome_Index_TableQ& indexTable, unsigned int subDiffThreshold)
{
    const unsigned int MAX_SUB_WHEN_QUERY_TABLE = 20;
    if (subDiffThreshold > MAX_SUB_WHEN_QUERY_TABLE) {
        indexTable.uiSubDiffThreshold = MAX_SUB_WHEN_QUERY_TABLE;
    } else {
        indexTable.uiSubDiffThreshold = subDiffThreshold;
    }
}

bool buildIndexTable(CGenome_Index_TableQ& indexTable, ParameterList& P)
{
    if (P.refFormat == "index" || hasTheExtName(P.refFile, ".index")) {
        LOG_INFO("\nInfo %d: Index file %s has incorrect format\n", ERROR_LOG, P.refFile);
    } else if (indexTable.getSeqFromFasta(P.refFile, P.refFormat)) {
        bool bMaskedMathRepeat = P.bMaskedMathRepeat && (atoi(P.readsFile) != 0);
        bool bMakeIndexSucessful = indexTable.make_index_table(P.anchorLength, P.seedId, P.bMappedSOLiDRead, bMaskedMathRepeat);
        if (bMakeIndexSucessful) {
            if (P.bSaveIndex) {
                const bool bPrintErrMsg = true;
                indexTable.save_index_table(P.indexFileN, bPrintErrMsg);
            }
            indexTable.bExcludeAmbiguous = P.bExcludeAmbiguousReads;
            setQueryIndexSubThreshold(indexTable, P.subDiffThreshold);
            return(true);
        } else {
            LOG_INFO("\nInfo %d: Faile to build index file %s.\n", ERROR_LOG, P.indexFileN);
        }
    }
    return(false);
}

/*
 * The main function construct or read in the index of a set of reference genome.
 * It maps the reads
 */
int main(int argc, const char* argv[])
{
    // testLongBases2Colors();
    // (1) Get parameters.
    ParameterList P = getParameterList(argc, argv);
    if (argc <= 2) return 0;
    vector<string> readSetsList1, readSetsList2;
    bool validSetting = retriveReadSetsAndSettings(P, readSetsList1, readSetsList2);
    if (validSetting) {
        P.printSetting();
    } else {
        LOG_INFO("\nInfo %d: Invalid or confusing command. Check the opt\n", CONFIG_LOG);
#ifdef WIN32
        STRIKE_KEY2CONTINUE;
#endif
        return(-1);
    }
    // (2) Build or read index table
    CGenome_Index_TableQ indexTable;
    indexTable.uiSubDiffThreshold = (unsigned int)P.subDiffThreshold;
    string indexPath = get_Index_Path(P);
    const bool bPrintWarning = false;
    if (P.bMakeIndex || indexTable.read_index_table(indexPath.c_str(), bPrintWarning) == false) {
        if (buildIndexTable(indexTable, P)  == false ) {
            return(-1);
        }
    }
    // testGenome_Index_TableQ(&indexTable);
    // testMappingLongRead(&indexTable);

    // (3) Mapped reads.
    if ( P.bMatePairedReads && !P.bMappedLongRead ) {
        if (readSetsList1.size() > 0 && (readSetsList1.size() == readSetsList2.size())) {
            TIME_INFO(parallelMappingPairedReads\
                      (readSetsList1, readSetsList2, indexTable, P),"Mapping paired reads\n");
        } else {
            TIME_INFO(parallelMappingPairedReads\
                      (readSetsList1, indexTable, P), "Mapping paired reads");
        }
    } else if (P.bMatePairedReads && P.bMappedLongRead ) {
        TIME_INFO(parallelMappingPairedLongReads(readSetsList1, readSetsList2, indexTable, P),\
                  "Mapped paired-ended long reads");
    } else if ( !P.bMatePairedReads && P.bMappedLongRead ) {
        TIME_INFO(parallelMappingLongReads(readSetsList1, indexTable, P),\
                  "Mapped single-ended long reads");
    } else if ( !P.bMatePairedReads && !P.bMappedLongRead ) {
        TIME_INFO(parallelMapping(readSetsList1, indexTable, P),\
                  "Mapped single-ended reads");
    }
    cout << endl;
    return 0;
}