<commit_before>///A simple SAM viewer. Convert a SAM alignment file into a SVG vector graphic.
#include <fstream>
#include <iostream>
#include <string>

#include <seqan/store.h>
#include <seqan/misc/misc_cmdparser.h>
#include <seqan/misc/misc_svg.h>

using namespace seqan;

int main(int argc, const char *argv[])
{
	typedef FragmentStore<> TFragStore;
	typedef TFragStore::TContigPos TContigPos;
	
	//////////////////////////////////////////////////////////////////////////////
	// Define options
	CommandLineParser parser;
	addUsageLine(parser, "sam2svg [OPTION]... <SAM file> <SVG file>");
	addUsageLine(parser, "sam2svg [OPTION]... <SAM file> <GENOME file> <SVG file>");
	
	addOption(parser, CommandLineOption("c",  "contig",    "display only contig #NUM (default: show all contigs)", OptionType::Int | OptionType::Label | OptionType::List));
	addOption(parser, CommandLineOption("p",  "pos",    2, "set begin and end position (default: show whole strands)", OptionType::Int | OptionType::Label));
	addOption(parser, CommandLineOption("l",  "lines",  2, "set first and last line of the alignment (default: show whole alignment)", OptionType::Int | OptionType::Label));
	addOption(parser, CommandLineOption("a",  "ascii",     "write alignment in ASCII format instead of SVG", OptionType::Bool | OptionType::Label));
	requiredArguments(parser, 2);

	if (argc == 1)
	{
		shortHelp(parser, std::cerr);	// print short help and exit
		return 0;
	}

	bool stop = !parse(parser, argc, argv, std::cerr);

	//////////////////////////////////////////////////////////////////////////////
	// Extract and check options

	if (isSetLong(parser, "help") || isSetLong(parser, "version")) return 0;	// print help or version and exit
	
	String<unsigned> contigs;
	TContigPos left = 0;
	TContigPos right = SupremumValue<TContigPos>::VALUE;
	unsigned firstLine = 0;
	unsigned lastLine = SupremumValue<unsigned>::VALUE;
	bool inASCII = false;
	
	if (isSetLong(parser, "pos"))
	{
		getOptionValueLong(parser, "pos", 0, left);
		getOptionValueLong(parser, "pos", 1, right);
		if ((left >= right) && (stop = true))
			std::cerr << "Begin position must be less than end position." << std::endl;
	}

	if (isSetLong(parser, "lines"))
	{
		getOptionValueLong(parser, "lines", 0, firstLine);
		getOptionValueLong(parser, "lines", 1, lastLine);
		if ((firstLine >= lastLine) && (stop = true))
			std::cerr << "First line must be less or equal than last line." << std::endl;
	}

	TFragStore store;
	std::ifstream samFile(toCString(getArgumentValue(parser, 0)), std::ios_base::in | std::ios_base::binary);
	std::ofstream ascii;
	SVGFile svg;
	
	//////////////////////////////////////////////////////////////////////////////
	// Load SAM file
	if (!stop) read(samFile, store, SAM());
	
	//////////////////////////////////////////////////////////////////////////////
	// Choose contigs
	if (isSetLong(parser, "contig"))
	{
		resize(contigs, length(getOptionValuesLong(parser, "contig")));
		for (unsigned i = 0; i < length(contigs); ++i)
			getOptionValueLong(parser, "contig", i, contigs[i]);
	} else {
		resize(contigs, length(store.contigStore));
		for (unsigned i = 0; i < length(contigs); ++i)
			contigs[i] = i;
	}

	if (isSetLong(parser, "ascii"))
		inASCII = true;

	//////////////////////////////////////////////////////////////////////////////
	// Optionally load genome and open SVG file for writing
	if (!stop)
	{
		uint outArgNo = 1;
		if (argumentCount(parser) > 2)
		{
			if (!loadContigs(store, getArgumentValue(parser, 1)) && (stop = true))
				std::cerr << "Failed to load genome." << std::endl;
			++outArgNo;
		}
		
		if (!stop)
		{
			if (inASCII)
			{
				ascii.open(toCString(getArgumentValue(parser, outArgNo)), std::ios_base::out | std::ios_base::trunc);
				if (!ascii.is_open()) stop = true;
			} else
				if (!open(svg, toCString(getArgumentValue(parser, outArgNo)))) stop = true;
			
			if (stop) std::cerr << "Failed to open output file for writing." << std::endl;
		}
	}
	
	// something went wrong
	if (stop)
	{
		std::cerr << "Exiting ..." << std::endl;
		return 1;
	}

	//////////////////////////////////////////////////////////////////////////////
	// Output alignment
	AlignedReadLayout layout;
	std::cout << "Layouting reads ... " << std::flush;
	layoutAlignment(layout, store);
	std::cout << "done" << std::endl;

	for (unsigned i = 0; i < length(contigs); ++i)
		if (i < length(store.contigStore))
		{
			std::cout << "Writing contig " << contigs[i] << " ... " << std::flush;
			TContigPos right_ = right;
			if (right_ == SupremumValue<TContigPos>::VALUE)
			{
				right_ = 0;
				for (unsigned j = 0; j < length(layout.contigRows[i]); ++j)
				{
					unsigned id = back(layout.contigRows[i][j]);
					if (right_ < store.alignedReadStore[id].beginPos)
						right_ = store.alignedReadStore[id].beginPos;
					if (right_ < store.alignedReadStore[id].endPos)
						right_ = store.alignedReadStore[id].endPos;
				}
			}
			
			if (inASCII)
				printAlignment(ascii, Raw(), layout, store, contigs[i], left, right_, firstLine, lastLine);
			else
				printAlignment(svg, Raw(), layout, store, contigs[i], left, right_, firstLine, lastLine);
			
			std::cout << "done" << std::endl;
		}

	return 0;
}<commit_msg>fixed contig load order<commit_after>///A simple SAM viewer. Convert a SAM alignment file into a SVG vector graphic.
#include <fstream>
#include <iostream>
#include <string>

#include <seqan/store.h>
#include <seqan/misc/misc_cmdparser.h>
#include <seqan/misc/misc_svg.h>

using namespace seqan;

int main(int argc, const char *argv[])
{
	typedef FragmentStore<> TFragStore;
	typedef TFragStore::TContigPos TContigPos;
	
	//////////////////////////////////////////////////////////////////////////////
	// Define options
	CommandLineParser parser;
	addUsageLine(parser, "sam2svg [OPTION]... <SAM file> <SVG file>");
	addUsageLine(parser, "sam2svg [OPTION]... <SAM file> <GENOME file> <SVG file>");
	
	addOption(parser, CommandLineOption("c",  "contig",    "display only contig #NUM (default: show all contigs)", OptionType::Int | OptionType::Label | OptionType::List));
	addOption(parser, CommandLineOption("p",  "pos",    2, "set begin and end position (default: show whole strands)", OptionType::Int | OptionType::Label));
	addOption(parser, CommandLineOption("l",  "lines",  2, "set first and last line of the alignment (default: show whole alignment)", OptionType::Int | OptionType::Label));
	addOption(parser, CommandLineOption("a",  "ascii",     "output alignment in ASCII format instead of SVG", OptionType::Bool | OptionType::Label));
	requiredArguments(parser, 2);

	if (argc == 1)
	{
		shortHelp(parser, std::cerr);	// print short help and exit
		return 0;
	}

	bool stop = !parse(parser, argc, argv, std::cerr);

	//////////////////////////////////////////////////////////////////////////////
	// Extract and check options

	if (isSetLong(parser, "help") || isSetLong(parser, "version")) return 0;	// print help or version and exit
	
	String<unsigned> contigs;
	TContigPos left = 0;
	TContigPos right = SupremumValue<TContigPos>::VALUE;
	unsigned firstLine = 0;
	unsigned lastLine = SupremumValue<unsigned>::VALUE;
	bool inASCII = false;
	
	if (isSetLong(parser, "pos"))
	{
		getOptionValueLong(parser, "pos", 0, left);
		getOptionValueLong(parser, "pos", 1, right);
		if ((left >= right) && (stop = true))
			std::cerr << "Begin position must be less than end position." << std::endl;
	}

	if (isSetLong(parser, "lines"))
	{
		getOptionValueLong(parser, "lines", 0, firstLine);
		getOptionValueLong(parser, "lines", 1, lastLine);
		if ((firstLine >= lastLine) && (stop = true))
			std::cerr << "First line must be less or equal than last line." << std::endl;
	}

	TFragStore store;
	std::ifstream samFile(toCString(getArgumentValue(parser, 0)), std::ios_base::in | std::ios_base::binary);
	std::ofstream ascii;
	SVGFile svg;
	
	//////////////////////////////////////////////////////////////////////////////
	// Optionally load genome file
	uint outArgNo = 1;
	if (argumentCount(parser) > 2)
	{
		if (!loadContigs(store, getArgumentValue(parser, 1)) && (stop = true))
			std::cerr << "Failed to load genome." << std::endl;
		++outArgNo;
	}

	//////////////////////////////////////////////////////////////////////////////
	// Load SAM file
	if (!stop) read(samFile, store, SAM());
	
	//////////////////////////////////////////////////////////////////////////////
	// Choose contigs
	if (isSetLong(parser, "contig"))
	{
		resize(contigs, length(getOptionValuesLong(parser, "contig")));
		for (unsigned i = 0; i < length(contigs); ++i)
			getOptionValueLong(parser, "contig", i, contigs[i]);
	} else {
		resize(contigs, length(store.contigStore));
		for (unsigned i = 0; i < length(contigs); ++i)
			contigs[i] = i;
	}

	if (isSetLong(parser, "ascii"))
		inASCII = true;

	//////////////////////////////////////////////////////////////////////////////
	// Optionally load genome and open SVG file for writing
	if (!stop)
	{
		
		if (!stop)
		{
			if (inASCII)
			{
				ascii.open(toCString(getArgumentValue(parser, outArgNo)), std::ios_base::out | std::ios_base::trunc);
				if (!ascii.is_open()) stop = true;
			} else
				if (!open(svg, toCString(getArgumentValue(parser, outArgNo)))) stop = true;
			
			if (stop) std::cerr << "Failed to open output file for writing." << std::endl;
		}
	}
	
	// something went wrong
	if (stop)
	{
		std::cerr << "Exiting ..." << std::endl;
		return 1;
	}
	
	for(unsigned o=0;o<length(store.contigStore);++o)
	std::cerr<<store.contigNameStore[o]<<std::endl;

	//////////////////////////////////////////////////////////////////////////////
	// Output alignment
	AlignedReadLayout layout;
	std::cout << "Layouting reads ... " << std::flush;
	layoutAlignment(layout, store);
	std::cout << "done" << std::endl;

	for (unsigned i = 0; i < length(contigs); ++i)
		if (i < length(store.contigStore))
		{
			std::cout << "Writing contig " << contigs[i] << " ... " << std::flush;
			TContigPos right_ = right;
			if (right_ == SupremumValue<TContigPos>::VALUE)
			{
				right_ = 0;
				for (unsigned j = 0; j < length(layout.contigRows[i]); ++j)
				{
					unsigned id = back(layout.contigRows[i][j]);
					if (right_ < store.alignedReadStore[id].beginPos)
						right_ = store.alignedReadStore[id].beginPos;
					if (right_ < store.alignedReadStore[id].endPos)
						right_ = store.alignedReadStore[id].endPos;
				}
			}
			std::cout << left<<'\t'<<right_<<'\t'<<firstLine<<'\t'<<lastLine<<std::endl;
			if (inASCII)
				printAlignment(ascii, Raw(), layout, store, contigs[i], left, right_, firstLine, lastLine);
			else
				printAlignment(svg, Raw(), layout, store, contigs[i], left, right_, firstLine, lastLine);
			
			std::cout << "done" << std::endl;
		}

	return 0;
}
{'fraction_non_alphanumeric': 0.24218968572227922, 'fraction_numerical': 0.0047561316795672855, 'mean_word_length': 2.765449438202247, 'pattern_counts': {'":': 0, '<': 150, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '660121', 'n_tokens_mistral': 3763, 'n_tokens_neox': 3364, 'n_words': 884}