1 /* $Id:
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's offical duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Amelia Fong
27 *
28 * ===========================================================================
29 */
30 
31 /// @file: tabular.cpp
32 /// Formatting of pairwise sequence alignments in tabular form.
33 /// One line is printed for each alignment with tab-delimited fielalnVec.
34 
35 #include <ncbi_pch.hpp>
36 
37 #include <algo/blast/format/sam.hpp>
38 #include <serial/iterator.hpp>
39 #include <objtools/align_format/align_format_util.hpp>
40 
41 BEGIN_NCBI_SCOPE
42 USING_SCOPE(objects);
43 
CBlast_SAM_Formatter(CNcbiOstream & out,CScope & scope,const string & custom_spec,const CSAM_Formatter::SProgramInfo & info)44 CBlast_SAM_Formatter::CBlast_SAM_Formatter(CNcbiOstream& out, CScope& scope,
45 			                               const string & custom_spec,
46 			                               const CSAM_Formatter::SProgramInfo & info) :
47 			                               CSAM_Formatter(out,scope),
48 			                               m_refRow(1)
49 {
50 	CSAM_Formatter::SetFlag(CSAM_Formatter::fSAM_PlainSeqIds);
51 	x_ProcessCustomSpec(custom_spec, info);
52 }
53 
x_Print(const CSeq_align_set & aln)54 void CBlast_SAM_Formatter::x_Print(const CSeq_align_set & aln)
55 {
56 	if(m_refRow == 1) {
57 		CSeq_align_set sorted;
58 		sorted.Set() = aln.Get();
59 		sorted.Set().sort(align_format::CAlignFormatUtil::SortHspByMasterStartAscending);
60 		CSAM_Formatter::Print(sorted, m_refRow);
61 	}
62 	else {
63 		CSAM_Formatter::Print(aln, m_refRow);
64 	}
65 }
66 
Print(const CSeq_align_set & aln)67 void CBlast_SAM_Formatter::Print(const CSeq_align_set &  aln)
68 {
69 	if (aln.Get().front()->GetSegs().Which() == CSeq_align::C_Segs::e_Dendiag){
70 		CSeq_align_set d_aln;
71 		ITERATE(CSeq_align_set::Tdata, itr, aln.Get()){
72 			CRef<CSeq_align> dense_seg = align_format::CAlignFormatUtil::CreateDensegFromDendiag(**itr);
73 			CDense_seg::TScores & scores = dense_seg->SetSegs().SetDenseg().SetScores();
74 			dense_seg->SetScore().swap(scores);
75 			d_aln.Set().push_back(dense_seg);
76 		}
77 		x_Print(d_aln);
78 	}
79 	else {
80 		x_Print(aln);
81 	}
82 }
83 
x_ProcessCustomSpec(const string & custom_spec,const CSAM_Formatter::SProgramInfo & info)84 void CBlast_SAM_Formatter::x_ProcessCustomSpec(const string & custom_spec,
85 		                                       const CSAM_Formatter::SProgramInfo & info)
86 {
87 	vector<string> format_tokens;
88 	NStr::Split(custom_spec, " ", format_tokens);
89 	CSAM_Formatter::SetProgram(info);
90 	m_refRow = 1;
91 	ITERATE (vector<string>, iter, format_tokens) {
92 		 if("SR" == *iter) {
93 			m_refRow = 0;
94 		 }
95 		 if ("SQ" == *iter) {
96 			CSAM_Formatter::SetFlag(CSAM_Formatter::fSAM_SeqData);
97 		}
98 	}
99 
100 	if(m_refRow == 1) {
101 		CSAM_Formatter::SetGroupOrder(eGO_Reference);
102 		CSAM_Formatter::SetSortOrder(eSO_Coordinate);
103 	}
104 }
105 
106 
107 END_NCBI_SCOPE
108