Hi,
I want to get some columns from text file with 7800 columns. the columns that I search about in this text file have the same header. the program that I have written in C# is as following:
int frcount;
int fccount=7800;
string fpkm_path = @"inputfile.txt";
List<int> list = new List<int>();
frcount = File.ReadLines(fpkm_path).Count();
string[,] fpkm_out = new string[frcount, fccount];
string out_txt = @"outputfile.txt";
string ex = "column_header";
try
{
fpkm_out = txt_TO_arr(fpkm_path, frcount, fccount);
list = No_in_List(ex, fpkm_out);
List_TO_Text(fpkm_out, list, out_txt);
}
tb.Text = "Done!!";
The function that I have used are as following:
1- txt_TO_arr: this will convert the text file to an array:
public string[,] txt_TO_arr(string txtfile,int rcount,int ccount) {
string[,] arr_in = new string[rcount, ccount];
using (StreamReader line_file = new StreamReader(txtfile))
{
int i = 0;
string line;
while ((line = line_file.ReadLine()) != null)
{
int j = 0;
var delimiters = new char[] { '\t' };
var segments = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
foreach (var segment in segments)
{
arr_in[i, j] = segment;
j = j + 1;
}
i = i + 1;
}
line_file.Close();
}
return arr_in;
}
2- No_in_List: which will create a list contain the position of the column that has the header that I am looking fore
public List<int> No_in_List(string id, string[,] fpkm_arr)
{
List<int> list = new List<int>();
list.Add(0);
for (int f = 1; f < fpkm_arr.GetLength(1); f++)
{
if (id == fpkm_arr[0, f])
{
list.Add(f);
}
}
return list;
}
3- List_TO_Text: which will create a text file that contain only the columns that have the header that I want
public void List_TO_Text(string[,] arr, List<int> list, string fileName) {
StreamWriter tw = new StreamWriter(fileName);
for (int i = 0; i < arr.GetLength(0); i++)
{
foreach (int item in list)
{
tw.Write(arr[i,item] + '\t');
}
tw.WriteLine();
}
tw.Close();
}
This script work fine with smaller text files like contain 1000 columns but don't get any result when trying to run it will my file with 7800 columns.
if anyone can help I will be very thankful.
Thank you.