djskj189 djskj189 - 11 days ago 6
C# Question

StreamWriter C# formatting output

Problem Statement

In order to run gene annotation software, I need to prepare two types of files, vcard files and coverage tables, and there has to be one-to-one match of vcard to coverage table. Since Im running 2k samples, its hard to identify which file is not one-to-one match. I know that both files have unique identifier numbers, hence, if both folders have files that have same unique numbers, i treat that as "same" file




I made a program that compares two folders and reports unique entries in each folder. To do so, I made two list that contains unique file names to each directory.

I want to format the report file (tab delimited .txt file) such that it looks something like below:

Unique in fdr1 Unique in fdr2
file x file a
file y file b
file z file c


I find this difficult to do because I have to iterate twice (since I have two lists), but there is no way of going back to the previous line in StreamWriter as far as I know. Basically, once I iterate through the first list and fill the first column, how can I fill the second column with the second list?

Can someone help me out with this?

Thanks

If design of the code has to change (i.e. one list instead of two), please let me know

As requested by some user, this is how I was going to do (not working version)

// Write report
using (StreamWriter sw = new StreamWriter(dest_txt.Text + @"\" + "Report.txt"))
{
// Write headers
sw.WriteLine("Unique Entries in Folder1" + "\t" + "Unique Entries in Folder2");

// Write unique entries in fdr1
foreach(string file in fdr1FileList)
{
sw.WriteLine(file + "\t");
}

// Write unique entries in fdr2
foreach (string file in fdr2FileList)
{
sw.WriteLine(file + "\t");
}
sw.Dispose();
}


As requested for my approach for finding unique entries, here's my code snippet

Dictionary<int, bool> fdr1Dict = new Dictionary<int, bool>();
Dictionary<int, bool> fdr2Dict = new Dictionary<int, bool>();

List<string> fdr1FileList = new List<string>();
List<string> fdr2FileList = new List<string>();

string fdr1Path = folder1_txt.Text;
string fdr2Path = folder2_txt.Text;

// File names in the specified directory; path not included
string[] fdr1FileNames = Directory.GetFiles(fdr1Path).Select(Path.GetFileName).ToArray();
string[] fdr2FileNames = Directory.GetFiles(fdr2Path).Select(Path.GetFileName).ToArray();

// Iterate through the first directory, and add GL number to dictionary
for(int i = 0; i < fdr1FileNames.Length; i++)
{
// Grabs only the number from the file name
string number = Regex.Match(fdr1FileNames[i], @"\d+").ToString();
int glNumber;

// Make sure it is a number
if(Int32.TryParse(number, out glNumber))
{
fdr1Dict[glNumber] = true;
}
// If number not present, raise exception
else
{
throw new Exception(String.Format("GL Number not found in: {0}", fdr1FileNames[i]));
}

}

// Iterate through the second directory, and add GL number to dictionary
for (int i = 0; i < fdr2FileNames.Length; i++)
{
// Grabs only the number from the file name
string number = Regex.Match(fdr2FileNames[i], @"\d+").ToString();
int glNumber;

// Make sure it is a number
if (Int32.TryParse(number, out glNumber))
{
fdr2Dict[glNumber] = true;
}
// If number not present, raise exception
else
{
throw new Exception(String.Format("GL Number not found in: {0}", fdr2FileNames[i]));
}
}

// Iterate through the first directory, and find files that are unique to it
for (int i = 0; i < fdr1FileNames.Length; i++)
{
int glNumber = Int32.Parse(Regex.Match(fdr1FileNames[i], @"\d+").Value);
// If same file is not present in the second folder add to the list
if(!fdr2Dict[glNumber])
{
fdr1FileList.Add(fdr1FileNames[i]);
}
}

// Iterate through the second directory, and find files that are unique to it
for (int i = 0; i < fdr2FileNames.Length; i++)
{
int glNumber = Int32.Parse(Regex.Match(fdr2FileNames[i], @"\d+").Value);
// If same file is not present in the first folder add to the list
if (!fdr1Dict[glNumber])
{
fdr2FileList.Add(fdr2FileNames[i]);
}

Sid Sid
Answer

I am a quite confident that this will work as I've tested it:

static void Main(string[] args)
{
    var firstDir = @"Path1";
    var secondDir = @"Path2";

    var firstDirFiles = System.IO.Directory.GetFiles(firstDir);
    var secondDirFiles = System.IO.Directory.GetFiles(secondDir);
    print2Dirs(firstDirFiles, secondDirFiles);

}

private static void print2Dirs(string[] firstDirFile, string[] secondDirFiles)
{
    var maxIndex = Math.Max(firstDirFile.Length, secondDirFiles.Length);

    using (StreamWriter streamWriter = new StreamWriter("result.txt"))
    {
        streamWriter.WriteLine(string.Format("{0,-150}{1,-150}", "Unique in fdr1", "Unique in fdr2"));
        for (int i = 0; i < maxIndex; i++)
        {
            streamWriter.WriteLine(string.Format("{0,-150}{1,-150}",
                firstDirFile.Length > i ? firstDirFile[i] : string.Empty,
                secondDirFiles.Length > i ? secondDirFiles[i] : string.Empty));
        }
    }
}

It's a quite simple code but if you need help understanding it just let me know :)

Comments